pub trait MessageSizeHint {
// Required method
fn size_hint(&self) -> usize;
}
Expand description
Extension trait that provides size hint functionality for Event Stream messages.
This trait allows callers to get an accurate estimate of the serialized size of a message before serialization, enabling optimal buffer pre-allocation.
Required Methods§
Sourcefn size_hint(&self) -> usize
fn size_hint(&self) -> usize
Returns an estimate of the serialized size of this message in bytes.
This provides a hint for buffer allocation to improve performance when serializing the message. The estimate includes the message prelude, headers, payload, and CRC checksums.
§Examples
use aws_smithy_types::event_stream::{Header, HeaderValue, Message};
use aws_smithy_eventstream::message_size_hint::MessageSizeHint;
use bytes::Bytes;
let message = Message::new(Bytes::from(b"hello world".to_vec()))
.add_header(Header::new("content-type", HeaderValue::String("text/plain".into())));
let size_hint = message.size_hint();
// Use the size hint to pre-allocate a buffer
let mut buffer: Vec<u8> = Vec::with_capacity(size_hint);