From 58841208b1f0b5c6357559e720c98e453d4cd32d Mon Sep 17 00:00:00 2001 From: Brian Smith Date: Tue, 31 Dec 2024 14:36:17 -0800 Subject: [PATCH] inout: Generalize `Overlapping` to support arbitrary types. Take a step towards `inout` being used by more things. --- src/aead/aes.rs | 2 +- src/inout/overlapping/overlapping.rs | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/aead/aes.rs b/src/aead/aes.rs index d40b91f56..7fd869875 100644 --- a/src/aead/aes.rs +++ b/src/aead/aes.rs @@ -33,7 +33,7 @@ pub(super) mod fallback; pub(super) mod hw; pub(super) mod vp; -pub type Overlapping<'o> = overlapping::Overlapping<'o>; +pub type Overlapping<'o> = overlapping::Overlapping<'o, u8>; cfg_if! { if #[cfg(any(target_arch = "aarch64", target_arch = "x86_64"))] { diff --git a/src/inout/overlapping/overlapping.rs b/src/inout/overlapping/overlapping.rs index 607e9be6d..f46cb5090 100644 --- a/src/inout/overlapping/overlapping.rs +++ b/src/inout/overlapping/overlapping.rs @@ -15,17 +15,17 @@ use crate::error; use core::ops::RangeFrom; -pub struct Overlapping<'o> { - in_out: &'o mut [u8], +pub struct Overlapping<'o, T> { + in_out: &'o mut [T], src: RangeFrom, } -impl<'o> Overlapping<'o> { - pub fn in_place(in_out: &'o mut [u8]) -> Self { +impl<'o, T> Overlapping<'o, T> { + pub fn in_place(in_out: &'o mut [T]) -> Self { Self { in_out, src: 0.. } } - pub fn overlapping(in_out: &'o mut [u8], src: RangeFrom) -> Result { + pub fn overlapping(in_out: &'o mut [T], src: RangeFrom) -> Result { match in_out.get(src.clone()) { Some(_) => Ok(Self { in_out, src }), None => Err(SrcIndexError::new(src)), @@ -33,20 +33,20 @@ impl<'o> Overlapping<'o> { } #[cfg(any(target_arch = "arm", target_arch = "x86"))] - pub fn into_slice_src_mut(self) -> (&'o mut [u8], RangeFrom) { + pub fn into_slice_src_mut(self) -> (&'o mut [T], RangeFrom) { (self.in_out, self.src) } } -impl Overlapping<'_> { +impl Overlapping<'_, T> { pub fn len(&self) -> usize { self.in_out[self.src.clone()].len() } - pub fn into_input_output_len(self) -> (*const u8, *mut u8, usize) { + pub fn into_input_output_len(self) -> (*const T, *mut T, usize) { let len = self.len(); let output = self.in_out.as_mut_ptr(); // TODO: MSRV(1.65): use `output.cast_const()` - let output_const: *const u8 = output; + let output_const: *const T = output; // SAFETY: The constructor ensures that `src` is a valid range. // Equivalent to `self.in_out[src.clone()].as_ptr()` but without // worries about compatibility with the stacked borrows model.