简体   繁体   English

为什么Rust的`Atomic *`类型使用非可变函数来改变值?

[英]Why do Rust's `Atomic*` types use non-mutable functions to mutate the value?

I notice that Rust's Atomic* structs have functions which modify the value, such as fetch_add . 我注意到Rust的Atomic*结构具有修改值的函数,例如fetch_add For instance, I can write this program: 例如,我可以写这个程序:

use std::sync::atomic::{AtomicUsize, Ordering};

struct Tester {
    counter: AtomicUsize
}

impl Tester {
    fn run(&self) {
        let counter = self.counter.fetch_add(1, Ordering::Relaxed);
        println!("Hi there, this has been run {} times", counter);
    }
}

fn main() {
    let t = Tester { counter: AtomicUsize::new(0) };
    t.run();
    t.run();
}

This compiles and runs fine, but if I change the AtomicUsize to a normal integer, it will (correctly) fail to compile due to mutability concerns: 这编译并运行正常,但如果我将AtomicUsize更改为正常整数,由于可变性问题,它将(正确)无法编译:

struct Tester {
    counter: u64
}

impl Tester {
    fn run(&self) {
        self.counter = self.counter + 1;
        println!("Hi there, this has been run {} times", self.counter);
    }
}

It wouldn't be very useful if it didn't work this way. 如果它不以这种方式工作就不会有用。 With &mut references, only one can exist at a time, and no & references at that time, so the whole question of atomicity of operation would be moot. 使用&mut引用,一次只能存在一个,并且当时没有&引用,所以整个操作原子性问题都没有实际意义。

Another way of looking at it is &mut are unique references, and & aliasable references. 查看它的另一种方法是&mut唯一引用和& 别名引用。 For normal types, mutation can only safely occur if you have a unique reference, but atomic types are all about mutation (via replacement) without needing a unique reference. 对于普通类型,只有具有唯一引用才能安全地发生变异,但原子类型都是关于变异(通过替换)而不需要唯一引用。

The naming of & and &mut has been a fraught matter, with much fear, uncertainty and doubt in the community and documents like Focusing on Ownership explaining how things actually are. &&mut的命名一直是一个令人担忧的问题,在社区中存在很多恐惧,不确定和怀疑,而像Focusing on Ownership这样的文件解释了事情的实际情况。 The language has ended up staying with & and &mut , but &mut is actually about uniqueness rather than mutability (it's just that most of the time the two are equivalent). 语言最终与&&mut保持一致,但是&mut实际上是关于唯一性而不是可变性(只是大多数情况下两者是等价的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Rust 中的非可变引用传递给不使用 const 的 C-API(即使它应该使用)? - How do I pass a non-mutable reference from Rust to a C-API that doesn't use const (even though it should)? 为什么 rust 的 read_line 函数使用可变引用而不是返回值? - Why does rust's read_line function use a mutable reference instead of a return value? 为什么通过原始指针修改可变引用的值不违反Rust的别名规则? - Why does modifying a mutable reference's value through a raw pointer not violate Rust's aliasing rules? 为什么 Rust 抱怨不使用这些类型实例的函数中类型的生命周期? - Why Rust complains about lifetime of types in functions that don't use instances of these types? 为什么 Rust 不能对不可变变量进行可变借用? - Why can't Rust do mutable borrow on an immutable varaiable? 为什么 Rust 生命周期会破坏循环中的可变引用? - Why do Rust lifetimes break mutable references in loops? 是否可以在 Rust 的常量 generics 上使用函数 - Is it possible to use functions on Rust's constant generics 为什么svd2rust生成的某些寄存器函数使用不安全,而另一些则不安全? - Why do some register functions generated by svd2rust use unsafe while others do not? Rust:如何构建函数的哈希图并在运行时对其进行变异 - Rust: how to build a hashmap of functions and mutate it at runtime 为什么我不能使用简单类型作为可变引用 - Why I cannot use simple types as mutable reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM