//! Extension traits for ergonomic operations on encoding and decoding. //! //! These traits provide convenience methods (like `read()`, `decode()`, `read_range()`, and //! `decode_range()`) that simplify common use cases of the core [Read] and [Decode] traits, //! particularly when default configurations (`()`) or [RangeCfg] are involved. use crate::{Decode, Error, RangeCfg, Read}; use bytes::Buf; /// Extension trait providing ergonomic read method for types requiring no configuration /// (i.e. `Cfg = ()`). /// /// Import this trait to use the `.read(buf)` method as a shorthand for `.read_cfg(buf, ())`. pub trait ReadExt: Read { /// Reads a value using the default `()` config. fn read(buf: &mut impl Buf) -> Result { Self::read_cfg(buf, &()) } } // Automatically implement `ReadExt` for types that implement `Read` with no config. impl> ReadExt for T {} /// Extension trait providing ergonomic decode method for types requiring no specific configuration. /// /// Import this trait to use the `.decode(buf)` method as a shorthand for `.decode_cfg(buf, ())`. pub trait DecodeExt: Decode { /// Decodes a value using the default `()` config. fn decode(buf: impl Buf) -> Result { Self::decode_cfg(buf, &X::default()) } } // Automatically implement `DecodeExt` for types that implement `Decode` with no config. impl> DecodeExt for T {} /// Extension trait for reading types whose config is `(RangeCfg, X)` where `X` is [Default]. /// /// Useful for reading collections like [`Vec`] where `T` implements [Read] with no specific /// configuration. Import this trait to use the `.read_range()` method. pub trait ReadRangeExt: Read { /// Reads a value using only a range configuration. /// /// The inner configuration type `X` must be [Default] and `X::default()` is used for it. fn read_range(buf: &mut impl Buf, range: impl Into) -> Result { Self::read_cfg(buf, &(range.into(), X::default())) } } // Automatically implement `ReadRangeExt` for types that implement `Read` with config // `(RangeCfg, X)`, where `X` is `Default`. impl> ReadRangeExt for U {} /// Extension trait for reading types whose config is `(RangeCfg, X)` where `X` is [Default], /// ensuring the buffer is consumed. /// /// Useful for decoding collections like [`Vec`] where `T` implements [Read] with no specific /// configuration. Import this trait to use the `.decode_range()` method. pub trait DecodeRangeExt: Decode { /// Decodes a value using only a range configuration. /// /// The inner configuration type `X` must be [Default] and `X::default()` is used for it. fn decode_range(buf: impl Buf, range: impl Into) -> Result { Self::decode_cfg(buf, &(range.into(), X::default())) } } // Automatically implement `DecodeRangeExt` for types that implement `Decode` with config // `(RangeCfg, X)`, where `X` has a default implementation. impl> DecodeRangeExt for U {}