pub trait Read: Sized {
type Cfg: Clone + Send + Sync + 'static;
// Required method
fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result<Self, Error>;
}
Expand description
Trait for types that can be read (decoded) from a byte buffer.
Required Associated Types§
Sourcetype Cfg: Clone + Send + Sync + 'static
type Cfg: Clone + Send + Sync + 'static
The Cfg
type parameter allows passing configuration during the read process. This is
crucial for safely decoding untrusted data, for example, by providing size limits for
collections or strings.
Use Cfg = ()
if no configuration is needed for a specific type.
Required Methods§
Sourcefn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result<Self, Error>
fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result<Self, Error>
Reads a value from the buffer using the provided configuration cfg
.
Implementations should consume the exact number of bytes required from buf
to reconstruct
the value.
Returns Error if decoding fails due to invalid data, insufficient bytes in the buffer,
or violation of constraints imposed by the cfg
.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.