小编tok*_*a-n的帖子

&mut unsafe { } 和 unsafe { &mut } 有什么区别?

我想将*mut指针转换为&mut引用。

// Both setting a value to ptr and getting a value from ptr succeeds.
let ptr: &mut usize = unsafe { &mut *(VIRTUAL_ADDRESS_TO_ACCESS_FREE_PAGE as *mut usize) };
Run Code Online (Sandbox Code Playgroud)

这有效。但是,如果&mutunsafe块之外,代码将无法部分工作。*ptr = foo不会存储foo到内存ptr点,但let foo = *ptr会将值分配*ptrfoo

// Setting a value to ptr fails, but getting a value from ptr succeeds.
let ptr: &mut usize = &mut unsafe { *(VIRTUAL_ADDRESS_TO_ACCESS_FREE_PAGE as *mut usize) …
Run Code Online (Sandbox Code Playgroud)

rust

9
推荐指数
1
解决办法
215
查看次数

具有可能被处理器意外更改的值是否安全?

例如,假设我有一个PageTable值并将其物理地址注册到 CR3 寄存器中。

#![no_std]

use {
    spinning_top::{const_spinlock, Spinlock},
    x86_64::structures::paging::PageTable,
};

// The physical address of `PML4` is registered with CR3 register.
static PML4: Spinlock<PageTable> = const_spinlock(PageTable::new());
Run Code Online (Sandbox Code Playgroud)

PML4如果页面被访问,处理器会更改条目的已访问位。恐怕优化后的代码可能会使用存储在 CPU 寄存器或堆栈之一中的缓存值而不是内存中的实际值,从而导致使用该位的旧值。

在这种情况下,影响应该很小,我不在乎 Accessed 位。但是,一般来说,拥有一个处理器可能会意外更改的值(如 MMIO 和 DMA 缓冲区)或对此类值的引用是否安全?或者我应该每次通过带有read_volatile和的原始指针执行读-修改-写循环write_volatile

rust

9
推荐指数
1
解决办法
142
查看次数

`&lt;&gt;` 的 `&gt;` 处出现意外标记

我正在尝试在本地环境中遵循有关 React 的 Tic-Tac-Toe 教程。但是,当我运行时npm start,我遇到了语法错误<>

如何重现

  1. 在教程中间的“此时您的代码应如下所示:”下,单击示例代码右上角的“分叉”,并在表格中写入 1 到 9 的数字。
  2. 单击代码沙盒左上角的按钮,导航到“文件”,然后导出到 Zip,并将代码示例下载为 Zip 文件。
  3. 解压下载的Zip文件,并在项目根目录下依次执行npm install和。npm start
  4. 显示以下错误消息。
./src/App.js
Syntax error: Unexpected token (3:5)

  1 | export default function Board() {
  2 |   return (
> 3 |     <>
    |      ^
  4 |       <div className="board-row">
  5 |         <button className="square">1</button>
  6 |         <button className="square">2</button>
Run Code Online (Sandbox Code Playgroud)

问题

我该如何解决这个错误?尽管我可以在线继续该教程,但我更愿意在本地环境中继续该教程,在那里我可以获得 lsp、格式化程序等的帮助。

版本信息

Node.js:v18.12.1

npm:8.19.2

npm view react version在项目根目录上:18.2.0

javascript node.js npm reactjs

9
推荐指数
1
解决办法
2767
查看次数

Clippy 对静态声明说“参数太多”

该代码来自我的操作系统

#[global_allocator]
pub static ALLOCATOR: LockedHeap = LockedHeap::empty();
Run Code Online (Sandbox Code Playgroud)

Clippy 说这个函数有太多参数。

error: this function has too many arguments (4/3)
  --> src/mem/allocator/heap.rs:14:1
   |
14 | pub static ALLOCATOR: LockedHeap = LockedHeap::empty();
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
note: the lint level is defined here
  --> src/lib.rs:14:9
   |
14 | #![deny(clippy::all)]
   |         ^^^^^^^^^^^
   = note: `#[deny(clippy::too_many_arguments)]` implied by `#[deny(clippy::all)]`
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#too_many_arguments
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more …
Run Code Online (Sandbox Code Playgroud)

rust rust-clippy

5
推荐指数
1
解决办法
3413
查看次数

如何避免`#[global_allocator]`冲突?

假设我有三个箱子:foobarbaz

Cargo.toml

[workspace]
members = [
    "foo",
    "bar",
    "baz",
]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
Run Code Online (Sandbox Code Playgroud)

foo/Cargo.toml

[package]
name = "foo"
version = "0.1.0"
edition = "2018"

[lib]
name = "foo"
crate-type = ["staticlib"]

[dependencies]
bar = { path = "../bar", default-features = false }
Run Code Online (Sandbox Code Playgroud)

foo/src/lib.rs

[workspace]
members = [
    "foo",
    "bar",
    "baz",
]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"
Run Code Online (Sandbox Code Playgroud)

bar/Cargo.toml

[package]
name = "bar"
version …
Run Code Online (Sandbox Code Playgroud)

rust

5
推荐指数
1
解决办法
1652
查看次数

GitPython 的“git show”输出出现“文件不是 zip 文件”错误

重现问题的脚本

将此代码保存为 shell 脚本并运行它。该代码应该报告File is not a zip file错误。

#!/bin/bash

set -eu

mkdir foo
cd foo

pip install --user GitPython

echo foo > a
zip a.zip a

# -t option validates the zip file.
# See https://unix.stackexchange.com/questions/197127/test-integrity-of-zip-file
unzip -t a.zip

git init
git add a.zip
git commit -m 'init commit'

cat << EOF > test.py
from git import Repo
import zipfile
from io import StringIO

repo = Repo('.', search_parent_directories=True)

raw = repo.git.show("HEAD:a.zip")

z = zipfile.ZipFile(StringIO(raw), "r")
EOF

python3 …
Run Code Online (Sandbox Code Playgroud)

python git plugins

4
推荐指数
1
解决办法
649
查看次数

为什么即使我指定 is 作为类型限制,GHC 也会抱怨缺少 `Typeable` 实例?

即使我添加了边界Typeable来满足cast函数的要求,为什么以下代码也无法编译?

\n
import           Data.Data\n\na :: Maybe Int\na = cast f\n  where\n    f :: Typeable a => a\n    f = undefined\n
Run Code Online (Sandbox Code Playgroud)\n
app/Main.hs:6:5: error:\n    \xe2\x80\xa2 No instance for (Typeable a0) arising from a use of \xe2\x80\x98cast\xe2\x80\x99\n    \xe2\x80\xa2 In the expression: cast f\n      In an equation for \xe2\x80\x98a\xe2\x80\x99:\n          a = cast f\n            where\n                f :: Typeable a => a\n                f = undefined\n  |\n6 | a = cast f\n  |     ^^^^\n
Run Code Online (Sandbox Code Playgroud)\n

Maybe Int应该实现类型类,因为以下代码可以编译。

\n
app/Main.hs:6:5: error:\n    \xe2\x80\xa2 …
Run Code Online (Sandbox Code Playgroud)

haskell

1
推荐指数
1
解决办法
179
查看次数

验证函数是否为 haskell 中的回文

我尝试使用一个函数来计算列表的反转。所以我可以在其他函数中使用它,这将是回文函数,但我总是收到错误。第一个有效。

这是代码:

rev :: [a] -> [a]
rev [] = []
rev (x:xs) = rev xs ++ [x]

palindrome :: [a] -> Bool
palindrome [] = True
palindrome (x:xs) = if xs == rev (x:xs) then True else False
Run Code Online (Sandbox Code Playgroud)

我必须提到我必须使用该签名来做到这一点:[a] -> Bool

haskell

0
推荐指数
1
解决办法
94
查看次数

标签 统计

rust ×4

haskell ×2

git ×1

javascript ×1

node.js ×1

npm ×1

plugins ×1

python ×1

reactjs ×1

rust-clippy ×1