Skip to content

Commit

Permalink
inout: add InOutBufReserved::split_reserved method
Browse files Browse the repository at this point in the history
  • Loading branch information
newpavlov committed Nov 20, 2024
1 parent 8bb6c50 commit d31cba1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inout/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- `InOut::into_out` and `InOutBufReserved::into_out` methods ([#1132])
- `InOutBufReserved::split_reserved` method ([#1133])

[#944]: https://github.com/RustCrypto/utils/pull/944
[#1116]: https://github.com/RustCrypto/utils/pull/1116
[#1132]: https://github.com/RustCrypto/utils/pull/1132
[#1132]: https://github.com/RustCrypto/utils/pull/1132

## 0.1.3 (2022-03-31)
### Fixed
Expand Down
19 changes: 19 additions & 0 deletions inout/src/reserved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ impl<'a, T> InOutBufReserved<'a, 'a, T> {
_pd: PhantomData,
})
}
}

impl<T> InOutBufReserved<'_, '_, T> {
/// Create [`InOutBufReserved`] from raw input and output pointers.
///
/// # Safety
Expand Down Expand Up @@ -93,6 +95,23 @@ impl<'a, T> InOutBufReserved<'a, 'a, T> {
pub fn get_out_len(&self) -> usize {
self.in_len
}

/// Split buffer into `InOutBuf` with input length and mutable slice pointing to
/// the reamining reserved suffix.
pub fn split_reserved<'a>(&'a mut self) -> (InOutBuf<'a, 'a, T>, &'a mut [T]) {
let in_len = self.get_in_len();
let out_len = self.get_out_len();
let in_ptr = self.get_in().as_ptr();
let out_ptr = self.get_out().as_mut_ptr();
// This never underflows because the type ensures that `out_len` is
// bigger or equal to `in_len`.
let tail_len = out_len - in_len;
unsafe {
let body = InOutBuf::from_raw(in_ptr, out_ptr, in_len);
let tail = slice::from_raw_parts_mut(out_ptr.add(in_len), tail_len);
(body, tail)
}
}
}

impl<'inp, 'out, T> InOutBufReserved<'inp, 'out, T> {
Expand Down

0 comments on commit d31cba1

Please sign in to comment.