File tree Expand file tree Collapse file tree 1 file changed +12
-0
lines changed
compiler/rustc_middle/src/ty/consts Expand file tree Collapse file tree 1 file changed +12
-0
lines changed Original file line number Diff line number Diff line change @@ -245,6 +245,18 @@ impl ScalarInt {
245
245
self . to_bits ( size)
246
246
}
247
247
248
+ // Tries to convert the `ScalarInt` to `bool`. Fails if the `size` of the `ScalarInt`
249
+ // in not equal to `Size { raw: 1 }` or if the value is not 0 or 1 and returns the `size`
250
+ // value of the `ScalarInt` in that case.
251
+ #[ inline]
252
+ pub fn try_to_bool ( self ) -> Result < bool , Size > {
253
+ match self . try_to_u8 ( ) ? {
254
+ 0 => Ok ( false ) ,
255
+ 1 => Ok ( true ) ,
256
+ _ => Err ( self . size ( ) ) ,
257
+ }
258
+ }
259
+
248
260
// Tries to convert the `ScalarInt` to `u8`. Fails if the `size` of the `ScalarInt`
249
261
// in not equal to `Size { raw: 1 }` and returns the `size` value of the `ScalarInt` in
250
262
// that case.
You can’t perform that action at this time.
0 commit comments