@@ -22,6 +22,14 @@ pub trait FixedSize {
2222pub trait EncodeSize {
2323 /// Returns the encoded size of this value (in bytes).
2424 fn encode_size ( & self ) -> usize ;
25+
26+ /// Returns the encoded size excluding bytes passed to [`BufsMut::push`]
27+ /// during [`Write::write_bufs`]. Used to size the working buffer for inline
28+ /// writes. Override alongside [`Write::write_bufs`] for types where large
29+ /// [`Bytes`] fields go via push; failing to do so will over-allocate.
30+ fn encode_inline_size ( & self ) -> usize {
31+ self . encode_size ( )
32+ }
2533}
2634
2735// Automatically implement `EncodeSize` for types that are `FixedSize`.
@@ -37,6 +45,13 @@ pub trait Write {
3745 ///
3846 /// Implementations should panic if the buffer doesn't have enough capacity.
3947 fn write ( & self , buf : & mut impl BufMut ) ;
48+
49+ /// Writes to a [`BufsMut`], allowing existing [`Bytes`] chunks to be
50+ /// appended via [`BufsMut::push`] instead of written inline. Must encode
51+ /// to the same format as [`Write::write`]. Defaults to [`Write::write`].
52+ fn write_bufs ( & self , buf : & mut impl BufsMut ) {
53+ self . write ( buf) ;
54+ }
4055}
4156
4257/// Trait for types that can be read (decoded) from a byte buffer.
@@ -194,6 +209,13 @@ pub trait CodecFixedShared: CodecFixed<Cfg = ()> + Send + Sync {}
194209// Automatically implement `CodecFixedShared` for types that meet all bounds.
195210impl < T : CodecFixed < Cfg = ( ) > + Send + Sync > CodecFixedShared for T { }
196211
212+ /// A [`BufMut`] that can also append pre-existing [`Bytes`] chunks.
213+ pub trait BufsMut : BufMut {
214+ /// Appends a [`Bytes`] chunk instead of writing its contents inline into
215+ /// the destination buffer.
216+ fn push ( & mut self , bytes : impl Into < Bytes > ) ;
217+ }
218+
197219#[ cfg( test) ]
198220mod tests {
199221 use super :: * ;
0 commit comments