From f028636b1aec09366973468e8c347c1cf8291561 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 11 Apr 2023 19:21:43 +0000 Subject: [PATCH] Sprinkle some whitespace & uses --- compiler/rustc_data_structures/src/tagged_ptr.rs | 16 ++++++++++++++-- .../rustc_data_structures/src/tagged_ptr/copy.rs | 15 +++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_data_structures/src/tagged_ptr.rs b/compiler/rustc_data_structures/src/tagged_ptr.rs index c96d7835b2b..e69a11dae0a 100644 --- a/compiler/rustc_data_structures/src/tagged_ptr.rs +++ b/compiler/rustc_data_structures/src/tagged_ptr.rs @@ -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 Pointer for Box { const BITS: usize = bits_for::(); + #[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>(ptr: usize, f: F) -> R { let raw = ManuallyDrop::new(Self::from_usize(ptr)); f(&raw) @@ -120,14 +123,17 @@ unsafe fn with_ref R>(ptr: usize, f: F) -> R { unsafe impl Pointer for Rc { const BITS: usize = bits_for::(); + #[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>(ptr: usize, f: F) -> R { let raw = ManuallyDrop::new(Self::from_usize(ptr)); f(&raw) @@ -136,14 +142,17 @@ unsafe fn with_ref R>(ptr: usize, f: F) -> R { unsafe impl Pointer for Arc { const BITS: usize = bits_for::(); + #[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>(ptr: usize, f: F) -> R { let raw = ManuallyDrop::new(Self::from_usize(ptr)); f(&raw) @@ -152,14 +161,17 @@ unsafe fn with_ref R>(ptr: usize, f: F) -> R { unsafe impl<'a, T: 'a> Pointer for &'a T { const BITS: usize = bits_for::(); + #[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>(ptr: usize, f: F) -> R { f(&*(&ptr as *const usize as *const Self)) } @@ -183,7 +195,7 @@ unsafe fn with_ref 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() -> usize { - let bits = std::mem::align_of::().trailing_zeros(); + let bits = mem::align_of::().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) diff --git a/compiler/rustc_data_structures/src/tagged_ptr/copy.rs b/compiler/rustc_data_structures/src/tagged_ptr/copy.rs index e1d3e0bd35a..d0c2b914584 100644 --- a/compiler/rustc_data_structures/src/tagged_ptr/copy.rs +++ b/compiler/rustc_data_structures/src/tagged_ptr/copy.rs @@ -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 std::ops::Deref for CopyTaggedPtr +impl Deref for CopyTaggedPtr where P: Pointer, T: Tag, { type Target = P::Target; + fn deref(&self) -> &Self::Target { self.pointer_ref() } } -impl std::ops::DerefMut for CopyTaggedPtr +impl DerefMut for CopyTaggedPtr where - P: Pointer + std::ops::DerefMut, + P: Pointer + DerefMut, T: Tag, { fn deref_mut(&mut self) -> &mut Self::Target {