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

Dedup & de-lint #6

Closed
wants to merge 3 commits into from
Closed
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
32 changes: 20 additions & 12 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ impl<T: ?Sized> From<Rc<T>> for Pointer<T> {
let arg = Rc::from_raw(arg);
let rc = arg.clone();
mem::forget(arg);
NonNull::new_unchecked(Rc::into_raw(rc) as *mut _)
rc_to_non_null(rc)
}
unsafe fn drop<T: ?Sized>(ptr: NonNull<T>) {
mem::drop(Rc::from_raw(ptr.as_ptr()));
}
Pointer {
ptr: unsafe { NonNull::new_unchecked(Rc::into_raw(ptr) as *mut _) },
clone: clone::<T>,
drop: drop::<T>,
ptr: rc_to_non_null(ptr),
clone,
drop,
sync: false,
}
}
Expand All @@ -44,30 +44,30 @@ impl<T: ?Sized> From<Arc<T>> for Pointer<T> {
let arg = Arc::from_raw(arg);
let rc = arg.clone();
mem::forget(arg);
NonNull::new_unchecked(Arc::into_raw(rc) as *mut _)
arc_to_non_null(rc)
}
unsafe fn drop<T: ?Sized>(ptr: NonNull<T>) {
mem::drop(Arc::from_raw(ptr.as_ptr()));
}
Pointer {
ptr: unsafe { NonNull::new_unchecked(Arc::into_raw(ptr) as *mut _) },
clone: clone::<T>,
drop: drop::<T>,
ptr: arc_to_non_null(ptr),
clone,
drop,
sync: true,
}
}
}

impl<T: ?Sized> From<&'static T> for Pointer<T> {
fn from(ptr: &'static T) -> Pointer<T> {
unsafe fn clone<T: ?Sized>(arg: &T) -> NonNull<T> {
fn clone<T: ?Sized>(arg: &T) -> NonNull<T> {
NonNull::from(arg)
}
unsafe fn drop<T: ?Sized>(_ptr: NonNull<T>) { }
fn drop<T: ?Sized>(_ptr: NonNull<T>) { }
Pointer {
ptr: NonNull::from(ptr),
clone: clone::<T>,
drop: drop::<T>,
clone,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried hard to define this in terms of NonNull::from but I couldn't get the type to aligned. Read a bunch about higher-ranked trait bounds but couldn't figure it out.

drop,
sync: true,
}
}
Expand All @@ -89,3 +89,11 @@ impl<T: ?Sized> Drop for Pointer<T> {
unsafe { (self.drop)(self.ptr) }
}
}

// TODO: lift these into From impl in the stdlib (and do From<Box<T>> too while you're at it)
fn rc_to_non_null<T: ?Sized>(rc: Rc<T>) -> NonNull<T> {
unsafe { NonNull::new_unchecked(Rc::into_raw(rc) as *mut _) }
}
fn arc_to_non_null<T: ?Sized>(arc: Arc<T>) -> NonNull<T> {
unsafe { NonNull::new_unchecked(Arc::into_raw(arc) as *mut _) }
}