提交 f702bd6a 编写于 作者: N Niko Matsakis

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.
上级 c67d518b
......@@ -44,9 +44,7 @@ pub struct DepNodeIndex { .. }
}
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 @@ pub struct CrateNum {
/// 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 @@ fn index(self) -> usize { self as usize }
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 @@ fn index(self) -> usize { self as usize }
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 @@ fn index(self) -> usize { self as usize }
}
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 @@ impl $type {
#[inline]
$v fn from_u32(value: u32) -> Self {
assert!(value < $max);
assert!(value <= $max);
unsafe {
$type::from_u32_unchecked(value)
}
......@@ -138,7 +142,7 @@ impl $type {
/// 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 @@ fn hash_stable<W: $crate::stable_hasher::StableHasherResult>(
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 @@ fn decode<D: Decoder>(d: &mut D) -> Result<u32, D::Error> {
}
}
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> {}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册