pub struct AlignedBuffer { /* private fields */ }Expand description
An owned, zero-initialised byte buffer aligned to a stated boundary.
The alignment is a property of the buffer, not of the first thing written
into it: it holds for the buffer’s whole life and is preserved by
Clone.
§Example
use windows_namespace_request_sys::AlignedBuffer;
// A self-relative security descriptor needs DWORD alignment; the directory
// information classes need 8. A `Box<[u8]>` guarantees neither -- its
// alignment is 1.
let mut buffer = AlignedBuffer::zeroed(20, 8);
assert_eq!(buffer.len(), 20);
assert_eq!(buffer.align(), 8);
assert_eq!(buffer.as_ptr() as usize % 8, 0);
assert!(buffer.as_slice().iter().all(|byte| *byte == 0));
// The length need not be a multiple of the alignment: the constraint is on
// the buffer's address, not its size.
buffer.as_mut_slice()[19] = 0xAB;
assert_eq!(buffer.as_slice()[19], 0xAB);§Example: a clone keeps the guarantee
use windows_namespace_request_sys::AlignedBuffer;
let original = AlignedBuffer::from_bytes(&[1, 2, 3], 4);
let clone = original.clone();
assert_eq!(clone, original);
assert_eq!(clone.as_ptr() as usize % 4, 0);
assert_ne!(clone.as_ptr(), original.as_ptr(), "a copy, not a second view");
// Alignment is part of what the buffer promises, so it is part of equality.
assert_ne!(AlignedBuffer::from_bytes(&[1, 2, 3], 8), original);Implementations§
Source§impl AlignedBuffer
impl AlignedBuffer
Sourcepub fn zeroed(len: usize, align: usize) -> Self
pub fn zeroed(len: usize, align: usize) -> Self
Allocates len zeroed bytes aligned to align.
§Panics
Panics if align is not a power of two, or if len rounded up to
align overflows isize – both of which are programming errors rather
than conditions a caller can encounter with valid input.
Sourcepub fn from_bytes(bytes: &[u8], align: usize) -> Self
pub fn from_bytes(bytes: &[u8], align: usize) -> Self
Sourcepub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
The buffer’s contents, mutably.
Sourcepub fn as_mut_ptr(&mut self) -> *mut u8
pub fn as_mut_ptr(&mut self) -> *mut u8
The buffer’s address, for a Win32 call that writes into it.
Trait Implementations§
Source§impl Clone for AlignedBuffer
impl Clone for AlignedBuffer
Source§impl Debug for AlignedBuffer
impl Debug for AlignedBuffer
Source§impl Drop for AlignedBuffer
impl Drop for AlignedBuffer
impl Eq for AlignedBuffer
Source§impl PartialEq for AlignedBuffer
impl PartialEq for AlignedBuffer
impl Send for AlignedBuffer
impl Sync for AlignedBuffer
Auto Trait Implementations§
impl Freeze for AlignedBuffer
impl RefUnwindSafe for AlignedBuffer
impl Unpin for AlignedBuffer
impl UnsafeUnpin for AlignedBuffer
impl UnwindSafe for AlignedBuffer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more