Struct ArcShift

Source
pub struct ArcShift<T: ?Sized> { /* private fields */ }
Expand description

Smart pointer with similar use case as std::sync::Arc, but with the added ability to atomically replace the contents of the Arc. See crate documentation for more information.

let mut instance = ArcShift::new("test");
println!("Value: {:?}", instance.get());

Implementations§

Source§

impl<T> ArcShift<T>

Source

pub fn new(val: T) -> ArcShift<T>

Crate a new ArcShift instance with the given value.

Source

pub fn try_into_inner(self) -> Option<T>

Drops this ArcShift instance. If this was the last instance of the entire chain, the payload value is returned.

Source

pub fn update(&mut self, val: T)

Update the value of this ArcShift instance (and all derived from it) to the given value.

Note, other ArcShift instances will not see the new value until they call the ArcShift::get or ArcShift::reload methods.

Source

pub fn rcu(&mut self, update: impl FnMut(&T) -> T) -> &T

Atomically update the value.

The supplied closure ‘update’ is called with the previous value as an argument. It then constructs a new, updated value. This value replaces the previous value of the ArcShift instance. Note, if other threads are updating the arcshift chain concurrently, it may take multiple retries for the update to succeed. The closure will be called once for every attempt, until an attempt is successful. This means that the final updated value will have always been calculated from the previous set value.

Source

pub fn rcu_maybe(&mut self, update: impl FnMut(&T) -> Option<T>) -> bool

Atomically update the value.

The supplied closure ‘update’ is called with the previous value as an argument. It then constructs a new, updated value. This value replaces the previous value of the ArcShift instance. Note, if other threads are updating the arcshift chain concurrently, it may take multiple retries for the update to succeed. The closure will be called once for every attempt, until an attempt is successful. This means that the final updated value will have always been calculated from the previous set value.

If the closure returns None, the update is cancelled, and this method returns false.

Source§

impl<T: ?Sized> ArcShift<T>

Source

pub fn from_box(input: Box<T>) -> ArcShift<T>

Basically the same as doing ArcShift::new, but avoids copying the contents of ‘input’ to the stack, even as a temporary variable. This can be useful, if the type is too large to fit on the stack.

Source

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

Returns a mutable reference to the inner value.

This method returns None if there are any other ArcShift or ArcShiftWeak instances pointing to the same object chain.

Source

pub fn update_box(&mut self, new_payload: Box<T>)

See ArcShift::update .

This method allows converting from Box<T>. This can be useful since it means it’s possible to use unsized types, such as str or [[u8]].

Source

pub fn get(&mut self) -> &T

Reload this ArcShift instance, and return the latest value.

Source

pub fn shared_get(&self) -> &T

Get the current value of this ArcShift instance.

WARNING! This does not reload the pointer. Use ArcShift::reload() to reload the value, or ArcShift::get() to always get the newest value.

This method has the advantage that it doesn’t require &mut self access.

Source

pub fn downgrade(this: &ArcShift<T>) -> ArcShiftWeak<T>

Create an ArcShiftWeak<T> instance.

Weak pointers do not prevent the inner from being dropped.

The returned pointer can later be upgraded back to a regular ArcShift instance, if there is at least one remaining strong reference (i.e, ArcShift<T> instance).

Source

pub fn reload(&mut self)

Reload this instance, making it point to the most recent value.

Trait Implementations§

Source§

impl<T: ?Sized> Clone for ArcShift<T>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for ArcShift<T>
where T: Debug + ?Sized,

Source§

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

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

impl<T: ?Sized> Drop for ArcShift<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Sync + Send + ?Sized> Send for ArcShift<T>

SAFETY: If T is Sync and Send, ArcShift<T> can also be Send

Source§

impl<T: Sync + Send + ?Sized> Sync for ArcShift<T>

SAFETY: If T is Sync and Send, ArcShift<T> can also be Sync

Source§

impl<T> UnwindSafe for ArcShift<T>

Auto Trait Implementations§

§

impl<T> Freeze for ArcShift<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for ArcShift<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Unpin for ArcShift<T>
where T: ?Sized,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.