Struct SyncWrapper

Source
pub struct SyncWrapper<T>(/* private fields */);
Expand description

A wrapper around a non-Sync type to make it Sync.

The mechanism is very simple. SyncWrapper prevents shared borrowing of the inner type. The only way to get to the inner data is through take or get_mut. Both methods can only be called with a mutable reference to SyncWrapper, so it’s not possible that the inner value can be observed simultaneously in multiple threads.

Implementations§

Source§

impl<T> SyncWrapper<T>

Source

pub fn new(value: T) -> Self

Construct a new SyncWrapper.

Source

pub fn take(&mut self) -> Option<T>

Move out the inner value.

If this has already been called on this thread or elsewhere, this will return None.

Source

pub fn get_mut(&mut self) -> Option<&mut T>

Obtain a mutable reference to the inner value.

If take has been called previously, this returns None.

Trait Implementations§

Source§

impl<T: Debug> Debug for SyncWrapper<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Send> Sync for SyncWrapper<T>

§Safety

SyncWrapper prevents shared borrowing of the inner type. The only way to get to the inner data is through take or get_mut. Both methods can only be called with a mutable reference to SyncWrapper, so it’s not possible that the inner value can be observed simultaneously in multiple threads.

Therefore, this implementation is safe.

For further reference, see the standard library’s implementation of Sync on core::sync::Mutex, as well as the get_mut method.

Auto Trait Implementations§

§

impl<T> Freeze for SyncWrapper<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for SyncWrapper<T>
where T: RefUnwindSafe,

§

impl<T> Send for SyncWrapper<T>
where T: Send,

§

impl<T> Unpin for SyncWrapper<T>
where T: Unpin,

§

impl<T> UnwindSafe for SyncWrapper<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ConditionalSend for T
where T: Send,