From 1bf0380fbe455630db875d51c4008dd85c567315 Mon Sep 17 00:00:00 2001 From: Tero Huttunen Date: Mon, 14 Oct 2024 10:43:25 +0300 Subject: [PATCH] Add const getters for pointer --- src/lib.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 802366d..03268d9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,6 +107,24 @@ where value: T, } +impl Aligned +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(value: T) -> Aligned { @@ -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);