Macro hard::buffer_type [−][src]
macro_rules! buffer_type {
($($(#[$metadata : meta]) * $vis : vis $name : ident($size : expr) $(;) ?) *) => { ... };
}
Expand description
Create a new fixed-size buffer type.
buffer_type!(Name(Size))
will create a new type with name Name
, that provides access to
Size
bytes of hardened contiguous memory.
The new type will implement the following traits:
Buffer
andBufferMut
AsRef<[u8; Size]>
andAsMut<[u8; Size]>
Borrow<[u8; $size]>
and [`BorrowMut<[u8;Debug
Deref<Target = [u8; Size]>
andDerefMut
$size]>`](std::borrow::BorrowMut)PartialEq<Rhs = BufferMut or BufferReadOnly>
andEq
- This implementation uses a constant-time comparison function for equivalent-sized buffers, suitable for comparing sensitive data without the risk of timing attacks.
Pointer
If the restricted-types
feature is enabled, this macro also generates NameNoAccess
and
NameReadOnly
variants, which use the operating system’s memory protection utilities to mark
the buffer’s contents as completely inaccessible, and immutable, respectively.
Example Usage
use hard::{buffer_type, Buffer};
// Create a 32-byte (256-bit) buffer type called `Key`
buffer_type!(Key(32));
let mut my_key = Key::new().unwrap();
// The type implements Deref<Target = [u8; 32]> and DerefMut, so we can use any methods from
// the array type.
my_key.copy_from_slice(b"Some data to copy into my_key...");
my_key[0] ^= 0xca;
println!("{:x?}", my_key);
// By default, a new buffer type will be private, but we can specify that it should be public.
buffer_type!(pub Password(128));
// We can also provide documentation for the newly generated type, if we like.
buffer_type! {
/// This type stores some very important information
pub ImportantBuf(99);
}