Skip to main content

Rope

Struct Rope 

Source
pub struct Rope { /* private fields */ }
Expand description

A segmented encode sink: an ordered sequence of Bytes segments.

Small writes (tags, varints, scalar fields, short strings) accumulate in a tail buffer; large owned segments arriving via put_shared — and large borrowed slices that provably lie inside the optional backing buffer — are captured by reference count instead of copied. Concatenating the segments reproduces exactly the bytes a contiguous sink would have received.

ⓘ
let mut rope = Rope::new();
message.encode(&mut rope);
for segment in rope.into_segments() {
    body.send_data(segment); // refcount handles, no payload copy
}

Implementations§

Source§

impl Rope

Source

pub fn new() -> Self

Create a rope with DEFAULT_MIN_SEGMENT.

Source

pub fn with_min_segment(min_segment: usize) -> Self

Create a rope with a custom minimum segment size.

min_segment = usize::MAX never segments (every write is copied into one contiguous tail — useful for differential testing); min_segment = 0 is clamped to 1 so empty payloads never produce empty segments.

Source

pub fn with_backing(self, backing: Bytes) -> Self

Attach the buffer that borrowed slices were decoded from.

With a backing buffer attached, put_slice captures any large slice that lies inside it via Bytes::slice_ref — zero-copy — instead of copying. This is the view-encoding hook: a view’s &[u8] fields borrow from the buffer the view was decoded from, so re-encoding a view through a rope backed by that buffer never copies the large fields.

Pass exactly the buffer the view was decoded from. Slices outside the backing buffer (modified fields, other sources, a mismatched buffer) are copied as usual — the output stays correct, but the zero-copy capture silently does not engage. The containment check is two pointer compares.

Source

pub fn len(&self) -> usize

Total byte length across all segments and the tail.

Source

pub fn is_empty(&self) -> bool

Whether the rope contains no bytes.

Source

pub fn segment_count(&self) -> usize

Number of segments into_segments will yield (including the pending tail, if non-empty).

Source

pub fn into_segments(self) -> Vec<Bytes>

Finish the rope, yielding its segments in output order.

Source

pub fn to_contiguous_bytes(&self) -> Bytes

Copy the rope out into one contiguous Bytes.

For consumers that need contiguous output after all — this performs the copies the rope avoided, so prefer into_segments wherever the consumer can take a segment sequence. (Named distinctly from Buf::copy_to_bytes, which consumes a length-bounded prefix; this is a non-consuming full copy.)

Trait Implementations§

Source§

impl Debug for Rope

Source§

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

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

impl Default for Rope

Source§

fn default() -> Self

Equivalent to Rope::new. (Not derived: the derive would default min_segment to 0, bypassing the clamp every constructor applies.)

Source§

impl EncodeSink for Rope

Source§

const IS_SEGMENTED: bool = true

Whether this sink can take ownership of Bytes segments without copying (put_shared is more than a copy). Read more
Source§

fn put_u8(&mut self, value: u8)

Append a single byte.
Source§

fn put_slice(&mut self, src: &[u8])

Append a borrowed slice.
Source§

fn put_u32_le(&mut self, value: u32)

Append a u32 in little-endian byte order (protobuf fixed32).
Source§

fn put_u64_le(&mut self, value: u64)

Append a u64 in little-endian byte order (protobuf fixed64).
Source§

fn put_shared(&mut self, bytes: Bytes)

Append an owned segment. Read more
Source§

fn put_i32_le(&mut self, value: i32)

Append an i32 in little-endian byte order (protobuf sfixed32).
Source§

fn put_i64_le(&mut self, value: i64)

Append an i64 in little-endian byte order (protobuf sfixed64).
Source§

fn put_f32_le(&mut self, value: f32)

Append an f32 in little-endian byte order (protobuf float).
Source§

fn put_f64_le(&mut self, value: f64)

Append an f64 in little-endian byte order (protobuf double).
Source§

impl EncodeSink for &mut Rope

Source§

const IS_SEGMENTED: bool = true

Whether this sink can take ownership of Bytes segments without copying (put_shared is more than a copy). Read more
Source§

fn put_u8(&mut self, value: u8)

Append a single byte.
Source§

fn put_slice(&mut self, src: &[u8])

Append a borrowed slice.
Source§

fn put_u32_le(&mut self, value: u32)

Append a u32 in little-endian byte order (protobuf fixed32).
Source§

fn put_u64_le(&mut self, value: u64)

Append a u64 in little-endian byte order (protobuf fixed64).
Source§

fn put_shared(&mut self, bytes: Bytes)

Append an owned segment. Read more
Source§

fn put_i32_le(&mut self, value: i32)

Append an i32 in little-endian byte order (protobuf sfixed32).
Source§

fn put_i64_le(&mut self, value: i64)

Append an i64 in little-endian byte order (protobuf sfixed64).
Source§

fn put_f32_le(&mut self, value: f32)

Append an f32 in little-endian byte order (protobuf float).
Source§

fn put_f64_le(&mut self, value: f64)

Append an f64 in little-endian byte order (protobuf double).
Source§

impl From<Rope> for RopeBuf

Source§

fn from(rope: Rope) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !Freeze for Rope

§

impl Forget for Rope

§

impl Move for Rope

§

impl RefUnwindSafe for Rope

§

impl Send for Rope

§

impl Sync for Rope

§

impl Unpin for Rope

§

impl UnsafeUnpin for Rope

§

impl UnwindSafe for Rope

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> 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, 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.