pub struct Bound<G, U> { /* private fields */ }
Expand description

Derive a struct from a reference and use it safely. The source is boxed, then a reference to it is used to construct the dependent data which is stored inline. This is not currently possible within Rust’s type system so we need to use unsafe code.

Usage

In abstract terms, the struct binds a lifetime and a dependent like so:

use bound::Bound;
// Struct with a lifetime parameter based on its reference field
struct Derived<'a>(&'a [usize]);
// Data that the struct must depend on
let data = vec![1, 2, 3];
let bound = Bound::new(data, |d| Derived(d.as_slice()));
// bound keeps alive the data and wraps the struct so that
// you just need to keep this struct in scope
assert_eq!(bound.wrapped().0, &[1, 2, 3]);

If the dependent is something like a mutex guard you can write /// through it. To be precise, Deref, DerefMut, AsRef and AsMut are all forwarded if the dependent implements them.

The canonical example is an Arc<RwLock>:

use std::sync::{Arc, RwLock};
use bound::Bound;
let shared_data = Arc::new(RwLock::new(1));
let mut writer = Bound::try_new(shared_data.clone(), |a| a.write()).expect("Failed to lock");
*writer = 2;

Normally you wouldn’t be able to pass around a RwLockWriteGuard, but because this is just a struct with no lifetime parameters, this works intuitively with a Bound.

Implementations

Bind data to a referrent

Bind data to a referrent or a referrent error

Bind data to an asynchronously obtained referrent

Bind data to an asynchronously obtained referrent or referrent error

Replace the referrent

Replace the referrent or produce a referrent error

Asynchronously replace the referrent

Asynchronously replace the referrent or produce a referrent error

Drop the referent and get back the source

Access the wrapped struct

Modify the wrapped struct.

Trait Implementations

Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Formats the value using the given formatter. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Formats the value using the given formatter. Read more

Ensure that

  • derived gets dropped before source_ptr
  • source_ptr gets dropped
Executes the destructor for this type. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

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.

Converts the given value to a String. Read more
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.