mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Give up on NonZeroUsize memory layout optimization with unstable features.
I’m not confident of the implications regarding variance, dropck, struct aliasing, etc. when this is extended to generic `*const T` and `*mut T`. Release builds should use `unstable` to enable other optimizations anyway.
This commit is contained in:
parent
3f467023fe
commit
4ee1b26b6c
1 changed files with 5 additions and 36 deletions
|
@ -40,9 +40,6 @@ mod imp {
|
||||||
|
|
||||||
#[cfg(not(feature = "unstable"))]
|
#[cfg(not(feature = "unstable"))]
|
||||||
mod imp {
|
mod imp {
|
||||||
use std::cmp;
|
|
||||||
use std::hash;
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||||
pub struct NonZeroU32(u32);
|
pub struct NonZeroU32(u32);
|
||||||
|
|
||||||
|
@ -67,14 +64,14 @@ mod imp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Eq)]
|
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
|
||||||
pub struct NonZeroUsize(&'static ());
|
pub struct NonZeroUsize(usize);
|
||||||
|
|
||||||
impl NonZeroUsize {
|
impl NonZeroUsize {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(x: usize) -> Option<Self> {
|
pub fn new(x: usize) -> Option<Self> {
|
||||||
if x != 0 {
|
if x != 0 {
|
||||||
Some(unsafe { Self::new_unchecked(x) })
|
Some(NonZeroUsize(x))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
@ -82,40 +79,12 @@ mod imp {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn new_unchecked(x: usize) -> Self {
|
pub unsafe fn new_unchecked(x: usize) -> Self {
|
||||||
NonZeroUsize(&*(x as *const ()))
|
NonZeroUsize(x)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get(self) -> usize {
|
pub fn get(self) -> usize {
|
||||||
self.0 as *const () as usize
|
self.0
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for NonZeroUsize {
|
|
||||||
#[inline]
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
self.get() == other.get()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for NonZeroUsize {
|
|
||||||
#[inline]
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
|
|
||||||
self.get().partial_cmp(&other.get())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ord for NonZeroUsize {
|
|
||||||
#[inline]
|
|
||||||
fn cmp(&self, other: &Self) -> cmp::Ordering {
|
|
||||||
self.get().cmp(&other.get())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl hash::Hash for NonZeroUsize {
|
|
||||||
#[inline]
|
|
||||||
fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
|
|
||||||
self.get().hash(hasher)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue