|
1 | 1 | //! Development-related functionality |
2 | 2 | pub use blobby; |
3 | 3 |
|
| 4 | +#[cfg(feature = "alloc")] |
| 5 | +use {core::fmt, inout::InOutBuf}; |
| 6 | + |
4 | 7 | /// Define AEAD test |
5 | 8 | #[macro_export] |
6 | 9 | macro_rules! new_test { |
@@ -75,3 +78,67 @@ macro_rules! new_test { |
75 | 78 | } |
76 | 79 | }; |
77 | 80 | } |
| 81 | + |
| 82 | +/// [`BicephalBuffer`] is meant for testing InOut-backed APIs. |
| 83 | +/// |
| 84 | +/// It will split the initial buffer in two backing buffers copying the initial data. |
| 85 | +#[cfg(feature = "alloc")] |
| 86 | +pub struct BicephalBuffer { |
| 87 | + in_buf: alloc::vec::Vec<u8>, |
| 88 | + out_buf: alloc::vec::Vec<u8>, |
| 89 | +} |
| 90 | + |
| 91 | +#[cfg(feature = "alloc")] |
| 92 | +impl AsRef<[u8]> for BicephalBuffer { |
| 93 | + fn as_ref(&self) -> &[u8] { |
| 94 | + &self.out_buf |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(feature = "alloc")] |
| 99 | +impl From<&[u8]> for BicephalBuffer { |
| 100 | + fn from(buf: &[u8]) -> Self { |
| 101 | + Self { |
| 102 | + in_buf: buf.to_vec(), |
| 103 | + out_buf: buf.to_vec(), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +#[cfg(feature = "alloc")] |
| 109 | +impl From<alloc::vec::Vec<u8>> for BicephalBuffer { |
| 110 | + fn from(buf: alloc::vec::Vec<u8>) -> Self { |
| 111 | + Self { |
| 112 | + in_buf: buf.clone(), |
| 113 | + out_buf: buf, |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[cfg(feature = "alloc")] |
| 119 | +impl BicephalBuffer { |
| 120 | + /// Get an [`InOutBuf`] from a [`BicephalBuffer`] |
| 121 | + pub fn to_in_out_buf(&mut self) -> InOutBuf<'_, '_, u8> { |
| 122 | + InOutBuf::new(self.in_buf.as_slice(), self.out_buf.as_mut_slice()) |
| 123 | + .expect("Invariant violation") |
| 124 | + } |
| 125 | + |
| 126 | + /// Return the length of the payload |
| 127 | + #[inline] |
| 128 | + pub fn len(&self) -> usize { |
| 129 | + self.in_buf.len() |
| 130 | + } |
| 131 | + |
| 132 | + /// Is the payload empty? |
| 133 | + #[inline] |
| 134 | + pub fn is_empty(&self) -> bool { |
| 135 | + self.in_buf.is_empty() |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +#[cfg(feature = "alloc")] |
| 140 | +impl fmt::Debug for BicephalBuffer { |
| 141 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 142 | + write!(f, "BicephalBuffer {{...}}") |
| 143 | + } |
| 144 | +} |
0 commit comments