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>
impl<T> ArcShift<T>
Sourcepub fn try_into_inner(self) -> Option<T>
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.
Sourcepub fn update(&mut self, val: T)
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.
Sourcepub fn rcu(&mut self, update: impl FnMut(&T) -> T) -> &T
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.
Sourcepub fn rcu_maybe(&mut self, update: impl FnMut(&T) -> Option<T>) -> bool
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>
impl<T: ?Sized> ArcShift<T>
Sourcepub fn from_box(input: Box<T>) -> ArcShift<T>
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.
Sourcepub fn try_get_mut(&mut self) -> Option<&mut T>
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.
Sourcepub fn update_box(&mut self, new_payload: Box<T>)
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]].
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.
Sourcepub fn downgrade(this: &ArcShift<T>) -> ArcShiftWeak<T>
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).
Trait Implementations§
impl<T: Sync + Send + ?Sized> Send for ArcShift<T>
SAFETY:
If T is Sync and Send, ArcShift<T> can also be Send
impl<T: Sync + Send + ?Sized> Sync for ArcShift<T>
SAFETY:
If T is Sync and Send, ArcShift<T> can also be Sync