Trait LengthPrefixed

Source
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§

Source

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.

Source

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.

Source

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.

Source

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.

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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.

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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.

Using Bytes is recommended for sending large amounts of data, since the Protocol implementation for Vec<u8> reads and writes each byte individually.

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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.
Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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>
where B::Owned: LengthPrefixed + Send + Sync,

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.

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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>

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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.
Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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>

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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>

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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.

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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>

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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.

Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

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.
Source§

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>>

Source§

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>>

Source§

fn read_length_prefixed_sync( stream: &mut impl Read, max_len: u64, ) -> Result<Self, ReadError>

Source§

fn write_length_prefixed_sync( &self, sink: &mut impl Write, max_len: u64, ) -> Result<(), WriteError>

Implementors§