Skip to content

Commit

Permalink
use ptr api
Browse files Browse the repository at this point in the history
  • Loading branch information
Congyuwang committed Feb 18, 2024
1 parent b97e7f3 commit 31a3a71
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<T> ListHead<T> {
where
F: FnMut(&T),
{
let self_ptr = self as *const Self;
let self_ptr = ptr::from_ref(self);
let mut this = self;
loop {
f(this.get());
Expand All @@ -200,8 +200,8 @@ impl<T> ListHead<T> {
where
F: FnMut(&mut T),
{
let self_ptr = self as *const Self;
let mut this = &mut *self;
let self_ptr = ptr::from_ref(self);
let mut this = self;
loop {
f(this.get_mut());
let next = unsafe { this.next.assume_init_mut() };
Expand All @@ -217,7 +217,7 @@ impl<T> ListHead<T> {
where
F: FnMut(&T),
{
let self_ptr = self as *const Self;
let self_ptr = ptr::from_ref(self);
let mut this = self;
loop {
f(this.get());
Expand All @@ -234,8 +234,8 @@ impl<T> ListHead<T> {
where
F: FnMut(&mut T),
{
let self_ptr = self as *const Self;
let mut this = &mut *self;
let self_ptr = ptr::from_ref(self);
let mut this = self;
loop {
f(this.get_mut());
let prev = unsafe { this.prev.assume_init_mut() };
Expand All @@ -248,22 +248,26 @@ impl<T> ListHead<T> {

#[inline(always)]
fn get(&self) -> &T {
&unsafe { self.inner() }.data
unsafe { &self.inner().data }
}

#[inline(always)]
fn get_mut(&mut self) -> &mut T {
&mut unsafe { self.inner_mut() }.data
unsafe { &mut self.inner_mut().data }
}

#[inline(always)]
unsafe fn inner(&self) -> &Inner<T> {
&*((self as *const Self).byte_offset(Self::offset()) as *const Inner<T>)
&*(ptr::from_ref(self)
.byte_offset(Self::offset())
.cast::<Inner<T>>())
}

#[inline(always)]
unsafe fn inner_mut(&mut self) -> &mut Inner<T> {
&mut *((self as *mut Self).byte_offset(Self::offset()) as *mut Inner<T>)
&mut *(ptr::from_mut(self)
.byte_offset(Self::offset())
.cast::<Inner<T>>())
}

#[inline(always)]
Expand Down

0 comments on commit 31a3a71

Please sign in to comment.