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]);However you can only read the dependent this way. Writing the dependent can be unsound so it’s not allowed, but 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
sourceimpl<'a, G, L: 'a> Bound<G, L>
impl<'a, G, L: 'a> Bound<G, L>
sourcepub fn try_new<E, F>(source: L, func: F) -> Result<Self, Bound<E, L>>where
F: FnOnce(&'a mut L) -> Result<G, E>,
pub fn try_new<E, F>(source: L, func: F) -> Result<Self, Bound<E, L>>where
F: FnOnce(&'a mut L) -> Result<G, E>,
Bind data to a referrent or a referrent error
sourcepub async fn async_new<Fut, F>(source: L, func: F) -> Selfwhere
Fut: IntoFuture<Output = G>,
F: FnOnce(&'a mut L) -> Fut,
pub async fn async_new<Fut, F>(source: L, func: F) -> Selfwhere
Fut: IntoFuture<Output = G>,
F: FnOnce(&'a mut L) -> Fut,
Bind data to an asynchronously obtained referrent
sourcepub async fn async_try_new<E, Fut, F>(
source: L,
func: F
) -> Result<Self, Bound<E, L>>where
Fut: IntoFuture<Output = Result<G, E>>,
F: FnOnce(&'a mut L) -> Fut,
pub async fn async_try_new<E, Fut, F>(
source: L,
func: F
) -> Result<Self, Bound<E, L>>where
Fut: IntoFuture<Output = Result<G, E>>,
F: FnOnce(&'a mut L) -> Fut,
Bind data to an asynchronously obtained referrent or referrent error
Trait Implementations
sourceimpl<G, L> Drop for Bound<G, L>
impl<G, L> Drop for Bound<G, L>
Ensure that
derivedgets dropped beforesource_ptrsource_ptrgets dropped
sourceimpl<T: ?Sized, G, L> Ord for Bound<G, L>where
G: Deref<Target = T>,
T: Ord,
impl<T: ?Sized, G, L> Ord for Bound<G, L>where
G: Deref<Target = T>,
T: Ord,
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
sourceimpl<T: ?Sized, G, L, T2: ?Sized, G2, L2> PartialEq<Bound<G2, L2>> for Bound<G, L>where
G: Deref<Target = T>,
G2: Deref<Target = T2>,
T: PartialEq<T2>,
impl<T: ?Sized, G, L, T2: ?Sized, G2, L2> PartialEq<Bound<G2, L2>> for Bound<G, L>where
G: Deref<Target = T>,
G2: Deref<Target = T2>,
T: PartialEq<T2>,
sourceimpl<T: ?Sized, G, L, T2: ?Sized, G2, L2> PartialOrd<Bound<G2, L2>> for Bound<G, L>where
G: Deref<Target = T>,
G2: Deref<Target = T2>,
T: PartialOrd<T2>,
impl<T: ?Sized, G, L, T2: ?Sized, G2, L2> PartialOrd<Bound<G2, L2>> for Bound<G, L>where
G: Deref<Target = T>,
G2: Deref<Target = T2>,
T: PartialOrd<T2>,
sourcefn partial_cmp(&self, other: &Bound<G2, L2>) -> Option<Ordering>
fn partial_cmp(&self, other: &Bound<G2, L2>) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more