#![allow(clippy::needless_lifetimes)]
use std::fmt;
use std::marker::PhantomData;
use std::mem;
use std::ops::Add;
#[doc(hidden)]
pub extern crate memoffset as __memoffset;
#[repr(transparent)]
pub struct FieldOffset<T, U>(
usize,
PhantomData<dyn for<'a> Fn(&'a T) -> &'a U>,
);
impl<T, U> FieldOffset<T, U> {
#[cfg(fieldoffset_maybe_uninit)]
#[inline]
fn with_uninit_ptr<R, F: FnOnce(*const T) -> R>(f: F) -> R {
let uninit = mem::MaybeUninit::<T>::uninit();
f(uninit.as_ptr())
}
#[cfg(not(fieldoffset_maybe_uninit))]
#[inline]
fn with_uninit_ptr<R, F: FnOnce(*const T) -> R>(f: F) -> R {
f(mem::align_of::<T>() as *const T)
}
pub unsafe fn new<F: for<'a> FnOnce(*const T) -> *const U>(f: F) -> Self {
let offset = Self::with_uninit_ptr(|base_ptr| {
let field_ptr = f(base_ptr);
(field_ptr as usize).wrapping_sub(base_ptr as usize)
});
Self::new_from_offset(offset)
}
#[inline]
pub unsafe fn new_from_offset(offset: usize) -> Self {
assert!(offset + mem::size_of::<U>() <= mem::size_of::<T>());
FieldOffset(offset, PhantomData)
}
#[inline]
pub fn apply_ptr(self, x: *const T) -> *const U {
((x as usize) + self.0) as *const U
}
#[inline]
pub fn apply_ptr_mut(self, x: *mut T) -> *mut U {
((x as usize) + self.0) as *mut U
}
#[inline]
pub fn apply<'a>(self, x: &'a T) -> &'a U {
unsafe { &*self.apply_ptr(x) }
}
#[inline]
pub fn apply_mut<'a>(self, x: &'a mut T) -> &'a mut U {
unsafe { &mut *self.apply_ptr_mut(x) }
}
#[inline]
pub fn get_byte_offset(self) -> usize {
self.0
}
#[inline]
pub unsafe fn unapply_ptr(self, x: *const U) -> *const T {
((x as usize) - self.0) as *const T
}
#[inline]
pub unsafe fn unapply_ptr_mut(self, x: *mut U) -> *mut T {
((x as usize) - self.0) as *mut T
}
#[inline]
pub unsafe fn unapply<'a>(self, x: &'a U) -> &'a T {
&*self.unapply_ptr(x)
}
#[inline]
pub unsafe fn unapply_mut<'a>(self, x: &'a mut U) -> &'a mut T {
&mut *self.unapply_ptr_mut(x)
}
}
impl<T, U, V> Add<FieldOffset<U, V>> for FieldOffset<T, U> {
type Output = FieldOffset<T, V>;
#[inline]
fn add(self, other: FieldOffset<U, V>) -> FieldOffset<T, V> {
FieldOffset(self.0 + other.0, PhantomData)
}
}
impl<T, U> fmt::Debug for FieldOffset<T, U> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "FieldOffset({:#x})", self.0)
}
}
impl<T, U> Copy for FieldOffset<T, U> {}
impl<T, U> Clone for FieldOffset<T, U> {
fn clone(&self) -> Self {
*self
}
}
#[macro_export]
macro_rules! offset_of {
($t: tt => $f: tt) => {{
#[allow(unused_unsafe)]
unsafe {
$crate::FieldOffset::<$t, _>::new(|x| {
$crate::__memoffset::raw_field!(x, $t, $f)
})
}
}};
($t: path => $f: ident: $($rest: tt)*) => {
offset_of!($t => $f) + offset_of!($($rest)*)
};
}
#[cfg(test)]
mod tests {
#[derive(Debug)]
struct Foo {
a: u32,
b: f64,
c: bool,
}
#[derive(Debug)]
struct Bar {
x: u32,
y: Foo,
}
#[derive(Debug)]
struct Tuple(i32, f64);
#[test]
fn test_simple() {
let foo_b = offset_of!(Foo => b);
let mut x = Foo {
a: 1,
b: 2.0,
c: false,
};
{
let y = foo_b.apply(&x);
assert!(*y == 2.0);
}
{
let y = foo_b.apply_mut(&mut x);
*y = 42.0;
}
assert!(x.b == 42.0);
}
#[test]
fn test_tuple() {
let tuple_1 = offset_of!(Tuple => 1);
let mut x = Tuple(1, 42.0);
{
let y = tuple_1.apply(&x);
assert!(*y == 42.0);
}
{
let y = tuple_1.apply_mut(&mut x);
*y = 5.0;
}
assert!(x.1 == 5.0);
}
#[test]
fn test_nested() {
let mut x = Bar {
x: 0,
y: Foo {
a: 1,
b: 2.0,
c: false,
},
};
let bar_y_b = offset_of!(Bar => y: Foo => b);
{
let y = bar_y_b.apply_mut(&mut x);
*y = 42.0;
}
assert!(x.y.b == 42.0);
}
}