Skip to main content

wire_repr/codec/
plan.rs

1/// A completed, infallible encoding operation.
2///
3/// Implementations perform all fallible work while they are created. `write_into`
4/// therefore only copies already-prepared bytes into an exactly-sized output slice.
5pub trait EncodePlan {
6    /// Returns the exact number of bytes written by [`Self::write_into`].
7    #[must_use]
8    fn encoded_len(&self) -> usize;
9
10    /// Writes this plan into `output`.
11    ///
12    /// `output` must have length [`Self::encoded_len`]. Passing another length is a
13    /// contract violation and may panic; implementations must not silently succeed
14    /// without writing the complete encoding.
15    fn write_into(&self, output: &mut [u8]);
16}
17
18impl<const N: usize> EncodePlan for [u8; N] {
19    #[inline]
20    fn encoded_len(&self) -> usize {
21        N
22    }
23
24    #[inline]
25    fn write_into(&self, output: &mut [u8]) {
26        output.copy_from_slice(self);
27    }
28}
29
30impl EncodePlan for &[u8] {
31    #[inline]
32    fn encoded_len(&self) -> usize {
33        self.len()
34    }
35
36    #[inline]
37    fn write_into(&self, output: &mut [u8]) {
38        output.copy_from_slice(self);
39    }
40}