pub struct Exclusive<T>where
T: ?Sized,{ /* private fields */ }
exclusive_wrapper #98407)
Exclusive provides mutable access, also referred to as exclusive access to the underlying value. However, it only permits immutable, or shared access to the underlying value when that value is Sync.
While this may seem not very useful, it allows Exclusive to unconditionally implement Sync. Indeed, the safety requirements of Sync state that for Exclusive to be Sync, it must be sound to share across threads, that is, it must be sound for &Exclusive to cross thread boundaries. By design, a &Exclusive<T> for non-Sync T has no API whatsoever, making it useless, thus harmless, thus memory safe.
Certain constructs like Futures can only be used with exclusive access, and are often Send but not Sync, so Exclusive can be used as hint to the Rust compiler that something is Sync in practice.
Using a non-Sync future prevents the wrapping struct from being 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();
}
});
Exclusive ensures the struct is Sync without stripping the future of its functionality:
#![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();
})
});In some sense, Exclusive can be thought of as a compile-time version of a mutex, as the borrow-checker guarantees that only one &mut can exist for any value. This is a parallel with the fact that & and &mut references together can be thought of as a compile-time version of a read-write lock.
impl<T> Exclusive<T>
pub const fn new(t: T) -> Exclusive<T> β
exclusive_wrapper #98407)
Wrap a value in an Exclusive
pub const fn into_inner(self) -> T
exclusive_wrapper #98407)
Unwrap the value contained in the Exclusive
impl<T> Exclusive<T>where
T: ?Sized,pub const fn get_mut(&mut self) -> &mut T
exclusive_wrapper #98407)
Gets exclusive access to the underlying value.
pub const fn get_pin_mut(self: Pin<&mut Exclusive<T>>) -> Pin<&mut T>
exclusive_wrapper #98407)
Gets pinned exclusive access to the underlying value.
Exclusive is considered to structurally pin the underlying value, which means unpinned Exclusives can produce unpinned access to the underlying value, but pinned Exclusives only produce pinned access to the underlying value.
pub const fn from_mut(r: &mut T) -> &mut Exclusive<T> β
exclusive_wrapper #98407)
Build a mutable reference to an Exclusive<T> from a mutable reference to a T. This allows you to skip building an Exclusive with Exclusive::new.
pub const fn from_pin_mut(r: Pin<&mut T>) -> Pin<&mut Exclusive<T>>
exclusive_wrapper #98407)
Build a pinned mutable reference to an Exclusive<T> from a pinned mutable reference to a T. This allows you to skip building an Exclusive with Exclusive::new.
impl<T> AsRef<T> for Exclusive<T>where
T: Sync + ?Sized,fn as_ref(&self) -> &T
impl<T> Clone for Exclusive<T>where
T: Sync + Clone,fn clone(&self) -> Exclusive<T> β
fn clone_from(&mut self, source: &Self)
source. Read more
impl<R, G> Coroutine<R> for Exclusive<G>where
G: Coroutine<R> + ?Sized,type Yield = <G as Coroutine<R>>::Yield
coroutine_trait #43122)
type Return = <G as Coroutine<R>>::Return
coroutine_trait #43122)
fn resume(
self: Pin<&mut Exclusive<G>>,
arg: R,
) -> CoroutineState<<Exclusive<G> as Coroutine<R>>::Yield, <Exclusive<G> as Coroutine<R>>::Return>coroutine_trait #43122)
impl<T> Debug for Exclusive<T>where
T: ?Sized,fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
impl<T> Default for Exclusive<T>where
T: Default + ?Sized,impl<F, Args> Fn<Args> for Exclusive<F>where
F: Sync + Fn<Args>,
Args: Tuple,extern "rust-call" fn call(
&self,
args: Args,
) -> <Exclusive<F> as FnOnce<Args>>::Output βfn_traits #29625)
impl<F, Args> FnMut<Args> for Exclusive<F>where
F: FnMut<Args>,
Args: Tuple,extern "rust-call" fn call_mut(
&mut self,
args: Args,
) -> <Exclusive<F> as FnOnce<Args>>::Output βfn_traits #29625)
impl<F, Args> FnOnce<Args> for Exclusive<F>where
F: FnOnce<Args>,
Args: Tuple,type Output = <F as FnOnce<Args>>::Output
extern "rust-call" fn call_once(
self,
args: Args,
) -> <Exclusive<F> as FnOnce<Args>>::Output βfn_traits #29625)
impl<T> From<T> for Exclusive<T>
fn from(t: T) -> Exclusive<T> β
impl<T> Future for Exclusive<T>where
T: Future + ?Sized,type Output = <T as Future>::Output
fn poll(
self: Pin<&mut Exclusive<T>>,
cx: &mut Context<'_>,
) -> Poll<<Exclusive<T> as Future>::Output>impl<T> Hash for Exclusive<T>where
T: Sync + Hash + ?Sized,fn hash<H>(&self, state: &mut H)where
H: Hasher,fn hash_slice<H>(data: &[Self], state: &mut H)where
H: Hasher,
Self: Sized,impl<T> Ord for Exclusive<T>where
T: Sync + Ord + ?Sized,fn cmp(&self, other: &Exclusive<T>) -> Ordering
fn max(self, other: Self) -> Selfwhere
Self: Sized,fn min(self, other: Self) -> Selfwhere
Self: Sized,fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: Sized,impl<T, U> PartialEq<Exclusive<U>> for Exclusive<T>where
T: Sync + PartialEq<U> + ?Sized,
U: Sync + ?Sized,fn eq(&self, other: &Exclusive<U>) -> bool
self and other values to be equal, and is used by ==.fn ne(&self, other: &Rhs) -> bool
!=. The default implementation is almost always sufficient, and should not be overridden without very good reason.impl<T, U> PartialOrd<Exclusive<U>> for Exclusive<T>where
T: Sync + PartialOrd<U> + ?Sized,
U: Sync + ?Sized,fn partial_cmp(&self, other: &Exclusive<U>) -> Option<Ordering>
fn lt(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
fn gt(&self, other: &Rhs) -> bool
fn ge(&self, other: &Rhs) -> bool
impl<T> Copy for Exclusive<T>where
T: Sync + Copy,impl<T> Eq for Exclusive<T>where
T: Sync + Eq + ?Sized,impl<T> StructuralPartialEq for Exclusive<T>where
T: Sync + StructuralPartialEq + ?Sized,impl<T> Sync for Exclusive<T>where
T: ?Sized,impl<T> Freeze for Exclusive<T>where
T: Freeze + ?Sized,impl<T> RefUnwindSafe for Exclusive<T>where
T: RefUnwindSafe + ?Sized,impl<T> Send for Exclusive<T>where
T: Send + ?Sized,impl<T> Unpin for Exclusive<T>where
T: Unpin + ?Sized,impl<T> UnwindSafe for Exclusive<T>where
T: UnwindSafe + ?Sized,impl<T> Any for Twhere
T: 'static + ?Sized,impl<T> Borrow<T> for Twhere
T: ?Sized,impl<T> BorrowMut<T> for Twhere
T: ?Sized,impl<T> CloneToUninit for Twhere
T: Clone,unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit #126799)
impl<T> From<!> for T
fn from(t: !) -> T
impl<T> From<T> for T
fn from(t: T) -> T
Returns the argument unchanged.
impl<T, U> Into<U> for Twhere
U: From<T>,fn into(self) -> U
Calls U::from(self).
That is, this conversion is whatever the implementation of From<T> for U chooses to do.
impl<F> IntoFuture for Fwhere
F: Future,type Output = <F as Future>::Output
type IntoFuture = F
fn into_future(self) -> <F as IntoFuture>::IntoFuture
impl<F> Pattern for Fwhere
F: FnMut(char) -> bool,type Searcher<'a> = CharPredicateSearcher<'a, F>
pattern #27721)
fn into_searcher<'a>(self, haystack: &'a str) -> CharPredicateSearcher<'a, F>
pattern #27721)
self and the haystack to search in.fn is_contained_in<'a>(self, haystack: &'a str) -> bool
pattern #27721)
fn is_prefix_of<'a>(self, haystack: &'a str) -> bool
pattern #27721)
fn strip_prefix_of<'a>(self, haystack: &'a str) -> Option<&'a str>
pattern #27721)
fn is_suffix_of<'a>(self, haystack: &'a str) -> boolwhere
CharPredicateSearcher<'a, F>: ReverseSearcher<'a>,pattern #27721)
fn strip_suffix_of<'a>(self, haystack: &'a str) -> Option<&'a str>where
CharPredicateSearcher<'a, F>: ReverseSearcher<'a>,pattern #27721)
fn as_utf8_pattern(&self) -> Option<Utf8Pattern<'_>>
pattern #27721)
impl<T> ToOwned for Twhere
T: Clone,type Owned = T
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)
impl<T, U> TryFrom<U> for Twhere
U: Into<T>,type Error = Infallible
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for Twhere
U: TryFrom<T>,
Β© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/sync/struct.Exclusive.html