Skip to main content

WirePayload

Struct WirePayload 

Source
pub struct WirePayload<'a>(/* private fields */);
Expand description

The raw, length-delimited payload of a string or bytes field, handed to ProtoString::from_wire / ProtoBytes::from_wire so a representation can construct itself directly from the wire — validating (or skipping validation) and choosing borrow-vs-own on its own terms.

By default the decoder hands over a borrowed payload when the field’s bytes are contiguous in the current input chunk (the common case for slice- and Bytes-backed sources) and an owned Bytes otherwise (e.g. a field straddling a Chain boundary). The exception is a ProtoBytes type that sets PREFERS_OWNED_PAYLOAD: the default ProtoBytes::decode_length_delimited hands it an owned payload for every bytes field. A representation validates-and-borrows with to_str, reads the raw bytes with as_slice (always zero-copy), or takes ownership with into_bytes (zero-copy only for an owned payload).

Opaque so that a borrowed payload can carry the surrounding wire-buffer tail for the slack-aware UTF-8 validator without exposing it to consumers; use the accessors above and the borrowed / owned constructors.

Implementations§

Source§

impl<'a> WirePayload<'a>

Source

pub const fn borrowed(field: &'a [u8]) -> Self

A payload borrowing exactly field (no surrounding wire buffer).

Tests and hand-built payloads use this; the decoder uses an internal constructor that also carries the surrounding tail so to_str can take the slack-aware fast path.

Source

pub const fn owned(bytes: Bytes) -> Self

A payload owning bytes.

Source

pub fn as_slice(&self) -> &[u8] ⓘ

Borrow the field’s bytes (zero-copy in both variants).

Source

pub fn len(&self) -> usize

Field length in bytes.

Source

pub fn is_empty(&self) -> bool

Whether the field is empty.

Source

pub fn is_owned(&self) -> bool

Whether this payload owns its bytes (and so into_bytes is zero-copy). A from_wire impl that wants the Bytes only when it’s free can branch on this and fall back to as_slice otherwise.

Source

pub fn to_str(&self) -> Result<&str, DecodeError>

Borrow the payload as a &str if it is valid UTF-8.

Convenience for ProtoString::from_wire implementations; uses buffa’s UTF-8 validator (so it picks up the fast-utf8 feature when enabled) and returns the same DecodeError::InvalidUtf8 the built-in String representation would. For a borrowed payload that carries surrounding wire-buffer tail (the decoder’s common case) this reaches the slack-aware fast path; the borrowed/owned constructors have no tail, so they take the plain validator.

§Errors

Returns DecodeError::InvalidUtf8 if the bytes are not valid UTF-8.

Source

pub fn into_bytes(self) -> Bytes

Take ownership of the field’s bytes as Bytes.

Zero-copy for an owned payload; a borrowed payload is copied. A ProtoBytes type that keeps this Bytes sets PREFERS_OWNED_PAYLOAD, so that the decoder hands it an owned payload. With the const at false, every contiguous field is copied here.

Trait Implementations§

Source§

impl<'a> Clone for WirePayload<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for WirePayload<'_>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> !Freeze for WirePayload<'a>

§

impl<'a> Forget for WirePayload<'a>

§

impl<'a> Move for WirePayload<'a>

§

impl<'a> RefUnwindSafe for WirePayload<'a>

§

impl<'a> Send for WirePayload<'a>

§

impl<'a> Sync for WirePayload<'a>

§

impl<'a> Unpin for WirePayload<'a>

§

impl<'a> UnsafeUnpin for WirePayload<'a>

§

impl<'a> UnwindSafe for WirePayload<'a>

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<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 = !

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

fn try_from(value: U) -> Result<T, !>

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.