[go: up one dir, main page]

Struct mucell::Ref [] [src]

pub struct Ref<'b, T: 'b> {
    // some fields omitted
}

An immutable reference to a MuCell. Normally you should dereference to get at the object, but after transformation with Ref::map or Ref::filter_map you might instead use .into_inner().

Methods

impl<'b, T: Clone> Ref<'b, T>
[src]

fn clone(orig: &Ref<'b, T>) -> Ref<'b, T>

Copies a Ref.

The MuCell is already immutably borrowed, so this cannot fail.

This is an associated function that needs to be used as Ref::clone(...). A Clone implementation or a method would interfere with the widespread use of r.borrow().clone() to clone the contents of a MuCell.

impl<'b, T: 'static> Ref<'b, T>
[src]

fn into_inner(self) -> T

Consumes the Ref, returning the wrapped value.

The 'static constraint on T is what makes this possible; there is no longer any need to keep the borrow alive, and so the Ref itself can be consumed while keeping the contained value.

Examples

use mucell::{MuCell, Ref};
use std::borrow::Cow;

let c = MuCell::new("foo");

let r1: Ref<Cow<str>> = Ref::map(c.borrow(), |s| Cow::from(*s));
let r2: Ref<String> = Ref::map(r1, |s| s.into_owned());
let string: String = r2.into_inner();

impl<'b, T> Ref<'b, T>
[src]

fn map<U, F>(orig: Ref<'b, T>, f: F) -> Ref<'b, U> where F: FnOnce(T) -> U

Make a new Ref for a component of the borrowed data.

The MuCell is already immutably borrowed, so this cannot fail.

This is an associated function that needs to be used as Ref::map(...). A method would interfere with methods of the same name on the contents of a MuCell used through Deref.

Example

use mucell::{MuCell, Ref};

let c = MuCell::new((5, 'b'));
let b1: Ref<&(u32, char)> = c.borrow();
let b2: Ref<&u32> = Ref::map(b1, |t| &t.0);
assert_eq!(*b2, 5)

fn filter_map<U, F>(orig: Ref<'b, T>, f: F) -> Option<Ref<'b, U>> where F: FnOnce(T) -> Option<U>

Make a new Ref for a optional component of the borrowed data, e.g. an enum variant.

The MuCell is already immutably borrowed, so this cannot fail.

This is an associated function that needs to be used as Ref::filter_map(...). A method would interfere with methods of the same name on the contents of a MuCell used through Deref.

Example

use mucell::{MuCell, Ref};

let c = MuCell::new(Ok(5));
let b1: Ref<&Result<u32, ()>> = c.borrow();
let b2: Ref<&u32> = Ref::filter_map(b1, |o| o.as_ref().ok()).unwrap();
assert_eq!(*b2, 5)

Trait Implementations

impl<'b, T: Deref + 'b> Deref for Ref<'b, T>
[src]

type Target = T::Target

The resulting type after dereferencing

fn deref(&self) -> &T::Target

The method called to dereference a value