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 closed, 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.close();
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

Create a carrier that carries and owns target.

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

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.

Closes this carrier.

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

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

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

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.

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.

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

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

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

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

Trait Implementations

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

Formats the value using the given formatter. Read more

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

The resulting type after dereferencing.

Dereferences the value.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.