vitaminc_async_traits/
lib.rs

1#![doc = include_str!("../README.md")]
2use vitaminc_protected::Zeroed;
3use vitaminc_traits::OutputSize;
4
5/// Defines an asynchronous digest or MAC algorithm output function.
6/// `N` is the output size in bytes.
7/// `O` is the output type which must implement `OutputSize<N>`.
8#[allow(async_fn_in_trait)]
9pub trait AsyncFixedOutput<const N: usize, O>: Sized
10where
11    // TODO: Make this bound on a "tagged" value (i.e. sensitive or safe)
12    O: Sized + Send + OutputSize<N>,
13{
14    type Error;
15
16    /// Consume value and write result into provided array.
17    async fn try_finalize_into(self, out: &mut O) -> Result<(), Self::Error>;
18
19    /// Retrieve result and consume the hasher instance.
20    #[inline]
21    async fn try_finalize_fixed(self) -> Result<O, Self::Error>
22    where
23        O: Zeroed,
24    {
25        let mut out = Zeroed::zeroed();
26        if let Err(err) = self.try_finalize_into(&mut out).await {
27            Err(err)
28        } else {
29            Ok(out)
30        }
31    }
32}
33
34/// Defines an asynchronous digest or MAC algorithm output function that resets the state.
35/// `N` is the output size in bytes.
36/// `O` is the output type which must implement `OutputSize<N>`.
37#[allow(async_fn_in_trait)]
38pub trait AsyncFixedOutputReset<const N: usize, O>
39where
40    O: OutputSize<N>,
41{
42    type Error;
43
44    async fn try_finalize_into_reset(&mut self, out: &mut O) -> Result<(), Self::Error>;
45
46    #[inline]
47    async fn try_finalize_fixed_reset(&mut self) -> Result<O, Self::Error>
48    where
49        O: Zeroed,
50    {
51        let mut out = Zeroed::zeroed();
52        if let Err(err) = self.try_finalize_into_reset(&mut out).await {
53            Err(err)
54        } else {
55            Ok(out)
56        }
57    }
58}