From 8bb6c50988ba6c32d92e4a8e640027ab3c0920cd Mon Sep 17 00:00:00 2001 From: Artyom Pavlov Date: Wed, 20 Nov 2024 20:19:15 +0300 Subject: [PATCH] inout: add `InOut::into_out` and `InOutBufReserved::into_out` (#1132) --- inout/CHANGELOG.md | 4 ++++ inout/src/inout.rs | 6 ++++++ inout/src/inout_buf.rs | 2 +- inout/src/reserved.rs | 6 ++++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/inout/CHANGELOG.md b/inout/CHANGELOG.md index 87bd1ba8..180e2c1a 100644 --- a/inout/CHANGELOG.md +++ b/inout/CHANGELOG.md @@ -9,8 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Migrated from `generic-array` to `hybrid-array` ([#944]) - MSRV is bumped to 1.81 ([#1116]) +### Added +- `InOut::into_out` and `InOutBufReserved::into_out` methods ([#1132]) + [#944]: https://github.com/RustCrypto/utils/pull/944 [#1116]: https://github.com/RustCrypto/utils/pull/1116 +[#1132]: https://github.com/RustCrypto/utils/pull/1132 ## 0.1.3 (2022-03-31) ### Fixed diff --git a/inout/src/inout.rs b/inout/src/inout.rs index 75ed2381..ae6b6882 100644 --- a/inout/src/inout.rs +++ b/inout/src/inout.rs @@ -33,6 +33,12 @@ impl<'inp, 'out, T> InOut<'inp, 'out, T> { unsafe { &mut *self.out_ptr } } + /// Consume `self` and get mutable reference to the output value with lifetime `'out`. + #[inline(always)] + pub fn into_out(self) -> &'out mut T { + unsafe { &mut *self.out_ptr } + } + /// Convert `self` to a pair of raw input and output pointers. #[inline(always)] pub fn into_raw(self) -> (*const T, *mut T) { diff --git a/inout/src/inout_buf.rs b/inout/src/inout_buf.rs index 7fb83877..442c59c6 100644 --- a/inout/src/inout_buf.rs +++ b/inout/src/inout_buf.rs @@ -121,7 +121,7 @@ impl<'inp, 'out, T> InOutBuf<'inp, 'out, T> { unsafe { slice::from_raw_parts_mut(self.out_ptr, self.len) } } - /// Consume self and return output slice with lifetime `'a`. + /// Consume `self` and get output slice with lifetime `'out`. #[inline(always)] pub fn into_out(self) -> &'out mut [T] { unsafe { slice::from_raw_parts_mut(self.out_ptr, self.len) } diff --git a/inout/src/reserved.rs b/inout/src/reserved.rs index 8a713781..587756b3 100644 --- a/inout/src/reserved.rs +++ b/inout/src/reserved.rs @@ -124,6 +124,12 @@ impl<'inp, 'out, T> InOutBufReserved<'inp, 'out, T> { pub fn get_out<'a>(&'a mut self) -> &'a mut [T] { unsafe { slice::from_raw_parts_mut(self.out_ptr, self.out_len) } } + + /// Consume `self` and get output slice with lifetime `'out`. + #[inline(always)] + pub fn into_out(self) -> &'out mut [T] { + unsafe { slice::from_raw_parts_mut(self.out_ptr, self.out_len) } + } } impl<'inp, 'out> InOutBufReserved<'inp, 'out, u8> {