pub trait LengthPrefixed: Protocol {
// Required methods
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>(
stream: &'a mut R,
max_len: u64,
) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>;
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>(
&'a self,
sink: &'a mut W,
max_len: u64,
) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>;
fn read_length_prefixed_sync(
stream: &mut impl Read,
max_len: u64,
) -> Result<Self, ReadError>;
fn write_length_prefixed_sync(
&self,
sink: &mut impl Write,
max_len: u64,
) -> Result<(), WriteError>;
}
Expand description
This trait allows restricting the acceptable length of collection types.
By default, types from this crate implementing this trait represent their length as a u64
. If the maximum length is limited (e.g. using the #[async_proto(max_len = ...)]
attribute when deriving Protocol
), the length may be represented using a smaller integer type.
Required Methods§
Sourcefn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>(
stream: &'a mut R,
max_len: u64,
) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
Reads a value of this type from an async stream, limiting the length to the given value.
Sourcefn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>(
&'a self,
sink: &'a mut W,
max_len: u64,
) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
Writes a value of this type to an async sink, limiting the length to the given value.
Sourcefn read_length_prefixed_sync(
stream: &mut impl Read,
max_len: u64,
) -> Result<Self, ReadError>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
Reads a value of this type from a sync stream, limiting the length to the given value.
Sourcefn write_length_prefixed_sync(
&self,
sink: &mut impl Write,
max_len: u64,
) -> Result<(), WriteError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Writes a value of this type to a sync sink, limiting the length to the given value.
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.
Implementations on Foreign Types§
Source§impl LengthPrefixed for String
A string is encoded in UTF-8 and prefixed with the length in bytes.
impl LengthPrefixed for String
A string is encoded in UTF-8 and prefixed with the length in bytes.
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl LengthPrefixed for BitVec<u8, Lsb0>
Available on crate feature bitvec
only.A BitVec
is prefixed with the length in bits.
impl LengthPrefixed for BitVec<u8, Lsb0>
bitvec
only.A BitVec
is prefixed with the length in bits.
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl LengthPrefixed for Bytes
Available on crate feature bytes
only.
impl LengthPrefixed for Bytes
bytes
only.fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl LengthPrefixed for Map<String, Value>
Available on crate feature serde_json
only.
impl LengthPrefixed for Map<String, Value>
serde_json
only.fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<'cow, B: ToOwned + Sync + ?Sized> LengthPrefixed for Cow<'cow, B>
A cow is represented like its owned variant.
impl<'cow, B: ToOwned + Sync + ?Sized> LengthPrefixed for Cow<'cow, B>
A cow is represented like its owned variant.
Note that due to a restriction in the type system, writing a borrowed cow requires cloning it.
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<K: Protocol + Eq + Hash + Send + Sync, V: Protocol + Send + Sync> LengthPrefixed for HashMap<K, V>
impl<K: Protocol + Eq + Hash + Send + Sync, V: Protocol + Send + Sync> LengthPrefixed for HashMap<K, V>
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<K: Protocol + Eq + Hash + Send + Sync, V: Protocol + Send + Sync> LengthPrefixed for NEMap<K, V>
Available on crate feature nonempty-collections
only.
impl<K: Protocol + Eq + Hash + Send + Sync, V: Protocol + Send + Sync> LengthPrefixed for NEMap<K, V>
nonempty-collections
only.fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<K: Protocol + Ord + Send + Sync + 'static, V: Protocol + Send + Sync + 'static> LengthPrefixed for BTreeMap<K, V>
impl<K: Protocol + Ord + Send + Sync + 'static, V: Protocol + Send + Sync + 'static> LengthPrefixed for BTreeMap<K, V>
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<T: Protocol + Eq + Hash + Send + Sync> LengthPrefixed for HashSet<T>
impl<T: Protocol + Eq + Hash + Send + Sync> LengthPrefixed for HashSet<T>
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<T: Protocol + Eq + Hash + Send + Sync> LengthPrefixed for NESet<T>
Available on crate feature nonempty-collections
only.A set is prefixed with the length as a u64
.
impl<T: Protocol + Eq + Hash + Send + Sync> LengthPrefixed for NESet<T>
nonempty-collections
only.A set is prefixed with the length as a u64
.
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<T: Protocol + Ord + Send + Sync + 'static> LengthPrefixed for BTreeSet<T>
impl<T: Protocol + Ord + Send + Sync + 'static> LengthPrefixed for BTreeSet<T>
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<T: Protocol + Send + Sync> LengthPrefixed for Vec<T>
Note that due to Rust’s lack of specialization, this implementation is inefficient for Vec<u8>
.
Prefer Bytes
if possible.
impl<T: Protocol + Send + Sync> LengthPrefixed for Vec<T>
Note that due to Rust’s lack of specialization, this implementation is inefficient for Vec<u8>
.
Prefer Bytes
if possible.
fn read_length_prefixed<'a, R: AsyncRead + Unpin + Send + 'a>( stream: &'a mut R, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<Self, ReadError>> + Send + 'a>>
fn write_length_prefixed<'a, W: AsyncWrite + Unpin + Send + 'a>( &'a self, sink: &'a mut W, max_len: u64, ) -> Pin<Box<dyn Future<Output = Result<(), WriteError>> + Send + 'a>>
fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>
fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>
Source§impl<T: Protocol + Send + Sync> LengthPrefixed for NEVec<T>
Available on crate feature nonempty-collections
only.
impl<T: Protocol + Send + Sync> LengthPrefixed for NEVec<T>
nonempty-collections
only.