|
| 1 | +/// Implements [`Tag`] for a given type. |
| 2 | +/// |
| 3 | +/// You can use `impl_tag` on structs and enums. |
| 4 | +/// You need to specify the type and all its possible values, |
| 5 | +/// which can only be paths with optional fields. |
| 6 | +/// |
| 7 | +/// [`Tag`]: crate::tagged_ptr::Tag |
| 8 | +/// |
| 9 | +/// # Examples |
| 10 | +/// |
| 11 | +/// Basic usage: |
| 12 | +/// |
| 13 | +/// ``` |
| 14 | +/// use rustc_data_structures::{impl_tag, tagged_ptr::Tag}; |
| 15 | +/// |
| 16 | +/// #[derive(Copy, Clone, PartialEq, Debug)] |
| 17 | +/// enum SomeTag { |
| 18 | +/// A, |
| 19 | +/// B, |
| 20 | +/// X { v: bool }, |
| 21 | +/// Y(bool, bool), |
| 22 | +/// } |
| 23 | +/// |
| 24 | +/// impl_tag! { |
| 25 | +/// // The type for which the `Tag` will be implemented |
| 26 | +/// for SomeTag; |
| 27 | +/// // You need to specify the `{value_of_the_type} <=> {tag}` relationship |
| 28 | +/// SomeTag::A <=> 0, |
| 29 | +/// SomeTag::B <=> 1, |
| 30 | +/// // For variants with fields, you need to specify the fields: |
| 31 | +/// SomeTag::X { v: true } <=> 2, |
| 32 | +/// SomeTag::X { v: false } <=> 3, |
| 33 | +/// // For tuple variants use named syntax: |
| 34 | +/// SomeTag::Y { 0: true, 1: true } <=> 4, |
| 35 | +/// SomeTag::Y { 0: false, 1: true } <=> 5, |
| 36 | +/// SomeTag::Y { 0: true, 1: false } <=> 6, |
| 37 | +/// SomeTag::Y { 0: false, 1: false } <=> 7, |
| 38 | +/// } |
| 39 | +/// |
| 40 | +/// assert_eq!(SomeTag::A.into_usize(), 0); |
| 41 | +/// assert_eq!(SomeTag::X { v: false }.into_usize(), 3); |
| 42 | +/// assert_eq!(SomeTag::Y(false, true).into_usize(), 5); |
| 43 | +/// |
| 44 | +/// assert_eq!(unsafe { SomeTag::from_usize(1) }, SomeTag::B); |
| 45 | +/// assert_eq!(unsafe { SomeTag::from_usize(2) }, SomeTag::X { v: true }); |
| 46 | +/// assert_eq!(unsafe { SomeTag::from_usize(7) }, SomeTag::Y(false, false)); |
| 47 | +/// ``` |
| 48 | +/// |
| 49 | +/// Structs are supported: |
| 50 | +/// |
| 51 | +/// ``` |
| 52 | +/// # use rustc_data_structures::impl_tag; |
| 53 | +/// #[derive(Copy, Clone)] |
| 54 | +/// struct Flags { a: bool, b: bool } |
| 55 | +/// |
| 56 | +/// impl_tag! { |
| 57 | +/// for Flags; |
| 58 | +/// Flags { a: true, b: true } <=> 3, |
| 59 | +/// Flags { a: false, b: true } <=> 2, |
| 60 | +/// Flags { a: true, b: false } <=> 1, |
| 61 | +/// Flags { a: false, b: false } <=> 0, |
| 62 | +/// } |
| 63 | +/// ``` |
| 64 | +/// |
| 65 | +// This is supposed to produce a compile error, but does not, |
| 66 | +// see <https://github.com/rust-lang/rust/issues/110613> for more information. |
| 67 | +// |
| 68 | +// Using the same pattern twice results in a compile error: |
| 69 | +// |
| 70 | +// ```compile_fail |
| 71 | +// # use rustc_data_structures::impl_tag; |
| 72 | +// #[derive(Copy, Clone)] |
| 73 | +// struct Unit; |
| 74 | +// |
| 75 | +// impl_tag! { |
| 76 | +// for Unit; |
| 77 | +// Unit <=> 0, |
| 78 | +// Unit <=> 1, |
| 79 | +// } |
| 80 | +// ``` |
| 81 | +// |
| 82 | +// Using the same tag twice results in a compile error: |
| 83 | +// |
| 84 | +// ```compile_fail |
| 85 | +// # use rustc_data_structures::impl_tag; |
| 86 | +// #[derive(Copy, Clone)] |
| 87 | +// enum E { A, B }; |
| 88 | +// |
| 89 | +// impl_tag! { |
| 90 | +// for E; |
| 91 | +// E::A <=> 0, |
| 92 | +// E::B <=> 0, |
| 93 | +// } |
| 94 | +// ``` |
| 95 | +// |
| 96 | +/// Not specifying all values results in a compile error: |
| 97 | +/// |
| 98 | +/// ```compile_fail,E0004 |
| 99 | +/// # use rustc_data_structures::impl_tag; |
| 100 | +/// #[derive(Copy, Clone)] |
| 101 | +/// enum E { |
| 102 | +/// A, |
| 103 | +/// B, |
| 104 | +/// } |
| 105 | +/// |
| 106 | +/// impl_tag! { |
| 107 | +/// for E; |
| 108 | +/// E::A <=> 0, |
| 109 | +/// } |
| 110 | +/// ``` |
| 111 | +#[macro_export] |
| 112 | +macro_rules! impl_tag { |
| 113 | + ( |
| 114 | + for $Self:ty; |
| 115 | + $( |
| 116 | + $($path:ident)::* $( { $( $fields:tt )* })? <=> $tag:literal, |
| 117 | + )* |
| 118 | + ) => { |
| 119 | + // Safety: |
| 120 | + // `into_usize` only returns one of `$tag`s, |
| 121 | + // `bits_for_tags` is called on all `$tag`s, |
| 122 | + // thus `BITS` constant is correct. |
| 123 | + unsafe impl $crate::tagged_ptr::Tag for $Self { |
| 124 | + const BITS: u32 = $crate::tagged_ptr::bits_for_tags(&[ |
| 125 | + $( $tag, )* |
| 126 | + ]); |
| 127 | + |
| 128 | + fn into_usize(self) -> usize { |
| 129 | + // This forbids use of repeating patterns (`Enum::V`&`Enum::V`, etc) |
| 130 | + // (or at least it should, see <https://github.com/rust-lang/rust/issues/110613>) |
| 131 | + #[forbid(unreachable_patterns)] |
| 132 | + match self { |
| 133 | + // `match` is doing heavy lifting here, by requiring exhaustiveness |
| 134 | + $( |
| 135 | + $($path)::* $( { $( $fields )* } )? => $tag, |
| 136 | + )* |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + unsafe fn from_usize(tag: usize) -> Self { |
| 141 | + // Similarly to the above, this forbids repeating tags |
| 142 | + // (or at least it should, see <https://github.com/rust-lang/rust/issues/110613>) |
| 143 | + #[forbid(unreachable_patterns)] |
| 144 | + match tag { |
| 145 | + $( |
| 146 | + $tag => $($path)::* $( { $( $fields )* } )?, |
| 147 | + )* |
| 148 | + |
| 149 | + // Safety: |
| 150 | + // `into_usize` only returns one of `$tag`s, |
| 151 | + // all `$tag`s are filtered up above, |
| 152 | + // thus if this is reached, the safety contract of this |
| 153 | + // function was already breached. |
| 154 | + _ => unsafe { |
| 155 | + debug_assert!( |
| 156 | + false, |
| 157 | + "invalid tag: {tag}\ |
| 158 | + (this is a bug in the caller of `from_usize`)" |
| 159 | + ); |
| 160 | + std::hint::unreachable_unchecked() |
| 161 | + }, |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + }; |
| 167 | +} |
| 168 | + |
| 169 | +#[cfg(test)] |
| 170 | +mod tests; |
0 commit comments