|
1 | | -use crate::errors::OutIsTooSmallError; |
| 1 | +use crate::{errors::OutIsTooSmallError, InOutBuf}; |
2 | 2 | use core::{marker::PhantomData, slice}; |
3 | 3 |
|
4 | 4 | #[cfg(feature = "block-padding")] |
5 | | -use crate::errors::PadError; |
6 | | -#[cfg(feature = "block-padding")] |
7 | | -use crate::{InOut, InOutBuf}; |
8 | | -#[cfg(feature = "block-padding")] |
9 | | -use block_padding::{PadType, Padding}; |
10 | | -#[cfg(feature = "block-padding")] |
11 | | -use hybrid_array::{Array, ArraySize}; |
| 5 | +use { |
| 6 | + crate::{errors::PadError, InOut}, |
| 7 | + block_padding::{PadType, Padding}, |
| 8 | + hybrid_array::{Array, ArraySize}, |
| 9 | +}; |
12 | 10 |
|
13 | 11 | /// Custom slice type which references one immutable (input) slice and one |
14 | 12 | /// mutable (output) slice. Input and output slices are either the same or |
@@ -38,7 +36,9 @@ impl<'a, T> InOutBufReserved<'a, 'a, T> { |
38 | 36 | _pd: PhantomData, |
39 | 37 | }) |
40 | 38 | } |
| 39 | +} |
41 | 40 |
|
| 41 | +impl<T> InOutBufReserved<'_, '_, T> { |
42 | 42 | /// Create [`InOutBufReserved`] from raw input and output pointers. |
43 | 43 | /// |
44 | 44 | /// # Safety |
@@ -93,6 +93,23 @@ impl<'a, T> InOutBufReserved<'a, 'a, T> { |
93 | 93 | pub fn get_out_len(&self) -> usize { |
94 | 94 | self.in_len |
95 | 95 | } |
| 96 | + |
| 97 | + /// Split buffer into `InOutBuf` with input length and mutable slice pointing to |
| 98 | + /// the reamining reserved suffix. |
| 99 | + pub fn split_reserved<'a>(&'a mut self) -> (InOutBuf<'a, 'a, T>, &'a mut [T]) { |
| 100 | + let in_len = self.get_in_len(); |
| 101 | + let out_len = self.get_out_len(); |
| 102 | + let in_ptr = self.get_in().as_ptr(); |
| 103 | + let out_ptr = self.get_out().as_mut_ptr(); |
| 104 | + // This never underflows because the type ensures that `out_len` is |
| 105 | + // bigger or equal to `in_len`. |
| 106 | + let tail_len = out_len - in_len; |
| 107 | + unsafe { |
| 108 | + let body = InOutBuf::from_raw(in_ptr, out_ptr, in_len); |
| 109 | + let tail = slice::from_raw_parts_mut(out_ptr.add(in_len), tail_len); |
| 110 | + (body, tail) |
| 111 | + } |
| 112 | + } |
96 | 113 | } |
97 | 114 |
|
98 | 115 | impl<'inp, 'out, T> InOutBufReserved<'inp, 'out, T> { |
|
0 commit comments