- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法在结构中递增一个值。我收到很多不同的编译错误。我有一个对 self
的不可变引用,但我无法使其可变。
这是我的结构:
/// Proposer factory.
pub struct ProposerFactory<C, A>
where
A: txpool::ChainApi,
{
/// The client instance.
pub client: Arc<C>,
/// The transaction pool.
pub transaction_pool: Arc<TransactionPool<A>>,
/// The inherents pool
pub inherents_pool: Arc<InherentsPool<<A::Block as BlockT>::Extrinsic>>,
/// Current queue number
cur_queue_no_ptr: Arc<u64>,
}
我想将 cur_queue_no_ptr
增加 +1
我试过这段代码:
let old_value_ref = Arc::get_mut(&mut self.cur_queue_no_ptr).unwrap();
let old_value = *old_value_ref;
let new_value = old_value + 1;
但是出现了这个错误:
152 | let old_value_ref=Arc::get_mut(&mut self.cur_queue_no_ptr).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `self` is a `&` reference, so the data it refers to cannot be borrowed as mutable
我试过这段代码:
let copied_arc = Arc::clone(&self.cur_queue_no_ptr);
let old_value = *Arc::make_mut(&mut copied_arc);
let new_value = old_value + 1;
还有一个错误:
150 | let old_value = *Arc::make_mut(&mut copied_arc);
| ^^^^^^^^^^^^^^^ cannot borrow as mutable
我也试过 RefCell
但我得到了这个错误:
^^^^^ `std::cell::RefCell<u64>` cannot be shared between threads safely
显然文档中的示例只适用于变量而不适用于结构,那么如何使用结构来实现呢?
最佳答案
Arc
只允许您获得对内容的可变引用,前提是您对 Arc
对象本身有可变引用,而 Arc
是唯一指向它的内容的(所有其他的都已经被删除了)。
你在这里想要的是用于线程安全编码的 RefCell
的等价物之一,即 Mutex
或 RwLock
.这些将在您借用内容时锁定对内容的访问,以便您可以同时从多个线程安全地访问它们:
use std::sync::{Arc, Mutex};
fn example() {
// defining the counter variable
let counter = Arc::new(Mutex::new(0));
// lock the mutex to borrow
// it is automatically released when the borrow ends
let mut counter_lock = counter.lock().unwrap();
*counter_lock = *counter_lock + 1;
}
Mutex
只允许您可变地借用,这使它更简单,但有时还不够。 RwLock
也允许您进行不可变借用,因此您可以拥有一个可变借用或多个不可变借用。
或者,对于数字类型,最好使用 atomic types .这些是专门为整数设计的,比 Mutex
或 RwLock
更快(因为它们不需要锁定任何东西,更改会自动发生)。对于上面的计数器,相应的示例如下:
use std::sync::{
atomic::{AtomicU32, Ordering},
Arc,
};
fn example() {
// define the counter variable
let counter = Arc::new(AtomicU32::new(0));
// increment the counter
// no lock or mutable borrow is necessary
counter.fetch_add(1, Ordering::SeqCst);
}
关于rust - 如何更改结构中 Arc<u64> 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56228614/
我知道您可以使用 -fno-objc-arc 标志对 ARC 项目中不支持 ARC 的文件禁用 ARC。 有什么方法可以在非 ARC 项目中为支持 ARC 的文件启用 ARC 吗? 谢谢! 最佳答案
ARC 代表自动引用计数。 我刚刚升级了我的项目以使用 ARC。虽然不是全部。只是一些。 我怎么知道? 最佳答案 要查看默认值,您必须检查目标属性(您的目标 -> build设置 -> Apple L
我有一个框架 A,它是 ARC。这会消耗一些来自非 ARC 框架 B 的 API。 框架B示例代码(非ARC): @interface Settings : NSObject { NSDict
我一直避免升级 Xcode,直到某个特定项目完成(即将推出)。我希望能够在未来的项目中使用 ARC,同时保留维护和调查先前软件产品问题的能力。 问题是可以通过安装一次 Xcode 来完成此操作吗?当我
我正在开发一个非常旧的非基于 ARC 的项目,并添加了一些基于 ARC 的新 UIViewController(可以通过在构建阶段设置 -fobjc-arc 标志来实现) . 由于ARC和非ARC的混
这个问题在这里已经有了答案: How do you enable ARC project-wide in Xcode 4.2 (3 个答案) How to automatically conver
我是 iPhone 开发的新手。我了解到ARC是现在IOS引入的新特性。我的问题是,我有一个手动编写版本的旧非 ARC 项目,是否可以将该项目从非 ARC 切换到 ARC ? 谢谢, 拉吉 最佳答案
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 7年前关闭。 Improve this questi
我的项目是在 ARC 打开的情况下创建的,现在我使用的是第三方框架,该框架很可能不是用 ARC 构建的。这样做会有什么问题吗?我现在遇到了框架中某个地方发生的崩溃,但我没有得到有关崩溃的太多详细信息。
我猜这是一个简单的问题,但我无法弄清楚: 我的项目中存在三个与构建阶段中设置的链接器标志 -fno-obj-arc 不 ARC 兼容的文件。 但是据我所知,这些标志被忽略了。编译器仍然提示向对象发送保
我认为标题很好地解释了我的问题,我目前正在开发一个满足我个人需求的小型框架,并考虑使用 ARC 构建它(如果可能的话?),并在旧项目中使用它是在 ARC 之前 build 的吗? 最佳答案 是的,但有
我正在努力让现有的启用 ARC 的控件在不启用 ARC 的项目中在 Xcode 4.2 (OSX Snow Leopard) 下运行,并且我面临着各种问题: 我应该如何解决以下问题? 在属性中使用(s
我想知道如何更改 Arc 中的值,然后使 Arc 的其他副本再次有效。 use std::sync::Arc; use std::thread; use std::error::Error; use
ARC 运行时版本:我不知道,但我在这个网站上下载了它,https://chrome.google.com/webstore/detail/arc-welder/emfinbmielocnlhgmfk
当我将 iPhone 项目转换为 Objective - c ARC 时,我总是遇到相同的错误: /Applications/Xcode.app/Contents/Developer/Platform
如何在没有 ARC 的情况下为使用 ARC 的项目构建静态库? 我找到了一些静态库的源代码,我想为我的项目构建它,但我使用的是 ARC。 最佳答案 静态库可以在没有 ARC 的情况下构建,并且可以使用
我有一个使用 ARC 的 A 类和不使用 ARC 的其他 B 类和 C 类。如果 A 包含 B 类对象而 B 包含 C 类对象,那么 ARC 如何工作以及当 B 类和 C 类中的内存管理处理不当时会发
一旦我将 -fno-objc-arc 标志添加到构建阶段中的一个或多个单独的文件,我的 MyProject-Prefix.pch 文件就会进入非 ARC 模式,导致 150 条警告,如“警告:没有‘分
在某些情况下,我需要为几个非 ARC xcode 项目中的许多文件启用 ARC。我不想完全转换为 ARC,只需要使用 arc 启用几个文件。 有许多解决方案使用 XCode 控制台将 -fobjc-a
我还没有使用过 ARC,只是在它通过第 3 方代码强制进入项目时处理它。我已经阅读了所有 ARC 文档,但还没有看到这个问题的答案: 如果我在使用 -fobjc-arc 编译的模块中定义了一个类,我能
我是一名优秀的程序员,十分优秀!