Skip to main content

Zc

Struct Zc 

Source
pub struct Zc<O: Owner, D> { /* private fields */ }
Expand description

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

Implementations§

Source§

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

Source

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

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

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

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.

Source

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

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

impl<O, D> Zc<O, D>
where O: Owner,

Source

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

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

pub fn into_owner(self) -> O

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

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

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.

Source

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

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.

Source§

impl<O, T> Zc<O, Option<T>>
where O: Owner,

Source

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

Decomposes self into an option.

Source§

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

Source

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

Decomposes self into a result.

§Errors

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

Trait Implementations§

Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O, T> From<Zc<O, Option<T>>> for Option<Zc<O, T>>
where O: Owner,

Source§

fn from(zc: Zc<O, Option<T>>) -> Self

Converts to this type from the input type.
Source§

impl<O, Ok, Err> From<Zc<O, Result<Ok, Err>>> for Result<Zc<O, Ok>, Zc<O, Err>>
where O: Owner,

Source§

fn from(zc: Zc<O, Result<Ok, Err>>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

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

§

impl<O, D> RefUnwindSafe for Zc<O, D>

§

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,

§

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

§

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

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.