pub struct SetOnce<T> { /* 私有字段 */ }展开描述
只能写入一次的线程安全单元。
SetOnce 灵感来自 Python 的 asyncio.Event 类型。它可用于等待 SetOnce 的值被设置,类似于"事件"机制。
§示例
use tokio::sync::{SetOnce, SetOnceError};
static ONCE: SetOnce<u32> = SetOnce::const_new();
// set the value inside a task somewhere...
tokio::spawn(async move { ONCE.set(20) });
// checking with .get doesn't block main thread
println!("{:?}", ONCE.get());
// wait until the value is set, blocks the thread
println!("{:?}", ONCE.wait().await);
Ok(())SetOnce 通常用于需要在首次使用时初始化一次但不需要进一步更改的全局变量。Tokio 中的 SetOnce 允许异步执行初始化过程。
§示例
use tokio::sync::{SetOnce, SetOnceError};
use std::sync::Arc;
let once = SetOnce::new();
let arc = Arc::new(once);
let first_cl = Arc::clone(&arc);
let second_cl = Arc::clone(&arc);
// set the value inside a task
tokio::spawn(async move { first_cl.set(20) }).await.unwrap()?;
// wait inside task to not block the main thread
tokio::spawn(async move {
// wait inside async context for the value to be set
assert_eq!(*second_cl.wait().await, 20);
}).await.unwrap();
// subsequent set calls will fail
assert!(arc.set(30).is_err());
println!("{:?}", arc.get());
Ok(())实现§
源代码§impl<T> SetOnce<T>
impl<T> SetOnce<T>
源代码pub const fn const_new() -> Self
pub const fn const_new() -> Self
创建一个新的空 SetOnce 实例。
等效于 SetOnce::new,但可以在静态变量中使用。
当使用 tracing 不稳定功能 时,使用 const_new 创建的 SetOnce 不会被检测。因此,它在 tokio-console 中不可见。如果需要,应改用 SetOnce::new 创建被检测的对象。
§示例
use tokio::sync::{SetOnce, SetOnceError};
static ONCE: SetOnce<u32> = SetOnce::const_new();
fn get_global_integer() -> Result<Option<&'static u32>, SetOnceError<u32>> {
ONCE.set(2)?;
Ok(ONCE.get())
}
let result = get_global_integer()?;
assert_eq!(result, Some(&2));
Ok(())源代码pub fn new_with(value: Option<T>) -> Self
pub fn new_with(value: Option<T>) -> Self
创建一个新的 SetOnce,其中包含所提供的值(如果有)。
如果 Option 为 None,则等效于 SetOnce::new。
源代码pub const fn const_new_with(value: T) -> Self
pub const fn const_new_with(value: T) -> Self
创建一个包含所提供值的新 SetOnce。
§示例
当使用 tracing 不稳定功能 时,使用 const_new_with 创建的 SetOnce 不会被检测。因此,它在 tokio-console 中不可见。如果需要,应改用 SetOnce::new_with 创建被检测的对象。
use tokio::sync::SetOnce;
static ONCE: SetOnce<u32> = SetOnce::const_new_with(1);
fn get_global_integer() -> Option<&'static u32> {
ONCE.get()
}
let result = get_global_integer();
assert_eq!(result, Some(&1));源代码pub fn initialized(&self) -> bool
pub fn initialized(&self) -> bool
如果 SetOnce 当前包含值,则返回 true,否则返回 false。
源代码pub fn set(&self, value: T) -> Result<(), SetOnceError<T>>
pub fn set(&self, value: T) -> Result<(), SetOnceError<T>>
如果 SetOnce 为空,则将其值设置为给定值。
如果 SetOnce 已经有值,则此调用将失败并返回 SetOnceError。
源代码pub fn into_inner(self) -> Option<T>
pub fn into_inner(self) -> Option<T>
从单元中取出值,此过程会销毁单元。如果单元为空,则返回 None。
trait 实现§
impl<T: Eq> Eq for SetOnce<T>
impl<T: Send> Send for SetOnce<T>
impl<T: Sync + Send> Sync for SetOnce<T>
自动 trait 实现§
impl<T> !Freeze for SetOnce<T>
impl<T> !RefUnwindSafe for SetOnce<T>
impl<T> Unpin for SetOnce<T>where
T: Unpin,
impl<T> UnsafeUnpin for SetOnce<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for SetOnce<T>where
T: UnwindSafe,
blanket 实现§
源代码§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
源代码§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. 更多信息