Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add const getters for value pointer #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ where
value: T,
}

impl<A, T> Aligned<A, T>
where
A: Alignment,
T: ?Sized,
{
/// Gets a mutable pointer to the wrapped value.
pub const fn get(this: &Self) -> *mut T {
&this.value as *const T as *mut T
}

/// Gets a mutable pointer to the wrapped value.
/// The difference from get is that this function accepts a raw pointer,
/// which is useful to avoid the creation of temporary references.
pub const fn raw_get(this: *const Self) -> *mut T {
unsafe { &(*this).value as *const T as *mut T }
}
}

/// Changes the alignment of `value` to be at least `A` bytes
#[allow(non_snake_case)]
pub const fn Aligned<A, T>(value: T) -> Aligned<A, T> {
Expand Down Expand Up @@ -332,6 +350,18 @@ fn sanity() {
assert!(z.as_ptr() as usize % 8 == 0);
assert!(w.as_ptr() as usize % 16 == 0);

// test pointer getters
assert_eq!(Aligned::get(&a) as *const _, a.as_ptr());
assert_eq!(Aligned::get(&x) as *const _, x.as_ptr());
assert_eq!(Aligned::get(&y) as *const _, y.as_ptr());
assert_eq!(Aligned::get(&z) as *const _, z.as_ptr());
assert_eq!(Aligned::get(&w) as *const _, w.as_ptr());
assert_eq!(Aligned::raw_get(&a as *const _) as *const _, a.as_ptr());
assert_eq!(Aligned::raw_get(&x as *const _) as *const _, x.as_ptr());
assert_eq!(Aligned::raw_get(&y as *const _) as *const _, y.as_ptr());
assert_eq!(Aligned::raw_get(&z as *const _) as *const _, z.as_ptr());
assert_eq!(Aligned::raw_get(&w as *const _) as *const _, w.as_ptr());

// test `deref`
assert_eq!(a.len(), 3);
assert_eq!(x.len(), 3);
Expand Down