Flatten logic in Number::from_i128

This commit is contained in:
David Tolnay 2024-10-18 08:10:50 -07:00
parent 22973d2783
commit c3149efd62
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -314,36 +314,32 @@ impl Number {
/// ```
#[inline]
pub fn from_i128(i: i128) -> Option<Number> {
match u64::try_from(i) {
Ok(u) => {
let n = {
#[cfg(not(feature = "arbitrary_precision"))]
{
N::PosInt(u)
}
#[cfg(feature = "arbitrary_precision")]
{
u.to_string()
}
};
Some(Number { n })
}
Err(_) => match i64::try_from(i) {
Ok(i) => {
let n = {
#[cfg(not(feature = "arbitrary_precision"))]
{
N::NegInt(i)
}
#[cfg(feature = "arbitrary_precision")]
{
i.to_string()
}
};
Some(Number { n })
if let Ok(u) = u64::try_from(i) {
let n = {
#[cfg(not(feature = "arbitrary_precision"))]
{
N::PosInt(u)
}
Err(_) => None,
},
#[cfg(feature = "arbitrary_precision")]
{
u.to_string()
}
};
Some(Number { n })
} else if let Ok(i) = i64::try_from(i) {
let n = {
#[cfg(not(feature = "arbitrary_precision"))]
{
N::NegInt(i)
}
#[cfg(feature = "arbitrary_precision")]
{
i.to_string()
}
};
Some(Number { n })
} else {
None
}
}