[][src]Struct zc::Zc

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

A zero-copy structure consisting of an Owner and a Dependant.

Implementations

impl<O, D> Zc<O, D> where
    O: Owner,
    D: Dependant<'static> + '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]

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]);

pub fn dependant<'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.dependant::<MyStruct>(),
    &MyStruct(&[2, 3])
);

pub fn 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.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]);

Trait Implementations

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

Auto Trait Implementations

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

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

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

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, 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.