Struct zerovec::VarZeroCow

source ·
pub struct VarZeroCow<'a, V: ?Sized> { /* private fields */ }
Expand description

Copy-on-write type that efficiently represents VarULE types as their bitstream representation.

The primary use case for VarULE types is the ability to store complex variable-length datastructures inside variable-length collections like crate::VarZeroVec.

Underlying this ability is the fact that VarULE types can be efficiently represented as a flat bytestream.

In zero-copy cases, sometimes one wishes to unconditionally use this bytestream representation, for example to save stack size. A struct with five Cow<'a, str>s is not as stack-efficient as a single Cow containing the bytestream representation of, say, Tuple5VarULE<str, str, str, str, str>.

This type helps in this case: It is logically a Cow<'a, V>, with some optimizations, that is guaranteed to serialize as a byte stream in machine-readable scenarios.

During human-readable serialization, it will fall back to the serde impls on V, which ought to have a human-readable variant.

Implementations§

source§

impl<'a, V: VarULE + ?Sized> VarZeroCow<'a, V>

source

pub fn parse_bytes(bytes: &'a [u8]) -> Result<Self, UleError>

Construct from a slice. Errors if the slice doesn’t represent a valid V

source

pub fn parse_owned_bytes(bytes: Box<[u8]>) -> Result<Self, UleError>

Construct from an owned slice. Errors if the slice doesn’t represent a valid V

source

pub const unsafe fn from_bytes_unchecked(bytes: &'a [u8]) -> Self

Construct from a slice that is known to represent a valid V

§Safety

bytes must be a valid V, i.e. it must successfully pass through V::parse_bytes() or V::validate_bytes().

source

pub fn from_encodeable<E: EncodeAsVarULE<V>>(encodeable: &E) -> Self

Construct this from an EncodeAsVarULE version of the contained type

Will always construct an owned version

source

pub fn new_borrowed(val: &'a V) -> Self

Construct a new borrowed version of this

source

pub fn new_owned(val: Box<V>) -> Self

Construct a new borrowed version of this

source§

impl<'a, V: ?Sized> VarZeroCow<'a, V>

source

pub fn is_owned(&self) -> bool

Whether or not this is owned

source

pub fn as_bytes(&self) -> &[u8]

Get the byte representation of this type

Is also always a valid V and can be passed to V::from_bytes_unchecked()

Trait Implementations§

source§

impl<'a, V: VarULE + ?Sized> Bake for VarZeroCow<'a, V>

source§

fn bake(&self, env: &CrateEnv) -> TokenStream

Returns a TokenStream that would evaluate to self. Read more
source§

impl<'a, V: VarULE + ?Sized> BakeSize for VarZeroCow<'a, V>

source§

fn borrows_size(&self) -> usize

Returns the size
source§

impl<'a, V: ?Sized> Clone for VarZeroCow<'a, V>

source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, V: VarULE + ?Sized + Debug> Debug for VarZeroCow<'a, V>

source§

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

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

impl<'a, V: VarULE + ?Sized> Deref for VarZeroCow<'a, V>

source§

type Target = V

The resulting type after dereferencing.
source§

fn deref(&self) -> &V

Dereferences the value.
source§

impl<'a, 'de: 'a, V: VarULE + ?Sized> Deserialize<'de> for VarZeroCow<'a, V>
where Box<V>: Deserialize<'de>,

source§

fn deserialize<Des>(deserializer: Des) -> Result<Self, Des::Error>
where Des: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<'a, V: ?Sized> Drop for VarZeroCow<'a, V>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<'a, V: VarULE + ?Sized> EncodeAsVarULE<V> for VarZeroCow<'a, V>

source§

fn encode_var_ule_as_slices<R>(&self, _: impl FnOnce(&[&[u8]]) -> R) -> R

Calls cb with a piecewise list of byte slices that when concatenated produce the memory pattern of the corresponding instance of T. Read more
source§

fn encode_var_ule_len(&self) -> usize

Return the length, in bytes, of the corresponding VarULE type
source§

fn encode_var_ule_write(&self, dst: &mut [u8])

Write the corresponding VarULE type to the dst buffer. dst should be the size of Self::encode_var_ule_len()
source§

impl<'a, V: VarULE + ?Sized> From<&'a V> for VarZeroCow<'a, V>

source§

fn from(other: &'a V) -> Self

Converts to this type from the input type.
source§

impl<'a, V: VarULE + ?Sized> From<Box<V>> for VarZeroCow<'a, V>

source§

fn from(other: Box<V>) -> Self

Converts to this type from the input type.
source§

impl<'a, V: VarULE + ?Sized + Ord> Ord for VarZeroCow<'a, V>

source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<'a, V: VarULE + ?Sized + PartialEq> PartialEq for VarZeroCow<'a, V>

source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a, V: VarULE + ?Sized + PartialOrd> PartialOrd for VarZeroCow<'a, V>

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<'a, V: VarULE + ?Sized + Serialize> Serialize for VarZeroCow<'a, V>

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'a, V: VarULE + ?Sized> ZeroFrom<'a, V> for VarZeroCow<'a, V>

source§

fn zero_from(other: &'a V) -> Self

Clone the other C into a struct that may retain references into C.
source§

impl<'a, 'b, V: VarULE + ?Sized> ZeroFrom<'a, VarZeroCow<'b, V>> for VarZeroCow<'a, V>

source§

fn zero_from(other: &'a VarZeroCow<'b, V>) -> Self

Clone the other C into a struct that may retain references into C.
source§

impl<'a, V: VarULE + ?Sized + Eq> Eq for VarZeroCow<'a, V>

source§

impl<'a, V: ?Sized> Send for VarZeroCow<'a, V>

source§

impl<'a, V: ?Sized> Sync for VarZeroCow<'a, V>

Auto Trait Implementations§

§

impl<'a, V> Freeze for VarZeroCow<'a, V>
where V: ?Sized,

§

impl<'a, V> RefUnwindSafe for VarZeroCow<'a, V>
where V: RefUnwindSafe + ?Sized,

§

impl<'a, V> Unpin for VarZeroCow<'a, V>
where V: ?Sized,

§

impl<'a, V> UnwindSafe for VarZeroCow<'a, V>

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<T> ErasedDestructor for T
where T: 'static,