pub struct SizeCache { /* private fields */ }Expand description
Transient pre-order cache of nested-message sizes for the two-pass
serialization model (compute_size populates, write_to consumes).
Message::encode and friends construct and discard a SizeCache
internally — most callers never name this type. It appears in the
compute_size / write_to signatures so that manual Message
implementations can thread it through nested-message recursion.
Storage is a small inline [u32; 16] array with a Vec<u32> spill for
the (uncommon) case of more than 16 nested length-delimited sub-messages,
so a fresh cache is allocation-free for typical messages.
Reusable across encodes: call clear between uses to
retain the spill allocation. SizeCache is intentionally not Clone
— it is transient encode state, not data. Reuse via
clear().
Implementations§
Source§impl SizeCache
impl SizeCache
Sourcepub fn reserve(&mut self) -> usize
pub fn reserve(&mut self) -> usize
Reserve a slot for a nested message’s size. Call immediately before
recursing into child.compute_size(cache), then fill the slot with
set after the recursion returns. This reserves the slot
in pre-order even though the size is known in post-order.
Used by generated compute_size implementations.
Sourcepub fn consume_next(&mut self) -> u32
pub fn consume_next(&mut self) -> u32
Consume the next cached size in pre-order.
Used by generated write_to implementations for length-delimited
nested message headers.
§Panics
Panics if the cursor runs past the end of the cache — i.e. if
write_to traversal diverges from compute_size traversal. For
generated code this indicates a codegen bug; for manual Message
implementations it indicates a traversal-order mismatch.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SizeCache
impl RefUnwindSafe for SizeCache
impl Send for SizeCache
impl Sync for SizeCache
impl Unpin for SizeCache
impl UnsafeUnpin for SizeCache
impl UnwindSafe for SizeCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request