aead_io/
buffer.rs

1use aead::Buffer;
2
3/// A trait for describing a buffer with a max capacity. Useful for `no_std` environments.
4/// Automatically implemented for `Vec<u8>` when `alloc` enabled
5pub trait CappedBuffer: Buffer {
6    /// Return the maximum capacity of the buffer
7    fn capacity(&self) -> usize;
8}
9
10#[cfg(feature = "alloc")]
11impl CappedBuffer for alloc::vec::Vec<u8> {
12    fn capacity(&self) -> usize {
13        self.capacity()
14    }
15}
16
17/// A trait for describing a buffer which can be resized. Useful for `no_std` environments.
18/// Automatically implemented for `Vec<u8>` when `alloc` enabled
19pub trait ResizeBuffer: Buffer {
20    /// Resize to the specified size and fill with zeroes when necessary
21    fn resize_zeroed(&mut self, new_len: usize) -> Result<(), aead::Error>;
22}
23
24#[cfg(feature = "alloc")]
25impl ResizeBuffer for alloc::vec::Vec<u8> {
26    fn resize_zeroed(&mut self, new_len: usize) -> Result<(), aead::Error> {
27        self.resize(new_len, 0);
28        Ok(())
29    }
30}