VarZeroCow

Struct 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: ?Sized> Clone for VarZeroCow<'a, V>

Source§

fn clone(&self) -> Self

Returns a duplicate 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, 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,

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, T: 'static + ?Sized> Yokeable<'a> for VarZeroCow<'static, T>

This impl requires enabling the optional yoke Cargo feature of the zerovec crate

Source§

type Output = VarZeroCow<'a, T>

This type MUST be Self with the 'static replaced with 'a, i.e. Self<'a>
Source§

fn transform(&'a self) -> &'a Self::Output

This method must cast self between &'a Self<'static> and &'a Self<'a>. Read more
Source§

fn transform_owned(self) -> Self::Output

This method must cast self between Self<'static> and Self<'a>. Read more
Source§

unsafe fn make(from: Self::Output) -> Self

This method can be used to cast away Self<'a>’s lifetime. Read more
Source§

fn transform_mut<F>(&'a mut self, f: F)
where F: 'static + for<'b> FnOnce(&'b mut Self::Output),

This method must cast self between &'a mut Self<'static> and &'a mut Self<'a>, and pass it to f. 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>

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> Send for VarZeroCow<'a, V>
where V: Sync + Send + ?Sized,

§

impl<'a, V> Sync for VarZeroCow<'a, V>
where V: Sync + ?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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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> ErasedDestructor for T
where T: 'static,

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 24 bytes