Carrier

Struct Carrier 

Source
pub struct Carrier<T> { /* private fields */ }
Expand description

A Carrier that manages the lifetime of an instance of type T.

The carrier owns the instance (the target). References to the target can be obtained by calling the create_ref method. The references returned by the method will be valid as long as the reference is alive.

The carrier can be frozen, after which no new references can be obtained. The carrier can also wait for all references it gave out to be dropped. The ownership of target will be returned to the caller after the wait is complete. The caller can then carry out clean-ups or any other type of work that requires an owned instance of type T.

use more_sync::Carrier;

// Create a carrier that holds a mutex.
let carrier = Carrier::new(std::sync::Mutex::new(7usize));

// Ask for a reference to the value held by the carrier.
let ref_one = carrier.create_ref().unwrap();
assert_eq!(*ref_one.lock().unwrap(), 7);

// Reference returned by Carrier can be sent to another thread.
std::thread::spawn(move || *ref_one.lock().unwrap() = 8usize);

// Close the carrier, no new references can be created.
carrier.freeze();
assert!(carrier.create_ref().is_none());

// Shutdown the carrier and wait for all references to be dropped.
// The value held by carrier is returned.
let mutex_value = carrier.wait();
// Destroy the mutex.
assert!(matches!(mutex_value.into_inner(), Ok(8usize)));

Implementations§

Source§

impl<T> Carrier<T>

Source

pub fn new(target: T) -> Self

Create a carrier that carries and owns target.

Source

pub fn create_ref(&self) -> Option<CarrierRef<T>>

Creates a reference to the owned instance. Returns None if the carrier has been frozen.

Source

pub fn ref_count(&self) -> usize

Returns the number of outstanding references created by this carrier.

The returned value is obsolete as soon as this method returns. The count can change at any time.

Source

pub fn freeze(&self)

Closes this carrier.

No new references can be created after the carrier is frozen. A frozen carrier cannot be re-opened again.

Source

pub fn is_frozen(&self) -> bool

Returns true if the carrier has been frozen, false otherwise.

For the same carrier, once this method returns true it will never return false again.

Source

pub fn wait(self) -> T

Blocks the current thread until all references created by this carrier are dropped.

wait() consumes the carrier and returns the owned instance. It returns immediately if all references have been dropped.

Source

pub fn wait_timeout(self, timeout: Duration) -> Result<T, Self>

Like wait(), but waits for at most timeout.

Returns Ok and the owned instance if the wait was successful. Returns Err(self) if timed out. The reference count can change at any time. It is not guaranteed that the number of references is greater than zero when this method returns.

Source

pub fn shutdown(self) -> T

Closes the carrier and waits for all references to be dropped.

A freeze() followed by a wait(). See the comments in those two methods.

Source

pub fn shutdown_timeout(self, timeout: Duration) -> Result<T, Self>

Like shutdown(), but waits for at most timeout.

A freeze() followed by a wait_timeout(). See the comments in those two methods.

Trait Implementations§

Source§

impl<T> AsRef<T> for Carrier<T>

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T: Debug> Debug for Carrier<T>

Source§

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

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

impl<T: Default> Default for Carrier<T>

Source§

fn default() -> Carrier<T>

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

impl<T> Deref for Carrier<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

§

impl<T> !Freeze for Carrier<T>

§

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

§

impl<T> Send for Carrier<T>
where T: Sync + Send,

§

impl<T> Sync for Carrier<T>
where T: Sync + Send,

§

impl<T> Unpin for Carrier<T>

§

impl<T> UnwindSafe for Carrier<T>
where T: RefUnwindSafe,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.