rewrite constants to use NewType::MAX instead of u32::MAX
Also, adjust the MAX to be `u32::MAX - 1`, leaving room for `u32::MAX` to become a sentinel value in the future.
This commit is contained in:
parent
c67d518b0d
commit
f702bd6a52
5 changed files with 35 additions and 13 deletions
|
|
@ -44,9 +44,7 @@ newtype_index! {
|
|||
}
|
||||
|
||||
impl DepNodeIndex {
|
||||
const INVALID: DepNodeIndex = unsafe {
|
||||
DepNodeIndex::from_u32_unchecked(::std::u32::MAX)
|
||||
};
|
||||
const INVALID: DepNodeIndex = DepNodeIndex::MAX;
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
|
|
|
|||
|
|
@ -27,21 +27,20 @@ newtype_index! {
|
|||
/// Virtual crate for builtin macros
|
||||
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get
|
||||
// `CrateNum`s.
|
||||
const BUILTIN_MACROS_CRATE = u32::MAX,
|
||||
const BUILTIN_MACROS_CRATE = CrateNum::MAX_AS_U32,
|
||||
|
||||
/// A CrateNum value that indicates that something is wrong.
|
||||
const INVALID_CRATE = u32::MAX - 1,
|
||||
const INVALID_CRATE = CrateNum::MAX_AS_U32 - 1,
|
||||
|
||||
/// A special CrateNum that we use for the tcx.rcache when decoding from
|
||||
/// the incr. comp. cache.
|
||||
const RESERVED_FOR_INCR_COMP_CACHE = u32::MAX - 2,
|
||||
const RESERVED_FOR_INCR_COMP_CACHE = CrateNum::MAX_AS_U32 - 2,
|
||||
}
|
||||
}
|
||||
|
||||
impl CrateNum {
|
||||
pub fn new(x: usize) -> CrateNum {
|
||||
assert!(x < (u32::MAX as usize));
|
||||
CrateNum::from_u32(x as u32)
|
||||
CrateNum::from_usize(x)
|
||||
}
|
||||
|
||||
pub fn as_def_id(&self) -> DefId { DefId { krate: *self, index: CRATE_DEF_INDEX } }
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ macro_rules! newtype_index {
|
|||
newtype_index!(
|
||||
// Leave out derives marker so we can use its absence to ensure it comes first
|
||||
@type [$name]
|
||||
@max [::std::u32::MAX]
|
||||
@max [::std::u32::MAX - 1]
|
||||
@vis [$v]
|
||||
@debug_format ["{}"]);
|
||||
);
|
||||
|
|
@ -82,7 +82,7 @@ macro_rules! newtype_index {
|
|||
newtype_index!(
|
||||
// Leave out derives marker so we can use its absence to ensure it comes first
|
||||
@type [$name]
|
||||
@max [::std::u32::MAX]
|
||||
@max [::std::u32::MAX - 1]
|
||||
@vis [$v]
|
||||
@debug_format ["{}"]
|
||||
$($tokens)+);
|
||||
|
|
@ -102,9 +102,13 @@ macro_rules! newtype_index {
|
|||
}
|
||||
|
||||
impl $type {
|
||||
$v const MAX_AS_U32: u32 = $max;
|
||||
|
||||
$v const MAX: $type = unsafe { $type::from_u32_unchecked($max) };
|
||||
|
||||
#[inline]
|
||||
$v fn from_usize(value: usize) -> Self {
|
||||
assert!(value < ($max as usize));
|
||||
assert!(value <= ($max as usize));
|
||||
unsafe {
|
||||
$type::from_u32_unchecked(value as u32)
|
||||
}
|
||||
|
|
@ -112,7 +116,7 @@ macro_rules! newtype_index {
|
|||
|
||||
#[inline]
|
||||
$v fn from_u32(value: u32) -> Self {
|
||||
assert!(value < $max);
|
||||
assert!(value <= $max);
|
||||
unsafe {
|
||||
$type::from_u32_unchecked(value)
|
||||
}
|
||||
|
|
@ -138,7 +142,7 @@ macro_rules! newtype_index {
|
|||
/// Extract value of this index as a u32.
|
||||
#[inline]
|
||||
$v const fn as_usize(self) -> usize {
|
||||
self.private as usize
|
||||
self.as_u32() as usize
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -217,6 +217,14 @@ impl_stable_hash_via_hash!(i128);
|
|||
impl_stable_hash_via_hash!(char);
|
||||
impl_stable_hash_via_hash!(());
|
||||
|
||||
impl<CTX> HashStable<CTX> for ::std::num::NonZeroU32 {
|
||||
fn hash_stable<W: StableHasherResult>(&self,
|
||||
ctx: &mut CTX,
|
||||
hasher: &mut StableHasher<W>) {
|
||||
self.get().hash_stable(ctx, hasher)
|
||||
}
|
||||
}
|
||||
|
||||
impl<CTX> HashStable<CTX> for f32 {
|
||||
fn hash_stable<W: StableHasherResult>(&self,
|
||||
ctx: &mut CTX,
|
||||
|
|
|
|||
|
|
@ -361,6 +361,18 @@ impl Decodable for u32 {
|
|||
}
|
||||
}
|
||||
|
||||
impl Encodable for ::std::num::NonZeroU32 {
|
||||
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_u32(self.get())
|
||||
}
|
||||
}
|
||||
|
||||
impl Decodable for ::std::num::NonZeroU32 {
|
||||
fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> {
|
||||
d.read_u32().map(|d| ::std::num::NonZeroU32::new(d).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for u64 {
|
||||
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
|
||||
s.emit_u64(*self)
|
||||
|
|
@ -895,3 +907,4 @@ impl<T: UseSpecializedDecodable> Decodable for T {
|
|||
impl<'a, T: ?Sized + Encodable> UseSpecializedEncodable for &'a T {}
|
||||
impl<T: ?Sized + Encodable> UseSpecializedEncodable for Box<T> {}
|
||||
impl<T: Decodable> UseSpecializedDecodable for Box<T> {}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue