[][src]Struct borrow_as::LifeRef

#[repr(transparent)]pub struct LifeRef<'a, T> { /* fields omitted */ }

Container for value which remains valid over specified lifetime.

Implementations

impl<'a, T: ?Sized> LifeRef<'a, (Ref<T>,)>[src]

pub fn wrap_ref(r: &'a T) -> Self[src]

Wraps immutable reference with inner value represented as 1-tuple for chaining with other methods.

Example

let s = String::from("Referenced");
let r = borrow_as::LifeRef::wrap_ref(&s[..3]);
assert_eq!(r.0, "Ref");

impl<'a, T: ?Sized> LifeRef<'a, (Mut<T>,)>[src]

pub fn wrap_mut(r: &'a mut T) -> Self[src]

Wraps mutable reference with inner value represented as 1-tuple for chaining with other methods.

Example

let mut v = vec![1, 2, 3];
let r_mut = borrow_as::LifeRef::wrap_mut(v.as_mut_slice());
let v_mut = r_mut.0.as_slice_of_cells();
v_mut[2].set(4);
assert_eq!(v, [1, 2, 4]);

impl<'a, T> LifeRef<'a, T>[src]

pub fn wrap_life(self) -> LifeRef<'a, (T,)>[src]

Wraps inner value into 1-tuple for chaining with other methods.

Example

let u = borrow_as::LifeRef::from(true);
assert!(*u);
let uu = u.wrap_life();
assert!(uu.0);

pub fn add_ref<U: ?Sized>(self, r: &'a U) -> LifeRef<'a, T::Output> where
    T: Append<Ref<U>>,
    U: 'a, 
[src]

Extends inner tuple by one element which represents passed immutable reference. Supports extending up to 16 elements.

Example

let t = (0, 1);
let s = String::from("Referenced");
let r = borrow_as::LifeRef::wrap_ref(&t).add_ref(&s[..3]).add_ref(&42);
assert_eq!(r.0, &t);
assert_eq!(r.1, "Ref");
assert_eq!(r.2, &42);

pub fn add_mut<U: ?Sized>(self, r: &'a mut U) -> LifeRef<'a, T::Output> where
    T: Append<Mut<U>>,
    U: 'a, 
[src]

Extends inner tuple by one element which represents passed mutable reference. Supports extending up to 16 elements.

Example

let mut t = (0, 1);
let mut s = String::from("Unaltered");
let r = borrow_as::LifeRef::wrap_mut(&mut t).add_mut(&mut s);

let (y, x) = r.0.get();
r.0.set((x, y));

let mut r1 = r.1.take();
r1.replace_range(..3, "A");
r.1.set(r1);

assert_eq!(t, (1, 0));
assert_eq!(s, "Altered");

pub fn add_life<'b, U>(self, other: LifeRef<'b, U>) -> LifeRef<'a, T::Output> where
    T: Append<U>,
    'b: 'a, 
[src]

Extends inner tuple with extracted value from another LifeRef.

Note: other can't outlive self and its lifetime will be shortened accordingly.

Example

use borrow_as::LifeRef as Life;
struct A;
struct B;
struct C;
let a = Life::from(A).wrap_life();
let b = Life::from(B);
let c = Life::from(C);
let ab = a.add_life(b).wrap_life();
let abc: Life<'_, ((A, B), C)> = ab.add_life(c);

pub fn map_life<U>(self, f: impl Fn(T) -> U) -> LifeRef<'a, U>[src]

Converts wrapped value from one type to another.

Example

use borrow_as::*;
struct Test {
    x: Mut<String>,
    y: Ref<i32>,
    z: Mut<bool>,
}
 
let int = 42;
let mut string = String::from("Unaltered");
let mut flag = false;
let test = LifeRef::
    wrap_mut(&mut string)
    .add_ref(&int)
    .add_mut(&mut flag)
    .map_life(|(x, y, z)| Test { x, y, z });
 
let mut x = test.x.take();
x.replace_range(..3, "A");
test.x.set(x);
assert_eq!(test.y, &42);
test.z.set(!test.z.get());
 
assert_eq!(string, "Altered");
assert!(flag);

Trait Implementations

impl<T, '_> Borrow<T> for LifeRef<'_, T>[src]

impl<'a, T: Clone> Clone for LifeRef<'a, T>[src]

impl<'a, T: Copy> Copy for LifeRef<'a, T>[src]

impl<'a, T: Debug> Debug for LifeRef<'a, T>[src]

impl<'a, T: Default> Default for LifeRef<'a, T>[src]

impl<'a, T> Deref for LifeRef<'a, T>[src]

type Target = T

The resulting type after dereferencing.

impl<'a, T: Eq> Eq for LifeRef<'a, T>[src]

impl<T, '_> From<T> for LifeRef<'_, T>[src]

impl<'a, T: Hash> Hash for LifeRef<'a, T>[src]

impl<'a, T: Ord> Ord for LifeRef<'a, T>[src]

impl<'a, T: PartialEq> PartialEq<LifeRef<'a, T>> for LifeRef<'a, T>[src]

impl<'a, T: PartialOrd> PartialOrd<LifeRef<'a, T>> for LifeRef<'a, T>[src]

impl<'a, T> StructuralEq for LifeRef<'a, T>[src]

impl<'a, T> StructuralPartialEq for LifeRef<'a, T>[src]

Auto Trait Implementations

impl<'a, T> RefUnwindSafe for LifeRef<'a, T> where
    T: RefUnwindSafe

impl<'a, T> Send for LifeRef<'a, T> where
    T: Send + Sync

impl<'a, T> Sync for LifeRef<'a, T> where
    T: Sync

impl<'a, T> Unpin for LifeRef<'a, T> where
    T: Unpin

impl<'a, T> UnwindSafe for LifeRef<'a, T> where
    T: RefUnwindSafe + UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<!> for T[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.