Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inout: add InOutBufReserved::split_reserved method #1133

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 25 additions & 8 deletions inout/src/reserved.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use crate::errors::OutIsTooSmallError;
use crate::{errors::OutIsTooSmallError, InOutBuf};
use core::{marker::PhantomData, slice};

#[cfg(feature = "block-padding")]
use crate::errors::PadError;
#[cfg(feature = "block-padding")]
use crate::{InOut, InOutBuf};
#[cfg(feature = "block-padding")]
use block_padding::{PadType, Padding};
#[cfg(feature = "block-padding")]
use hybrid_array::{Array, ArraySize};
use {
crate::{errors::PadError, InOut},
block_padding::{PadType, Padding},
hybrid_array::{Array, ArraySize},
};

/// Custom slice type which references one immutable (input) slice and one
/// mutable (output) slice. Input and output slices are either the same or
Expand Down Expand Up @@ -38,7 +36,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 +93,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