Skip to content

Commit 7948352

Browse files
authored
inout: add InOutBufReserved::split_reserved method (#1133)
1 parent 8bb6c50 commit 7948352

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

inout/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
### Added
1313
- `InOut::into_out` and `InOutBufReserved::into_out` methods ([#1132])
14+
- `InOutBufReserved::split_reserved` method ([#1133])
1415

1516
[#944]: https://github.com/RustCrypto/utils/pull/944
1617
[#1116]: https://github.com/RustCrypto/utils/pull/1116
1718
[#1132]: https://github.com/RustCrypto/utils/pull/1132
19+
[#1132]: https://github.com/RustCrypto/utils/pull/1132
1820

1921
## 0.1.3 (2022-03-31)
2022
### Fixed

inout/src/reserved.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
use crate::errors::OutIsTooSmallError;
1+
use crate::{errors::OutIsTooSmallError, InOutBuf};
22
use core::{marker::PhantomData, slice};
33

44
#[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+
};
1210

1311
/// Custom slice type which references one immutable (input) slice and one
1412
/// mutable (output) slice. Input and output slices are either the same or
@@ -38,7 +36,9 @@ impl<'a, T> InOutBufReserved<'a, 'a, T> {
3836
_pd: PhantomData,
3937
})
4038
}
39+
}
4140

41+
impl<T> InOutBufReserved<'_, '_, T> {
4242
/// Create [`InOutBufReserved`] from raw input and output pointers.
4343
///
4444
/// # Safety
@@ -93,6 +93,23 @@ impl<'a, T> InOutBufReserved<'a, 'a, T> {
9393
pub fn get_out_len(&self) -> usize {
9494
self.in_len
9595
}
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+
}
96113
}
97114

98115
impl<'inp, 'out, T> InOutBufReserved<'inp, 'out, T> {

0 commit comments

Comments
 (0)