enc/util/
impl_encode_to_write_stack_buf.rs

1/// Implements `EncodeToWrite` for a type that also implements `EncodeToSlice` where the encoding
2/// has a small maximum encoded length. The data is buffered on the stack before it is written out.
3#[macro_export]
4macro_rules! impl_encode_to_write_stack_buf {
5    ($target_type:ty, $max_len:expr) => {
6        impl $crate::EncodeToWrite for $target_type {
7            fn encode_to_write<W>(&self, w: &mut W) -> Result<usize, $crate::StreamError>
8            where
9                W: std::io::Write,
10            {
11                use $crate::EncodeToSlice;
12
13                let mut buffer: [u8; $max_len] = [0u8; $max_len];
14                let encoded_len: usize = unsafe { self.encode_to_slice_unchecked(&mut buffer)? };
15                w.write_all(&buffer[..encoded_len])?;
16                Ok(encoded_len)
17            }
18        }
19    };
20}