Skip to main content

Delimited

Struct Delimited 

Source
pub struct Delimited<'d> { /* private fields */ }
Expand description

Byte-delimited framer.

§Example

use wire_codec::WriteBuf;
use wire_codec::framing::{Delimited, Framer};

let framer = Delimited::bounded(b"\n").unwrap();

let mut out = [0u8; 32];
let mut buf = WriteBuf::new(&mut out);
framer.write_frame(b"line one", &mut buf).unwrap();
framer.write_frame(b"line two", &mut buf).unwrap();
let n = buf.position();
assert_eq!(&out[..n], b"line one\nline two\n");

let first = framer.next_frame(&out[..n]).unwrap().unwrap();
assert_eq!(first.payload(), b"line one");
assert_eq!(first.consumed(), 9);

Implementations§

Source§

impl<'d> Delimited<'d>

Source

pub const DEFAULT_MAX_PAYLOAD: usize

Payload limit applied by Delimited::bounded: 64 KiB.

Generous for line-oriented text protocols (SMTP caps a line at 1000 bytes, HTTP servers commonly cap a header line at 8 KiB) while bounding the memory a single peer can pin.

Source

pub const fn new(delimiter: &'d [u8]) -> Result<Self>

Build a framer using delimiter to separate frames, with no payload size limit.

Without a limit the framer keeps asking for more input until it sees the delimiter, however long that takes. Prefer Delimited::bounded for input from untrusted peers.

§Errors

Returns Error::EmptyDelimiter if delimiter is empty. An empty delimiter cannot uniquely separate frames.

§Example
use wire_codec::framing::Delimited;

let framer = Delimited::new(b"\r\n").unwrap();
assert_eq!(framer.max_payload(), usize::MAX);
Source

pub const fn bounded(delimiter: &'d [u8]) -> Result<Self>

Build a framer using delimiter, capped at Delimited::DEFAULT_MAX_PAYLOAD payload bytes per frame.

This is the recommended constructor for input from untrusted peers. Chain Delimited::with_max_payload to pick a different cap.

§Errors

Returns Error::EmptyDelimiter if delimiter is empty.

§Example
use wire_codec::framing::Delimited;

let framer = Delimited::bounded(b"\n").unwrap();
assert_eq!(framer.max_payload(), Delimited::DEFAULT_MAX_PAYLOAD);
Source

pub const fn with_max_payload(self, max: usize) -> Self

Set an upper bound on payload size. Frames whose payload would exceed this size cause Error::FrameTooLarge.

The delimiter search never looks further than max + delimiter.len() bytes into the input, so the limit also bounds the work per call.

§Example
use wire_codec::framing::{Delimited, Framer};
use wire_codec::Error;

let framer = Delimited::new(b"\n").unwrap().with_max_payload(4);
assert!(framer.next_frame(b"1234\n").unwrap().is_some());
assert!(matches!(
    framer.next_frame(b"123456"),
    Err(Error::FrameTooLarge { limit: 4, .. })
));
Source

pub const fn delimiter(&self) -> &'d [u8] ⓘ

Configured delimiter sequence.

Source

pub const fn max_payload(&self) -> usize

Configured maximum payload size.

Trait Implementations§

Source§

impl<'d> Clone for Delimited<'d>

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<'d> Copy for Delimited<'d>

Source§

impl<'d> Debug for Delimited<'d>

Source§

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

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

impl<'d> Eq for Delimited<'d>

Source§

impl Framer for Delimited<'_>

Source§

fn next_frame<'a>(&self, input: &'a [u8]) -> Result<Option<Frame<'a>>>

Extract the bytes up to the first delimiter.

§Errors

Returns Error::FrameTooLarge once more than max_payload + delimiter.len() bytes are buffered without a delimiter starting at or before offset max_payload. The len field is then a lower bound (max_payload + 1): the true length is unknown because the frame has not ended yet.

Source§

fn write_frame(&self, payload: &[u8], out: &mut WriteBuf<'_>) -> Result<()>

Serialize payload followed by the delimiter.

§Errors
  • Error::FrameTooLarge if payload exceeds the configured limit.
  • Error::DelimiterInPayload if the receiver would not read the payload back as a single frame: either payload contains the delimiter, or (for a self-overlapping delimiter such as b"aa") the payload ends in a way that makes a delimiter appear earlier than intended (b"a" followed by b"aa" reads back as an empty frame).
  • Error::BufferFull if out cannot hold the payload and the delimiter.

Nothing is written on error.

Source§

fn next_frame_from<'a>( &self, input: &'a [u8], scanned: &mut usize, ) -> Result<Option<Frame<'a>>>

Resumable delimiter search; see Framer::next_frame_from.

On Ok(None), scanned is set to the number of leading bytes that can no longer start a delimiter, so the next call only examines new input (plus delimiter.len() - 1 bytes of overlap for a delimiter split across reads). Values larger than the input are clamped.

§Errors

Same contract as Framer::next_frame for this type.

Source§

fn max_frame_len(&self, payload_len: usize) -> Option<usize>

Upper bound on the number of bytes Framer::write_frame writes for a payload of payload_len bytes. Read more
Source§

impl<'d> Hash for Delimited<'d>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'d> PartialEq for Delimited<'d>

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<'d> StructuralPartialEq for Delimited<'d>

Auto Trait Implementations§

§

impl<'d> Freeze for Delimited<'d>

§

impl<'d> RefUnwindSafe for Delimited<'d>

§

impl<'d> Send for Delimited<'d>

§

impl<'d> Sync for Delimited<'d>

§

impl<'d> Unpin for Delimited<'d>

§

impl<'d> UnsafeUnpin for Delimited<'d>

§

impl<'d> UnwindSafe for Delimited<'d>

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.