Sprinkle some whitespace & uses
This commit is contained in:
parent
c738dcc284
commit
f028636b1a
@ -13,7 +13,7 @@
|
||||
//! The tag must implement the `Tag` trait. We assert that the tag and `Pointer`
|
||||
//! are compatible at compile time.
|
||||
|
||||
use std::mem::ManuallyDrop;
|
||||
use std::mem::{self, ManuallyDrop};
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
use std::sync::Arc;
|
||||
@ -104,14 +104,17 @@ pub unsafe trait Tag: Copy {
|
||||
|
||||
unsafe impl<T> Pointer for Box<T> {
|
||||
const BITS: usize = bits_for::<Self::Target>();
|
||||
|
||||
#[inline]
|
||||
fn into_usize(self) -> usize {
|
||||
Box::into_raw(self) as usize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn from_usize(ptr: usize) -> Self {
|
||||
Box::from_raw(ptr as *mut T)
|
||||
}
|
||||
|
||||
unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
|
||||
let raw = ManuallyDrop::new(Self::from_usize(ptr));
|
||||
f(&raw)
|
||||
@ -120,14 +123,17 @@ unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
|
||||
|
||||
unsafe impl<T> Pointer for Rc<T> {
|
||||
const BITS: usize = bits_for::<Self::Target>();
|
||||
|
||||
#[inline]
|
||||
fn into_usize(self) -> usize {
|
||||
Rc::into_raw(self) as usize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn from_usize(ptr: usize) -> Self {
|
||||
Rc::from_raw(ptr as *const T)
|
||||
}
|
||||
|
||||
unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
|
||||
let raw = ManuallyDrop::new(Self::from_usize(ptr));
|
||||
f(&raw)
|
||||
@ -136,14 +142,17 @@ unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
|
||||
|
||||
unsafe impl<T> Pointer for Arc<T> {
|
||||
const BITS: usize = bits_for::<Self::Target>();
|
||||
|
||||
#[inline]
|
||||
fn into_usize(self) -> usize {
|
||||
Arc::into_raw(self) as usize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn from_usize(ptr: usize) -> Self {
|
||||
Arc::from_raw(ptr as *const T)
|
||||
}
|
||||
|
||||
unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
|
||||
let raw = ManuallyDrop::new(Self::from_usize(ptr));
|
||||
f(&raw)
|
||||
@ -152,14 +161,17 @@ unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
|
||||
|
||||
unsafe impl<'a, T: 'a> Pointer for &'a T {
|
||||
const BITS: usize = bits_for::<Self::Target>();
|
||||
|
||||
#[inline]
|
||||
fn into_usize(self) -> usize {
|
||||
self as *const T as usize
|
||||
}
|
||||
|
||||
#[inline]
|
||||
unsafe fn from_usize(ptr: usize) -> Self {
|
||||
&*(ptr as *const T)
|
||||
}
|
||||
|
||||
unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
|
||||
f(&*(&ptr as *const usize as *const Self))
|
||||
}
|
||||
@ -183,7 +195,7 @@ unsafe fn with_ref<R, F: FnOnce(&Self) -> R>(ptr: usize, f: F) -> R {
|
||||
/// Returns the number of bits available for use for tags in a pointer to `T`
|
||||
/// (this is based on `T`'s alignment).
|
||||
pub const fn bits_for<T>() -> usize {
|
||||
let bits = std::mem::align_of::<T>().trailing_zeros();
|
||||
let bits = mem::align_of::<T>().trailing_zeros();
|
||||
|
||||
// This is a replacement for `.try_into().unwrap()` unavailable in `const`
|
||||
// (it's fine to make an assert here, since this is only called in compile time)
|
||||
|
@ -3,6 +3,7 @@
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use std::num::NonZeroUsize;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
/// A `Copy` TaggedPtr.
|
||||
///
|
||||
@ -73,6 +74,7 @@ pub fn new(pointer: P, tag: T) -> Self {
|
||||
pub(super) fn pointer_raw(&self) -> usize {
|
||||
self.packed.get() << T::BITS
|
||||
}
|
||||
|
||||
pub fn pointer(self) -> P
|
||||
where
|
||||
P: Copy,
|
||||
@ -83,21 +85,25 @@ pub fn pointer(self) -> P
|
||||
// P: Copy
|
||||
unsafe { P::from_usize(self.pointer_raw()) }
|
||||
}
|
||||
|
||||
pub fn pointer_ref(&self) -> &P::Target {
|
||||
// SAFETY: pointer_raw returns the original pointer
|
||||
unsafe { std::mem::transmute_copy(&self.pointer_raw()) }
|
||||
}
|
||||
|
||||
pub fn pointer_mut(&mut self) -> &mut P::Target
|
||||
where
|
||||
P: std::ops::DerefMut,
|
||||
P: DerefMut,
|
||||
{
|
||||
// SAFETY: pointer_raw returns the original pointer
|
||||
unsafe { std::mem::transmute_copy(&self.pointer_raw()) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn tag(&self) -> T {
|
||||
unsafe { T::from_usize(self.packed.get() >> Self::TAG_BIT_SHIFT) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn set_tag(&mut self, tag: T) {
|
||||
let mut packed = self.packed.get();
|
||||
@ -109,20 +115,21 @@ pub fn set_tag(&mut self, tag: T) {
|
||||
}
|
||||
}
|
||||
|
||||
impl<P, T, const COMPARE_PACKED: bool> std::ops::Deref for CopyTaggedPtr<P, T, COMPARE_PACKED>
|
||||
impl<P, T, const COMPARE_PACKED: bool> Deref for CopyTaggedPtr<P, T, COMPARE_PACKED>
|
||||
where
|
||||
P: Pointer,
|
||||
T: Tag,
|
||||
{
|
||||
type Target = P::Target;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.pointer_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<P, T, const COMPARE_PACKED: bool> std::ops::DerefMut for CopyTaggedPtr<P, T, COMPARE_PACKED>
|
||||
impl<P, T, const COMPARE_PACKED: bool> DerefMut for CopyTaggedPtr<P, T, COMPARE_PACKED>
|
||||
where
|
||||
P: Pointer + std::ops::DerefMut,
|
||||
P: Pointer + DerefMut,
|
||||
T: Tag,
|
||||
{
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
|
Loading…
Reference in New Issue
Block a user