File tree 1 file changed +28
-5
lines changed
1 file changed +28
-5
lines changed Original file line number Diff line number Diff line change @@ -163,12 +163,35 @@ match message {
163
163
}
164
164
```
165
165
166
- It's also not allowed to cast non-exhaustive types from foreign crates.
167
- ``` rust, ignore
168
- use othercrate::NonExhaustiveEnum;
166
+ It's also not allowed to use numeric casts (` as ` ) on enums that contain any non-exhaustive variants.
167
+
168
+ For example, the following enum can be cast because it doesn't contain any non-exhaustive variants:
169
+
170
+ ``` rust
171
+ #[non_exhaustive]
172
+ pub enum Example {
173
+ First ,
174
+ Second
175
+ }
176
+ ```
177
+
178
+ However, if the enum contains even a single non-exhaustive variant, casting will result in an error. Consider this modified version of the same enum:
179
+
180
+ ``` rust
181
+ #[non_exhaustive]
182
+ pub enum EnumWithNonExhaustiveVariants {
183
+ First ,
184
+ #[non_exhaustive]
185
+ Second
186
+ }
187
+ ```
188
+
189
+ <!-- ignore: needs multiple crates -->
190
+ ``` rust,ignore
191
+ use othercrate::EnumWithNonExhaustiveVariants;
169
192
170
- // Cannot cast a non-exhaustive enum outside of its defining crate.
171
- let _ = NonExhaustiveEnum::default() as u8;
193
+ // Error: cannot cast an enum with a non-exhaustive variant when it's defined in another crate
194
+ let _ = EnumWithNonExhaustiveVariants::First as u8;
172
195
```
173
196
174
197
Non-exhaustive types are always considered inhabited in downstream crates.
You can’t perform that action at this time.
0 commit comments