Skip to main content

AlignedBuffer

Struct AlignedBuffer 

Source
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

Source

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.

Source

pub fn from_bytes(bytes: &[u8], align: usize) -> Self

Allocates a buffer aligned to align holding a copy of bytes.

§Panics

As zeroed.

Source

pub fn len(&self) -> usize

The buffer’s length in bytes.

Source

pub fn is_empty(&self) -> bool

Whether the buffer holds no bytes.

Source

pub fn align(&self) -> usize

The alignment the buffer’s address is guaranteed to satisfy.

Source

pub fn as_slice(&self) -> &[u8]

The buffer’s contents.

Source

pub fn as_mut_slice(&mut self) -> &mut [u8]

The buffer’s contents, mutably.

Source

pub fn as_ptr(&self) -> *const u8

The buffer’s address, for handing to a Win32 call.

Source

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

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AlignedBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for AlignedBuffer

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Eq for AlignedBuffer

Source§

impl PartialEq for AlignedBuffer

Source§

fn eq(&self, other: &Self) -> bool

Compares contents and alignment, not addresses.

1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Send for AlignedBuffer

Source§

impl Sync for AlignedBuffer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.