Refactor cranelift immediates slightly

I've run up against the `Into`-vs-`From` impls a few times and figured
I'd go ahead and put up a refactoring. This switches `Into` impls into
`From` impls which allows using both traits instead of just the `Into`
version. Additionally this removes a few small `as` casts in favor of
infallible `from`/`into` or `try_from` with error handling.
This commit is contained in:
Alex Crichton
2021-08-06 08:18:33 -07:00
committed by Andrew Brown
parent bb85366a3b
commit ee3ff52661

View File

@@ -6,6 +6,7 @@
use alloc::vec::Vec; use alloc::vec::Vec;
use core::cmp::Ordering; use core::cmp::Ordering;
use core::convert::TryFrom;
use core::fmt::{self, Display, Formatter}; use core::fmt::{self, Display, Formatter};
use core::str::FromStr; use core::str::FromStr;
use core::{i32, u32}; use core::{i32, u32};
@@ -76,16 +77,16 @@ impl Imm64 {
return; return;
} }
let bit_width = bit_width as i64; let bit_width = i64::from(bit_width);
let delta = 64 - bit_width; let delta = 64 - bit_width;
let sign_extended = (self.0 << delta) >> delta; let sign_extended = (self.0 << delta) >> delta;
*self = Imm64(sign_extended); *self = Imm64(sign_extended);
} }
} }
impl Into<i64> for Imm64 { impl From<Imm64> for i64 {
fn into(self) -> i64 { fn from(val: Imm64) -> i64 {
self.0 val.0
} }
} }
@@ -164,9 +165,9 @@ impl Uimm64 {
} }
} }
impl Into<u64> for Uimm64 { impl From<Uimm64> for u64 {
fn into(self) -> u64 { fn from(val: Uimm64) -> u64 {
self.0 val.0
} }
} }
@@ -284,15 +285,15 @@ pub type Uimm8 = u8;
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct Uimm32(u32); pub struct Uimm32(u32);
impl Into<u32> for Uimm32 { impl From<Uimm32> for u32 {
fn into(self) -> u32 { fn from(val: Uimm32) -> u32 {
self.0 val.0
} }
} }
impl Into<i64> for Uimm32 { impl From<Uimm32> for i64 {
fn into(self) -> i64 { fn from(val: Uimm32) -> i64 {
i64::from(self.0) i64::from(val.0)
} }
} }
@@ -382,34 +383,27 @@ impl Offset32 {
/// Create a new `Offset32` representing the signed number `x` if possible. /// Create a new `Offset32` representing the signed number `x` if possible.
pub fn try_from_i64(x: i64) -> Option<Self> { pub fn try_from_i64(x: i64) -> Option<Self> {
let casted = x as i32; let x = i32::try_from(x).ok()?;
if casted as i64 == x { Some(Self::new(x))
Some(Self::new(casted))
} else {
None
}
} }
/// Add in the signed number `x` if possible. /// Add in the signed number `x` if possible.
pub fn try_add_i64(self, x: i64) -> Option<Self> { pub fn try_add_i64(self, x: i64) -> Option<Self> {
let casted = x as i32; let x = i32::try_from(x).ok()?;
if casted as i64 == x { let ret = self.0.checked_add(x)?;
self.0.checked_add(casted).map(Self::new) Some(Self::new(ret))
} else {
None
}
}
}
impl Into<i32> for Offset32 {
fn into(self) -> i32 {
self.0
} }
} }
impl Into<i64> for Offset32 { impl From<Offset32> for i32 {
fn into(self) -> i64 { fn from(val: Offset32) -> i32 {
i64::from(self.0) val.0
}
}
impl From<Offset32> for i64 {
fn from(val: Offset32) -> i64 {
i64::from(val.0)
} }
} }