cachepadded: turn into pub newtype

This commit is contained in:
Janis 2025-07-05 14:21:15 +02:00
parent eee2f8995a
commit c9e69c3b06

View file

@ -146,9 +146,7 @@ use core::ops::{Deref, DerefMut};
)), )),
repr(align(64)) repr(align(64))
)] )]
pub struct CachePadded<T> { pub struct CachePadded<T>(pub T);
value: T,
}
unsafe impl<T: Send> Send for CachePadded<T> {} unsafe impl<T: Send> Send for CachePadded<T> {}
unsafe impl<T: Sync> Sync for CachePadded<T> {} unsafe impl<T: Sync> Sync for CachePadded<T> {}
@ -164,7 +162,7 @@ impl<T> CachePadded<T> {
/// let padded_value = CachePadded::new(1); /// let padded_value = CachePadded::new(1);
/// ``` /// ```
pub const fn new(t: T) -> CachePadded<T> { pub const fn new(t: T) -> CachePadded<T> {
CachePadded::<T> { value: t } CachePadded::<T>(t)
} }
/// Returns the inner value. /// Returns the inner value.
@ -179,7 +177,7 @@ impl<T> CachePadded<T> {
/// assert_eq!(value, 7); /// assert_eq!(value, 7);
/// ``` /// ```
pub fn into_inner(self) -> T { pub fn into_inner(self) -> T {
self.value self.0
} }
} }
@ -187,20 +185,20 @@ impl<T> Deref for CachePadded<T> {
type Target = T; type Target = T;
fn deref(&self) -> &T { fn deref(&self) -> &T {
&self.value &self.0
} }
} }
impl<T> DerefMut for CachePadded<T> { impl<T> DerefMut for CachePadded<T> {
fn deref_mut(&mut self) -> &mut T { fn deref_mut(&mut self) -> &mut T {
&mut self.value &mut self.0
} }
} }
impl<T: fmt::Debug> fmt::Debug for CachePadded<T> { impl<T: fmt::Debug> fmt::Debug for CachePadded<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CachePadded") f.debug_struct("CachePadded")
.field("value", &self.value) .field("value", &self.0)
.finish() .finish()
} }
} }
@ -213,6 +211,6 @@ impl<T> From<T> for CachePadded<T> {
impl<T: fmt::Display> fmt::Display for CachePadded<T> { impl<T: fmt::Display> fmt::Display for CachePadded<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.value, f) fmt::Display::fmt(&self.0, f)
} }
} }