Skip to content

Commit b555472

Browse files
committed
Add helper method to ScalarInt
1 parent af63e3b commit b555472

File tree

1 file changed

+12
-0
lines changed
  • compiler/rustc_middle/src/ty/consts

1 file changed

+12
-0
lines changed

compiler/rustc_middle/src/ty/consts/int.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

0 commit comments

Comments
 (0)