|
1 | 1 | /// Creates a buffered wrapper around block-level "core" type which implements variable output size traits |
2 | 2 | /// with output size selected at compile time. |
| 3 | +#[doc(hidden)] |
3 | 4 | #[macro_export] |
4 | | -macro_rules! buffer_ct_variable { |
| 5 | +macro_rules! buffer_ct_variable_internal { |
5 | 6 | ( |
6 | 7 | $(#[$attr:meta])* |
7 | 8 | $vis:vis struct $name:ident<$out_size:ident>($core_ty:ty); |
@@ -116,8 +117,7 @@ macro_rules! buffer_ct_variable { |
116 | 117 | Self { core, buffer } |
117 | 118 | } |
118 | 119 | fn decompose(self) -> (Self::Core, $crate::block_api::Buffer<Self::Core>) { |
119 | | - let Self { core, buffer } = self; |
120 | | - (core, buffer) |
| 120 | + (self.core.clone(), self.buffer.clone()) |
121 | 121 | } |
122 | 122 | } |
123 | 123 |
|
@@ -209,3 +209,68 @@ macro_rules! buffer_ct_variable { |
209 | 209 | } |
210 | 210 | }; |
211 | 211 | } |
| 212 | + |
| 213 | +#[doc(hidden)] |
| 214 | +#[cfg(not(feature = "zeroize"))] |
| 215 | +#[macro_export] |
| 216 | +macro_rules! buffer_ct_variable_zeroize_on_drop { |
| 217 | + ($name:ident, $out_size:ident, $max_size:ty) => { |
| 218 | + impl<$out_size> Drop for $name<$out_size> |
| 219 | + where |
| 220 | + $out_size: $crate::array::ArraySize |
| 221 | + + $crate::typenum::IsLessOrEqual<$max_size, Output = $crate::typenum::True>, |
| 222 | + { |
| 223 | + fn drop(&mut self) {} |
| 224 | + } |
| 225 | + }; |
| 226 | +} |
| 227 | + |
| 228 | +#[doc(hidden)] |
| 229 | +#[cfg(feature = "zeroize")] |
| 230 | +#[macro_export] |
| 231 | +macro_rules! buffer_ct_variable_zeroize_on_drop { |
| 232 | + ($name:ident, $out_size:ident, $max_size:ty) => { |
| 233 | + impl<$out_size> Drop for $name<$out_size> |
| 234 | + where |
| 235 | + $out_size: $crate::array::ArraySize |
| 236 | + + $crate::typenum::IsLessOrEqual<$max_size, Output = $crate::typenum::True>, |
| 237 | + { |
| 238 | + fn drop(&mut self) { |
| 239 | + let Self { core, buffer } = self; |
| 240 | + // Ensure these types implement `ZeroizeOnDrop` and therefore we have no work to do |
| 241 | + fn implements_zeroize_on_drop(_value: &mut impl $crate::zeroize::ZeroizeOnDrop) {} |
| 242 | + implements_zeroize_on_drop(core); |
| 243 | + implements_zeroize_on_drop(buffer); |
| 244 | + } |
| 245 | + } |
| 246 | + impl<$out_size> $crate::zeroize::ZeroizeOnDrop for $name<$out_size> where |
| 247 | + $out_size: $crate::array::ArraySize |
| 248 | + + $crate::typenum::IsLessOrEqual<$max_size, Output = $crate::typenum::True> |
| 249 | + { |
| 250 | + } |
| 251 | + }; |
| 252 | +} |
| 253 | + |
| 254 | +/// Creates a buffered wrapper around block-level "core" type which implements variable output size traits |
| 255 | +/// with output size selected at compile time. |
| 256 | +#[macro_export] |
| 257 | +macro_rules! buffer_ct_variable { |
| 258 | + ( |
| 259 | + $(#[$attr:meta])* |
| 260 | + $vis:vis struct $name:ident<$out_size:ident>($core_ty:ty); |
| 261 | + exclude: SerializableState; |
| 262 | + // Ideally, we would use `$core_ty::OutputSize`, but unfortunately the compiler |
| 263 | + // does not accept such code. The likely reason is this issue: |
| 264 | + // https://github.com/rust-lang/rust/issues/79629 |
| 265 | + max_size: $max_size:ty; |
| 266 | + ) => { |
| 267 | + $crate::buffer_ct_variable_internal!( |
| 268 | + $(#[$attr])* |
| 269 | + $vis struct $name<$out_size>($core_ty); |
| 270 | + exclude: SerializableState; |
| 271 | + max_size: $max_size; |
| 272 | + ); |
| 273 | + |
| 274 | + $crate::buffer_ct_variable_zeroize_on_drop!($name, $out_size, $max_size); |
| 275 | + } |
| 276 | +} |
0 commit comments