diff options
| author | Oliver Scherer <github35764891676564198441@oli-obk.de> | 2018-11-03 15:08:27 +0100 |
|---|---|---|
| committer | Oliver Scherer <github35764891676564198441@oli-obk.de> | 2018-12-04 10:17:36 +0100 |
| commit | ec6573f33b15275551af321df1a19cfd3d835982 (patch) | |
| tree | 590dc0040d174fda17b1074b19c15e5e80ab54ca /src/librustc_data_structures | |
| parent | 02b22323f129446c9e2255d0eeab6c7ab17aac52 (diff) | |
| download | rust-ec6573f33b15275551af321df1a19cfd3d835982.tar.gz rust-ec6573f33b15275551af321df1a19cfd3d835982.zip | |
Make `newtype_index` safe
Diffstat (limited to 'src/librustc_data_structures')
| -rw-r--r-- | src/librustc_data_structures/indexed_vec.rs | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/librustc_data_structures/indexed_vec.rs b/src/librustc_data_structures/indexed_vec.rs index a59bf9d530c..c35490ce35b 100644 --- a/src/librustc_data_structures/indexed_vec.rs +++ b/src/librustc_data_structures/indexed_vec.rs @@ -98,12 +98,18 @@ macro_rules! newtype_index { @max [$max:expr] @vis [$v:vis] @debug_format [$debug_format:tt]) => ( - #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] + #[derive(Copy, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)] #[rustc_layout_scalar_valid_range_end($max)] $v struct $type { private: u32 } + impl Clone for $type { + fn clone(&self) -> Self { + *self + } + } + impl $type { $v const MAX_AS_U32: u32 = $max; @@ -145,7 +151,7 @@ macro_rules! newtype_index { #[inline] $v const unsafe fn from_u32_unchecked(value: u32) -> Self { - $type { private: value } + unsafe { $type { private: value } } } /// Extract value of this index as an integer. @@ -328,12 +334,17 @@ macro_rules! newtype_index { derive [$($derives:ident,)+] $($tokens:tt)*) => ( newtype_index!( - @derives [$($derives,)+ RustcDecodable, RustcEncodable,] + @derives [$($derives,)+ RustcEncodable,] @type [$type] @max [$max] @vis [$v] @debug_format [$debug_format] $($tokens)*); + impl Decodable for $type { + fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> { + d.read_u32().into() + } + } ); // The case where no derives are added, but encodable is overridden. Don't @@ -360,12 +371,17 @@ macro_rules! newtype_index { @debug_format [$debug_format:tt] $($tokens:tt)*) => ( newtype_index!( - @derives [RustcDecodable, RustcEncodable,] + @derives [RustcEncodable,] @type [$type] @max [$max] @vis [$v] @debug_format [$debug_format] $($tokens)*); + impl Decodable for $type { + fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error> { + d.read_u32().map(Self::from) + } + } ); // Rewrite final without comma to one that includes comma |
