s2n_codec/decoder/
buffer_mut.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::decoder::{
5    buffer::DecoderBuffer,
6    value::{DecoderParameterizedValueMut, DecoderValueMut},
7    DecoderError,
8};
9
10pub type DecoderBufferMutResult<'a, T> = Result<(T, DecoderBufferMut<'a>), DecoderError>;
11
12/// DecoderBufferMut is a panic-free, mutable byte buffer for decoding untrusted input
13#[derive(Debug, Eq, PartialEq, PartialOrd, Ord, Hash)]
14pub struct DecoderBufferMut<'a> {
15    bytes: &'a mut [u8],
16}
17
18impl<'a> DecoderBufferMut<'a> {
19    /// Create a new `DecoderBufferMut` from a byte slice
20    #[inline]
21    pub fn new(bytes: &'a mut [u8]) -> Self {
22        Self { bytes }
23    }
24
25    /// Freeze the mutable buffer into a `DecoderBuffer`
26    #[inline]
27    pub fn freeze(self) -> DecoderBuffer<'a> {
28        DecoderBuffer::new(self.bytes)
29    }
30
31    /// Move out the buffer's slice. This should be used with caution, as it
32    /// removes any panic protection this struct provides.
33    #[inline]
34    pub fn into_less_safe_slice(self) -> &'a mut [u8] {
35        self.bytes
36    }
37
38    /// Mutably borrow the buffer's slice. This should be used with caution, as it
39    /// removes any panic protection this struct provides.
40    #[inline]
41    pub fn as_less_safe_slice_mut(&'a mut self) -> &'a mut [u8] {
42        self.bytes
43    }
44}
45
46impl_buffer!(
47    DecoderBufferMut,
48    DecoderBufferMutResult,
49    DecoderValueMut,
50    decode_mut,
51    DecoderParameterizedValueMut,
52    decode_parameterized_mut,
53    split_at_mut
54);
55
56impl<'a> From<DecoderBufferMut<'a>> for DecoderBuffer<'a> {
57    fn from(b: DecoderBufferMut<'a>) -> Self {
58        b.freeze()
59    }
60}