- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在思考为什么 Rust 中的内部可变性在大多数情况下需要运行时检查(例如 RefCell
)。看起来我找到了一个没有运行时成本的安全替代方案。我调用类型 SafeCell
(主要是因为它是围绕 UnsafeCell
的安全包装器),并且它允许您将任何函数应用于包装的值而没有引用转义的风险:
struct SafeCell<T> {
inner: UnsafeCell<T>,
}
impl<T> SafeCell<T> {
pub fn new(value: T) -> Self {
Self {
inner: UnsafeCell::new(value),
}
}
pub fn apply<R, F>(&self, fun: F) -> R
where
F: FnOnce(&mut T) -> R,
{
// Reference below has a lifetime of the current scope, so if
// user tries to save it somewhere, borrow checker will catch this.
let reference: &mut T = unsafe { &mut *self.inner.get() };
fun(reference)
}
}
这种类型可用于内部可变性,如下所示:
pub struct MySet {
set: HashSet<i32>,
unique_lookups: SafeCell<HashSet<i32>>,
}
impl MySet {
pub fn contains(&self, value: i32) -> bool {
self.unique_lookups.apply(|lookups| lookups.insert(value));
self.set.contains(value)
}
pub fn unique_lookups_count(&self) -> usize {
self.unique_lookups.apply(|lookups| lookups.len())
}
}
或与
Rc
一起使用:
fn foo(rc: Rc<SafeCell<String>>) {
rc.apply(|string| {
if string.starts_with("hello") {
string.push_str(", world!")
}
println!("{}", string);
});
}
Playground
RefCell
一样好用同时提供静态生命周期检查而不是运行时检查。 最佳答案
您的 API 中没有任何内容阻止用户调用 apply
再次在提供给 apply
的闭包中.这允许对同一数据有多个同时的可变引用,这是未定义的行为。
let x = SafeCell::new(0);
x.apply(|y| {
x.apply(|z| {
// `y` and `z` are now both mutable references to the same data
// UB!
*y = 1;
*z = 2;
})
});
x.apply(|y| println!("x: {}", y));
error: Undefined Behavior: not granting access to tag <untagged> because incompatible item is protected: [Unique for <1651> (call 1230)]
--> src/main.rs:20:42
|
20 | let reference: &mut T = unsafe { &mut *self.inner.get() };
| ^^^^^^^^^^^^^^^^^^^^^^ not granting access to tag <untagged> because incompatible item is protected: [Unique for <1651> (call 1230)]
|
关于rust - 为什么我的 RefCell 零成本替代方案不是实现内部可变性的标准方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61159541/
我遇到了 python 问题..我有一个二叉树节点类型: class NODE: element = 0 leftchild = None rightc
这个问题已经有答案了: Local (?) variable referenced before assignment [duplicate] (3 个回答) 已关闭 4 年前。 我有一些来自初学者编
class TestClass(object): def __init__(self): self.value = 100 self.x = lambda: s
谁能解释为什么下面的代码会编译,但如果我注释掉一行,那么它不会,即使代码本质上在做同样的事情? struct OtherStruct { x: i32, } struct Inner { bl
我有一个在同一主机上运行的 TCP 客户端和服务器。客户端发送消息,服务器确认该消息,然后客户端发送下一条消息。在任何时刻,都只有一条未处理的未确认消息。消息大小为 1KB。家庭计算机运行CentOs
我是 Panda 的 Dataframe 的新手,如果有人可以通过以下示例向我简要讨论 DataFrame 的可变性,我将不胜感激: d1=pd.date_range('1/1/2016',perio
在 C++ 编程语言第 4 版的第 16.2.9.4 节“通过间接实现的可变性”中,有一个使用间接代替 mutable 关键字进行惰性求值的示例的草图。 struct cache { bool
我对 Haskell 了解不多,但从我读到的关于计算的可变性(例如:函数返回函数、复杂的 monad 和函数等)的内容来看,你似乎可以做很多元编程,即使在运行时。 如果像函数和 monad 这样的一切
在 Python 中,globals() 返回全局符号表的表示,而 locals() 返回本地状态的表示。虽然两者都返回字典,但对 globals() 的更改会在全局符号表中生效,而对 locals(
我正在关注斯坦福算法 MOOC 并尝试使用 Haskell 解决问题。许多算法需要大量的数据处理,纯解决方案的运行速度比人们为命令式语言引用的基准要慢得多。所以我觉得我需要使用可变数据结构。 大多数
在 react 井字游戏中 tutorial ,为什么他们必须使用 Array.slice()? handleClick(i) { const squares = this.state.square
在 react 井字游戏中 tutorial ,为什么他们必须使用 Array.slice()? handleClick(i) { const squares = this.state.square
在 C# 中,我想制作“智能”枚举,这在 Java 中是可能的,其中有更多信息附加到枚举值,而不仅仅是底层 int。我偶然发现了一个创建类(而不是枚举)的方案,如以下简单示例所示: public se
我是一名优秀的程序员,十分优秀!