Skip to content

Commit 0966d23

Browse files
authored
inout: add into_out_with_copied_in methods (#1169)
The methods can be useful in cases where we want to support `inout` APIs, but algorithm implementation supports only the in-place mode.
1 parent 9160974 commit 0966d23

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

inout/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1212
### Added
1313
- `InOut::into_out` and `InOutBufReserved::into_out` methods ([#1132])
1414
- `InOutBufReserved::split_reserved` method ([#1133])
15+
- `InOut::into_out_with_copied_in` and `InOutBuf::into_out_with_copied_in` methods ([#1169])
1516

1617
[#944]: https://github.com/RustCrypto/utils/pull/944
1718
[#1132]: https://github.com/RustCrypto/utils/pull/1132
1819
[#1132]: https://github.com/RustCrypto/utils/pull/1132
1920
[#1149]: https://github.com/RustCrypto/utils/pull/1149
21+
[#1169]: https://github.com/RustCrypto/utils/pull/1169
2022

2123
## 0.1.4 (2025-02-21)
2224
### Fixed

inout/src/inout.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ impl<'inp, 'out, T> InOut<'inp, 'out, T> {
3333
unsafe { &mut *self.out_ptr }
3434
}
3535

36+
/// Consume `self` and get mutable reference to the output value with lifetime `'out`
37+
/// and output value equal to the input value.
38+
///
39+
/// In the case if the input and output references are the same, simply returns
40+
/// the output reference. Otherwise, copies data from the former to the latter
41+
/// before returning the output reference.
42+
pub fn into_out_with_copied_in(self) -> &'out mut T
43+
where
44+
T: Copy,
45+
{
46+
if self.in_ptr != self.out_ptr {
47+
unsafe {
48+
ptr::copy(self.in_ptr, self.out_ptr, 1);
49+
}
50+
}
51+
unsafe { &mut *self.out_ptr }
52+
}
53+
3654
/// Consume `self` and get mutable reference to the output value with lifetime `'out`.
3755
#[inline(always)]
3856
pub fn into_out(self) -> &'out mut T {

inout/src/inout_buf.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,24 @@ impl<'inp, 'out, T> InOutBuf<'inp, 'out, T> {
121121
unsafe { slice::from_raw_parts_mut(self.out_ptr, self.len) }
122122
}
123123

124+
/// Consume `self` and get the output slice with lifetime `'out` filled with data from
125+
/// the input slice.
126+
///
127+
/// In the case if the input and output slices point to the same memory, simply returns
128+
/// the output slice. Otherwise, copies data from the former to the latter
129+
/// before returning the output slice.
130+
pub fn into_out_with_copied_in(self) -> &'out mut [T]
131+
where
132+
T: Copy,
133+
{
134+
if self.in_ptr != self.out_ptr {
135+
unsafe {
136+
core::ptr::copy(self.in_ptr, self.out_ptr, self.len);
137+
}
138+
}
139+
unsafe { slice::from_raw_parts_mut(self.out_ptr, self.len) }
140+
}
141+
124142
/// Consume `self` and get output slice with lifetime `'out`.
125143
#[inline(always)]
126144
pub fn into_out(self) -> &'out mut [T] {

0 commit comments

Comments
 (0)