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
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
F: FnOnce(&'a mut L) -> Fut,
Fut: IntoFuture<Output = G>,
pub async fn async_new<Fut, F>(source: L, func: F) -> Selfwhere
F: FnOnce(&'a mut L) -> Fut,
Fut: IntoFuture<Output = G>,
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
F: FnOnce(&'a mut L) -> Fut,
Fut: IntoFuture<Output = Result<G, E>>,
pub async fn async_try_new<E, Fut, F>(
source: L,
func: F
) -> Result<Self, Bound<E, L>>where
F: FnOnce(&'a mut L) -> Fut,
Fut: IntoFuture<Output = Result<G, E>>,
Bind data to an asynchronously obtained referrent or referrent error
sourcepub fn map<G2, F>(self, func: F) -> Bound<G2, L>where
F: FnOnce(G) -> G2,
pub fn map<G2, F>(self, func: F) -> Bound<G2, L>where
F: FnOnce(G) -> G2,
Replace the referrent
sourcepub fn try_map<G2, E, F>(self, func: F) -> Result<Bound<G2, L>, Bound<E, L>>where
F: FnOnce(G) -> Result<G2, E>,
pub fn try_map<G2, E, F>(self, func: F) -> Result<Bound<G2, L>, Bound<E, L>>where
F: FnOnce(G) -> Result<G2, E>,
Replace the referrent or produce a referrent error
sourcepub async fn async_map<G2, Fut, F>(self, func: F) -> Bound<G2, L>where
F: FnOnce(G) -> Fut,
Fut: IntoFuture<Output = G2>,
pub async fn async_map<G2, Fut, F>(self, func: F) -> Bound<G2, L>where
F: FnOnce(G) -> Fut,
Fut: IntoFuture<Output = G2>,
Asynchronously replace the referrent
sourcepub async fn async_try_map<G2, E, Fut, F>(
self,
func: F
) -> Result<Bound<G2, L>, Bound<E, L>>where
F: FnOnce(G) -> Fut,
Fut: IntoFuture<Output = Result<G2, E>>,
pub async fn async_try_map<G2, E, Fut, F>(
self,
func: F
) -> Result<Bound<G2, L>, Bound<E, L>>where
F: FnOnce(G) -> Fut,
Fut: IntoFuture<Output = Result<G2, E>>,
Asynchronously replace the referrent or produce a referrent error
sourcepub fn wrapped_mut(&mut self) -> &mut G
pub fn wrapped_mut(&mut self) -> &mut G
Modify the wrapped struct.
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 · sourceconst fn max(self, other: Self) -> Self
const fn max(self, other: Self) -> Self
1.21.0 · sourceconst fn min(self, other: Self) -> Self
const fn min(self, other: Self) -> Self
1.50.0 · sourceconst fn clamp(self, min: Self, max: Self) -> Selfwhere
Self: PartialOrd<Self>,
const 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 · sourceconst fn le(&self, other: &Rhs) -> bool
const fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more