#[repr(transparent)]pub struct Exclusive<T>where
T: ?Sized,{ /* private fields */ }🔬This is a nightly-only experimental API. (
exclusive_wrapper #98407)Expand description
Exclusive 仅提供非法访问,也称为对底层值的独占访问。它不提供对底层值的不公开或共享访问。
虽然这看起来不是很有用,但它允许 Exclusive 无条件地实现 Sync。
事实上,Sync 的安全要求规定,Exclusive 要成为 Sync,跨线程共享必须是健全的,即 &Exclusive 跨线程边界必须是健全的。
根据设计,&Exclusive 没有任何 API,使其无用,因此无害,因此内存安全。
Future s 之类的某些结构只能用于独占访问,并且通常是 Send 而不是 Sync,因此 Exclusive 可以用作 rust 编译器的提示,即在实践中某些东西是 Sync。
Examples
使用非 Sync future 防止包装结构体为 Sync
ⓘ
use core::cell::Cell;
async fn other() {}
fn assert_sync<T: Sync>(t: T) {}
struct State<F> {
future: F
}
assert_sync(State {
future: async {
let cell = Cell::new(1);
let cell_ref = &cell;
other().await;
let value = cell_ref.get();
}
});RunExclusive 确保结构体是 Sync 而不会剥夺 future 的功能。
#![feature(exclusive_wrapper)]
use core::cell::Cell;
use core::sync::Exclusive;
async fn other() {}
fn assert_sync<T: Sync>(t: T) {}
struct State<F> {
future: Exclusive<F>
}
assert_sync(State {
future: Exclusive::new(async {
let cell = Cell::new(1);
let cell_ref = &cell;
other().await;
let value = cell_ref.get();
})
});Run与互斥锁平行
在某种意义上,Exclusive 可以被认为是互斥锁的编译时版本,因为借用检查器保证只有一个 &mut 可以存在任何值。
这与 & 和 &mut 引用一起可以被认为是读写锁的编译时版本的事实是并行的。
Implementations§
source§impl<T> Exclusive<T>where
T: ?Sized,
impl<T> Exclusive<T>where T: ?Sized,
sourcepub const fn get_mut(&mut self) -> &mut T
🔬This is a nightly-only experimental API. (exclusive_wrapper #98407)
pub const fn get_mut(&mut self) -> &mut T
exclusive_wrapper #98407)获得对,底层,值的独占访问权。
sourcepub const fn get_pin_mut(self: Pin<&mut Exclusive<T>>) -> Pin<&mut T>
🔬This is a nightly-only experimental API. (exclusive_wrapper #98407)
pub const fn get_pin_mut(self: Pin<&mut Exclusive<T>>) -> Pin<&mut T>
exclusive_wrapper #98407)获得对,底层,值的固定独占访问权。
Exclusive 被认为在结构上固定了,底层,值,这意味着未固定的 Exclusive 可以产生对,底层,值的未固定访问,但固定的 Exclusive 仅产生对,底层,值的固定访问。
sourcepub const fn from_mut(r: &mut T) -> &mut Exclusive<T> ⓘ
🔬This is a nightly-only experimental API. (exclusive_wrapper #98407)
pub const fn from_mut(r: &mut T) -> &mut Exclusive<T> ⓘ
exclusive_wrapper #98407)从可变引用到 T 构建可变引用到 Exclusive<T>。
这允许您跳过使用 Exclusive::new 构建 Exclusive。
sourcepub const fn from_pin_mut(r: Pin<&mut T>) -> Pin<&mut Exclusive<T>>
🔬This is a nightly-only experimental API. (exclusive_wrapper #98407)
pub const fn from_pin_mut(r: Pin<&mut T>) -> Pin<&mut Exclusive<T>>
exclusive_wrapper #98407)从固定可变引用到 T 构建固定可变引用到 Exclusive<T>。
这允许您跳过使用 Exclusive::new 构建 Exclusive。
Trait Implementations§
impl<T> Sync for Exclusive<T>where T: ?Sized,
Auto Trait Implementations§
impl<T: ?Sized> RefUnwindSafe for Exclusive<T>where T: RefUnwindSafe,
impl<T: ?Sized> Send for Exclusive<T>where T: Send,
impl<T: ?Sized> Unpin for Exclusive<T>where T: Unpin,
impl<T: ?Sized> UnwindSafe for Exclusive<T>where T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
从拥有的值中借用。 Read more
source§impl<F> IntoFuture for Fwhere
F: Future,
impl<F> IntoFuture for Fwhere F: Future,
§type IntoFuture = F
type IntoFuture = F
我们要把它变成哪种 future?
source§fn into_future(self) -> <F as IntoFuture>::IntoFuture
fn into_future(self) -> <F as IntoFuture>::IntoFuture
根据一个值创建一个 future。 Read more