SynchronizedOptional

Struct SynchronizedOptional 

Source
pub struct SynchronizedOptional<T> { /* private fields */ }
Available on crate feature std only.
Expand description

Basically a RwLock<Option<Arc<T>>> - the benefits here being:

  1. This structure is Sync in that it can be shared between threads
  2. This structure can be lazily initialized, and then reset back to None, and back again
  3. This structure provides multiple access to a single shared instance, like a String so we’re not cloning it a bunch of times unnecessarily. (Arc<T>)

The Arc<T> bit is because you can’t return a &T out of a RwLock<T> the way you can from a std::sync::OnceLock<T>. The only reason this isn’t a OnceLock is because I wanted a swap method that was atomic, rather than relying on a std::sync::OnceLock<T>::take followed by a std::sync::OnceLock<T>::set, which allows a very slight race condition.

Implementations§

Source§

impl<T> SynchronizedOptional<T>

Source

pub fn empty() -> Self

Returns a new uninitialized/empty struct

Source

pub fn new(value: T) -> Self

Returns a new struct initialized with the provided value

Source

pub fn new_shared(value: Arc<T>) -> Self

Returns a new struct initialized with the provided already shared value

Source

pub fn get(&self) -> Option<Arc<T>>

If this struct has been initialized, returns a copy of the data, otherwise None

Source

pub fn set(&self, value: Option<T>) -> Result<(), Option<T>>

Sets the value to be the specified value, throwing away any value that was stored previously Returns the value provided as a parameter if it was unable to replace the value.

Source

pub fn set_shared(&self, value: Arc<T>) -> Result<(), Arc<T>>

Sets the value to be the specified value, throwing away any value that was stored previously Returns the value provided as a parameter if it was unable to replace the value.

Source

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

Takes the value out of this structure, leaving None in it’s place.

Source

pub fn swap(&self, value: T) -> Result<Option<Arc<T>>, T>

Swaps the value contained within this structure (if any) with the value provided. Upon success, returns the old value (which is possibly None). Will only fail if the lock has been poisoned, at which point it returns the provided value back to you.

Source

pub fn swap_shared(&self, value: Arc<T>) -> Result<Option<Arc<T>>, Arc<T>>

Swaps the value contained within this structure (if any) with the already shared value provided. Upon success, returns the old value (which is possibly None). Will only fail if the lock has been poisoned, at which point it returns the provided value back to you.

Trait Implementations§

Source§

impl<T> Debug for SynchronizedOptional<T>
where T: Debug,

Source§

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

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

impl<T> Default for SynchronizedOptional<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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<T, U> MaybeInto<U> for T
where U: MaybeFrom<T>,

Source§

fn maybe_into(self) -> Option<U>

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.