pub struct Zc<O: Owner, D> { /* private fields */ }Implementations§
Source§impl<O, D> Zc<O, D>
impl<O, D> Zc<O, D>
Sourcepub fn new<C>(owner: O, constructor: C) -> Self
pub fn new<C>(owner: O, constructor: C) -> Self
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]);Sourcepub fn try_new<C, E>(owner: O, constructor: C) -> Result<Self, (E, O)>
pub fn try_new<C, E>(owner: O, constructor: C) -> Result<Self, (E, O)>
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.
Sourcepub fn get<'a, T>(&'a self) -> &Twhere
T: Dependant<'a, Static = D>,
pub fn get<'a, T>(&'a self) -> &Twhere
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,
impl<O, D> Zc<O, D>where
O: Owner,
Sourcepub fn as_owned(&self) -> &<O::Storage as Deref>::Target
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]);Sourcepub fn into_owner(self) -> O
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]);Sourcepub unsafe fn map_unchecked<F, U>(self, f: F) -> Zc<O, U>where
F: FnOnce(D) -> U,
pub unsafe fn map_unchecked<F, U>(self, f: F) -> Zc<O, U>where
F: FnOnce(D) -> U,
Sourcepub unsafe fn try_map_unchecked<F, U, E>(self, f: F) -> Result<Zc<O, U>, E>
pub unsafe fn try_map_unchecked<F, U, E>(self, f: F) -> Result<Zc<O, 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.
Trait Implementations§
Auto Trait Implementations§
impl<O, D> Freeze for Zc<O, D>
impl<O, D> RefUnwindSafe for Zc<O, D>
impl<O, D> Send for Zc<O, D>
impl<O, D> Sync for Zc<O, D>
impl<O, D> Unpin for Zc<O, D>
impl<O, D> UnsafeUnpin for Zc<O, D>
impl<O, D> UnwindSafe for Zc<O, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more