From 960e0b606fdf62b2e7a33111b95ecbc170555ab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 19 Mar 2025 14:40:21 +0300 Subject: [PATCH 1/2] inout: add `into_out_with_copied_in` methods --- inout/src/inout.rs | 18 ++++++++++++++++++ inout/src/inout_buf.rs | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/inout/src/inout.rs b/inout/src/inout.rs index 20f5471e..6d0a05f0 100644 --- a/inout/src/inout.rs +++ b/inout/src/inout.rs @@ -33,6 +33,24 @@ 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` + /// and output value equal to the input value. + /// + /// In the case if the input and output references are the same, simply returns + /// the output reference. Otherwise, copies data from the former to the latter + /// before returning the output reference. + pub fn into_out_with_copied_in(self) -> &'out mut T + where + T: Copy, + { + if self.in_ptr != self.out_ptr { + unsafe { + ptr::copy(self.in_ptr, self.out_ptr, 1); + } + } + 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 { diff --git a/inout/src/inout_buf.rs b/inout/src/inout_buf.rs index e573b34d..618d0102 100644 --- a/inout/src/inout_buf.rs +++ b/inout/src/inout_buf.rs @@ -121,6 +121,24 @@ impl<'inp, 'out, T> InOutBuf<'inp, 'out, T> { unsafe { slice::from_raw_parts_mut(self.out_ptr, self.len) } } + /// Consume `self` and get the output slice with lifetime `'out` filled with data from + /// the input slice. + /// + /// In the case if the input and output slices point to the same memory, simply returns + /// the output slice. Otherwise, copies data from the former to the latter + /// before returning the output slice. + pub fn into_out_with_copied_in(self) -> &'out mut [T] + where + T: Copy, + { + if self.in_ptr != self.out_ptr { + unsafe { + core::ptr::copy(self.in_ptr, self.out_ptr, self.len); + } + } + unsafe { slice::from_raw_parts_mut(self.out_ptr, self.len) } + } + /// Consume `self` and get output slice with lifetime `'out`. #[inline(always)] pub fn into_out(self) -> &'out mut [T] { From e77371326f93c692d1dc841a40bcd159dc8b46d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=9F=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=BE=D0=B2=20=5BArtyom=20Pavlov=5D?= Date: Wed, 19 Mar 2025 14:43:15 +0300 Subject: [PATCH 2/2] Update changelog --- inout/CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/inout/CHANGELOG.md b/inout/CHANGELOG.md index a10d6fc2..494b02f2 100644 --- a/inout/CHANGELOG.md +++ b/inout/CHANGELOG.md @@ -12,11 +12,13 @@ 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]) +- `InOut::into_out_with_copied_in` and `InOutBuf::into_out_with_copied_in` methods ([#1169]) [#944]: https://github.com/RustCrypto/utils/pull/944 [#1132]: https://github.com/RustCrypto/utils/pull/1132 [#1132]: https://github.com/RustCrypto/utils/pull/1132 [#1149]: https://github.com/RustCrypto/utils/pull/1149 +[#1169]: https://github.com/RustCrypto/utils/pull/1169 ## 0.1.4 (2025-02-21) ### Fixed