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>
impl<'a> WirePayload<'a>
Sourcepub const fn borrowed(field: &'a [u8]) -> Self
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.
Sourcepub fn is_owned(&self) -> bool
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.
Sourcepub fn to_str(&self) -> Result<&str, DecodeError>
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.
Sourcepub fn into_bytes(self) -> Bytes
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.