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 {
245245 self . to_bits ( size)
246246 }
247247
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+
248260 // Tries to convert the `ScalarInt` to `u8`. Fails if the `size` of the `ScalarInt`
249261 // in not equal to `Size { raw: 1 }` and returns the `size` value of the `ScalarInt` in
250262 // that case.
You can’t perform that action at this time.
0 commit comments