diff options
| author | bors <bors@rust-lang.org> | 2021-11-14 12:17:32 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-11-14 12:17:32 +0000 |
| commit | 3b2c45441d7eefed63f6658ff8becd5a51eaeae1 (patch) | |
| tree | 6e5f3c88894b514fdb2fbd3471fd50bed4d88d76 | |
| parent | 6d387431e5457a9a3e185dc27b8416b6fcd78c88 (diff) | |
| parent | 845c25d1b445f76cd4a2c69a6ac61b85f0327626 (diff) | |
| download | rust-3b2c45441d7eefed63f6658ff8becd5a51eaeae1.tar.gz rust-3b2c45441d7eefed63f6658ff8becd5a51eaeae1.zip | |
Auto merge of #90839 - pierwill:docs-rustc-newtype-index, r=jackh726
Generate documentation in rustc `rustc_index::newtype_index` macro The macro now documents all generated items. Documentation notes possible panics and unsafety.
| -rw-r--r-- | compiler/rustc_index/src/vec.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 45639bad243..55ccfd0ad23 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -118,32 +118,54 @@ macro_rules! newtype_index { } impl $type { + /// Maximum value the index can take, as a `u32`. $v const MAX_AS_U32: u32 = $max; + /// Maximum value the index can take. $v const MAX: Self = Self::from_u32($max); + /// Creates a new index from a given `usize`. + /// + /// # Panics + /// + /// Will panic if `value` exceeds `MAX`. #[inline] $v const fn from_usize(value: usize) -> Self { assert!(value <= ($max as usize)); + // SAFETY: We just checked that `value <= max`. unsafe { Self::from_u32_unchecked(value as u32) } } + /// Creates a new index from a given `u32`. + /// + /// # Panics + /// + /// Will panic if `value` exceeds `MAX`. #[inline] $v const fn from_u32(value: u32) -> Self { assert!(value <= $max); + // SAFETY: We just checked that `value <= max`. unsafe { Self::from_u32_unchecked(value) } } + /// Creates a new index from a given `u32`. + /// + /// # Safety + /// + /// The provided value must be less than or equal to the maximum value for the newtype. + /// Providing a value outside this range is undefined due to layout restrictions. + /// + /// Prefer using `from_u32`. #[inline] $v const unsafe fn from_u32_unchecked(value: u32) -> Self { Self { private: value } } - /// Extracts the value of this index as an integer. + /// Extracts the value of this index as a `usize`. #[inline] $v const fn index(self) -> usize { self.as_usize() |
