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
impl Rope
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a rope with DEFAULT_MIN_SEGMENT.
Sourcepub fn with_min_segment(min_segment: usize) -> Self
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.
Sourcepub fn with_backing(self, backing: Bytes) -> Self
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.
Sourcepub fn segment_count(&self) -> usize
pub fn segment_count(&self) -> usize
Number of segments into_segments will yield
(including the pending tail, if non-empty).
Sourcepub fn into_segments(self) -> Vec<Bytes>
pub fn into_segments(self) -> Vec<Bytes>
Finish the rope, yielding its segments in output order.
Sourcepub fn to_contiguous_bytes(&self) -> Bytes
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 EncodeSink for Rope
impl EncodeSink for Rope
Source§const IS_SEGMENTED: bool = true
const IS_SEGMENTED: bool = true
Bytes segments without
copying (put_shared is more than a copy). Read moreSource§fn put_u32_le(&mut self, value: u32)
fn put_u32_le(&mut self, value: u32)
u32 in little-endian byte order (protobuf fixed32).Source§fn put_u64_le(&mut self, value: u64)
fn put_u64_le(&mut self, value: u64)
u64 in little-endian byte order (protobuf fixed64).Source§fn put_i32_le(&mut self, value: i32)
fn put_i32_le(&mut self, value: i32)
i32 in little-endian byte order (protobuf sfixed32).Source§fn put_i64_le(&mut self, value: i64)
fn put_i64_le(&mut self, value: i64)
i64 in little-endian byte order (protobuf sfixed64).Source§fn put_f32_le(&mut self, value: f32)
fn put_f32_le(&mut self, value: f32)
f32 in little-endian byte order (protobuf float).Source§fn put_f64_le(&mut self, value: f64)
fn put_f64_le(&mut self, value: f64)
f64 in little-endian byte order (protobuf double).Source§impl EncodeSink for &mut Rope
impl EncodeSink for &mut Rope
Source§const IS_SEGMENTED: bool = true
const IS_SEGMENTED: bool = true
Bytes segments without
copying (put_shared is more than a copy). Read moreSource§fn put_u32_le(&mut self, value: u32)
fn put_u32_le(&mut self, value: u32)
u32 in little-endian byte order (protobuf fixed32).Source§fn put_u64_le(&mut self, value: u64)
fn put_u64_le(&mut self, value: u64)
u64 in little-endian byte order (protobuf fixed64).Source§fn put_i32_le(&mut self, value: i32)
fn put_i32_le(&mut self, value: i32)
i32 in little-endian byte order (protobuf sfixed32).Source§fn put_i64_le(&mut self, value: i64)
fn put_i64_le(&mut self, value: i64)
i64 in little-endian byte order (protobuf sfixed64).Source§fn put_f32_le(&mut self, value: f32)
fn put_f32_le(&mut self, value: f32)
f32 in little-endian byte order (protobuf float).Source§fn put_f64_le(&mut self, value: f64)
fn put_f64_le(&mut self, value: f64)
f64 in little-endian byte order (protobuf double).