Struct zc::Zc[][src]

pub struct Zc<O: Owner, D> { /* fields omitted */ }

Zero-copy structure consisting of an Owner and a Dependant.

Implementations

impl<O, D> Zc<O, D> where
    O: Owner,
    D: Dependant<'static>, 
[src]

pub fn new<C>(owner: O, constructor: C) -> Self where
    C: for<'o> Construct<'o, <O::Storage as Deref>::Target, Dependant = D>, 
[src]

Construct a new zero-copied structure given an Owner and a function for constructing the Dependant.

Example

use zc::{Zc, Dependant};

#[derive(Dependant)]
struct MyStruct<'a>(&'a [u8]);

impl<'a> From<&'a [u8]> for MyStruct<'a> {
    fn from(bytes: &'a [u8]) -> Self {
        Self(&bytes[1..])
    }
}

let owner = vec![1, 2, 3];
let _ = zc::from!(owner, MyStruct, [u8]);

pub fn try_new<C, E>(owner: O, constructor: C) -> Result<Self, (E, O)> where
    E: 'static,
    C: for<'o> TryConstruct<'o, <O::Storage as Deref>::Target, Error = E, Dependant = D>, 
[src]

Try construct a new zero-copied structure given an Owner and a function for constructing the Dependant.

Example

use zc::{Zc, Dependant};
use core::convert::TryFrom;

#[derive(Dependant)]
struct MyStruct<'a>(&'a [u8]);

impl<'a> TryFrom<&'a [u8]> for MyStruct<'a> {
    type Error = ();

    fn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error> {
        Ok(Self(&bytes[1..]))
    }
}

let owner = vec![1, 2, 3];
let _ = zc::try_from!(owner, MyStruct, [u8]);

Errors

Returns E if the constructor failed.

pub fn get<'a, T>(&'a self) -> &T where
    T: Dependant<'a, Static = D>, 
[src]

Return a reference to the Dependant.

The dependant type T must be supplied (eg. Self::dependant::<MyStruct>(&self)).

Example

use zc::{Zc, Dependant};

#[derive(Debug, PartialEq, Dependant)]
struct MyStruct<'a>(&'a [u8]);

impl<'a> From<&'a [u8]> for MyStruct<'a> {
    fn from(bytes: &'a [u8]) -> Self {
        Self(&bytes[1..])
    }
}

let owner = vec![1, 2, 3];
let data = zc::from!(owner, MyStruct, [u8]);

assert_eq!(
    data.get::<MyStruct>(),
    &MyStruct(&[2, 3])
);

impl<O, D> Zc<O, D> where
    O: Owner
[src]

pub fn as_owned(&self) -> &<O::Storage as Deref>::Target[src]

Return a reference to the data Owner provides.

Example

use zc::{Zc, Dependant};

#[derive(Debug, PartialEq, Dependant)]
struct MyStruct<'a>(&'a [u8]);

impl<'a> From<&'a [u8]> for MyStruct<'a> {
    fn from(bytes: &'a [u8]) -> Self {
        Self(&bytes[1..])
    }
}

let owner = vec![1, 2, 3];
let data = zc::from!(owner, MyStruct, [u8]);

assert_eq!(data.as_owned(), &[1, 2, 3]);

pub fn into_owner(self) -> O[src]

Consumes self into the Owner.

Example

use zc::{Zc, Dependant};

#[derive(Debug, PartialEq, Dependant)]
struct MyStruct<'a>(&'a [u8]);

impl<'a> From<&'a [u8]> for MyStruct<'a> {
    fn from(bytes: &'a [u8]) -> Self {
        Self(&bytes[1..])
    }
}

let owner = vec![1, 2, 3];
let data = zc::from!(owner, MyStruct, [u8]);

assert_eq!(data.into_owner(), vec![1, 2, 3]);

pub unsafe fn map_unchecked<F, U>(self, f: F) -> Zc<O, U> where
    F: FnOnce(D) -> U, 
[src]

Map the stored Dependant to another.

Safety

The Dependant passed to the function has its lifetime erased to 'static and must be handled appropriately. Nothing within the Dependant passed can be referenced from outside of the closure.

pub unsafe fn try_map_unchecked<F, U, E>(self, f: F) -> Result<Zc<O, U>, E> where
    F: FnOnce(D) -> Result<U, E>, 
[src]

Try to map the stored Dependant to another.

Errors

Returns any error the provided function returns.

Safety

The Dependant passed to the function has its lifetime erased to 'static and must be handled appropriately. Nothing within the Dependant passed can be referenced from outside of the closure, this includes the error returned.

impl<O, T> Zc<O, Option<T>> where
    O: Owner
[src]

pub fn into_option(self) -> Option<Zc<O, T>>[src]

Decomposes self into an option.

impl<O, Ok, Err> Zc<O, Result<Ok, Err>> where
    O: Owner
[src]

pub fn into_result(self) -> Result<Zc<O, Ok>, Zc<O, Err>>[src]

Decomposes self into a result.

Errors

Returns Zc<O, Err> if the inner dependant is Result::Err.

Trait Implementations

impl<O, D> Debug for Zc<O, D> where
    O: Owner,
    O::Storage: Debug,
    D: Debug
[src]

impl<O, D> Display for Zc<O, D> where
    O: Owner,
    D: Display
[src]

Auto Trait Implementations

impl<O, D> RefUnwindSafe for Zc<O, D> where
    D: RefUnwindSafe,
    <O as Owner>::Storage: RefUnwindSafe
[src]

impl<O, D> Send for Zc<O, D> where
    D: Send,
    <O as Owner>::Storage: Send
[src]

impl<O, D> Sync for Zc<O, D> where
    D: Sync,
    <O as Owner>::Storage: Sync
[src]

impl<O, D> Unpin for Zc<O, D> where
    D: Unpin,
    <O as Owner>::Storage: Unpin
[src]

impl<O, D> UnwindSafe for Zc<O, D> where
    D: UnwindSafe,
    <O as Owner>::Storage: UnwindSafe
[src]

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<T> for T[src]

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

impl<T> ToString for T where
    T: Display + ?Sized
[src]

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.