This commit is contained in:
Oli Scherer
2025-03-05 10:07:07 +00:00
parent e3a4165363
commit 8b0e95b6de
3 changed files with 22 additions and 24 deletions
+20 -21
View File
@@ -249,11 +249,8 @@ macro_rules! int_to_int {
where where
E: Error, E: Error,
{ {
if Self::Value::MIN as i64 <= v as i64 && v as i64 <= Self::Value::MAX as i64 { Self::Value::try_from(v as i64)
Ok(v as Self::Value) .map_err(|_| Error::invalid_value(Unexpected::Signed(v as i64), &self))
} else {
Err(Error::invalid_value(Unexpected::Signed(v as i64), &self))
}
} }
}; };
@@ -262,8 +259,8 @@ macro_rules! int_to_int {
where where
E: Error, E: Error,
{ {
if $primitive::MIN as i64 <= v as i64 && v as i64 <= $primitive::MAX as i64 { if let Ok(v) = $primitive::try_from(v as i64) {
if let Some(nonzero) = Self::Value::new(v as $primitive) { if let Some(nonzero) = Self::Value::new(v) {
return Ok(nonzero); return Ok(nonzero);
} }
} }
@@ -294,12 +291,14 @@ macro_rules! int_to_uint {
where where
E: Error, E: Error,
{ {
if 0 <= v && v as u64 <= Self::Value::MAX as u64 { if 0 <= v {
Ok(v as Self::Value) #[allow(irrefutable_let_patterns)]
} else { if let Ok(v) = Self::Value::try_from(v as u64) {
Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) return Ok(v as Self::Value);
} }
} }
Err(Error::invalid_value(Unexpected::Signed(v as i64), &self))
}
}; };
(nonzero $primitive:ident $ty:ident : $visit:ident) => { (nonzero $primitive:ident $ty:ident : $visit:ident) => {
@@ -307,11 +306,14 @@ macro_rules! int_to_uint {
where where
E: Error, E: Error,
{ {
if 0 < v && v as u64 <= $primitive::MAX as u64 { if 0 < v {
if let Some(nonzero) = Self::Value::new(v as $primitive) { #[allow(irrefutable_let_patterns)]
if let Ok(v) = $primitive::try_from(v as u64) {
if let Some(nonzero) = Self::Value::new(v) {
return Ok(nonzero); return Ok(nonzero);
} }
} }
}
Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)) Err(Error::invalid_value(Unexpected::Signed(v as i64), &self))
} }
}; };
@@ -339,11 +341,8 @@ macro_rules! uint_to_self {
where where
E: Error, E: Error,
{ {
if v as u64 <= Self::Value::MAX as u64 { Self::Value::try_from(v as u64)
Ok(v as Self::Value) .map_err(|_| Error::invalid_value(Unexpected::Unsigned(v as u64), &self))
} else {
Err(Error::invalid_value(Unexpected::Unsigned(v as u64), &self))
}
} }
}; };
@@ -352,8 +351,8 @@ macro_rules! uint_to_self {
where where
E: Error, E: Error,
{ {
if v as u64 <= $primitive::MAX as u64 { if let Ok(v) = $primitive::try_from(v as u64) {
if let Some(nonzero) = Self::Value::new(v as $primitive) { if let Some(nonzero) = Self::Value::new(v) {
return Ok(nonzero); return Ok(nonzero);
} }
} }
@@ -366,7 +365,7 @@ macro_rules! uint_to_self {
where where
E: Error, E: Error,
{ {
if v as u64 <= $primitive::MAX as u64 { if let Ok(v) = $primitive::try_from(v as u64) {
Ok(Saturating(v as $primitive)) Ok(Saturating(v as $primitive))
} else { } else {
Ok(Saturating($primitive::MAX)) Ok(Saturating($primitive::MAX))
+1
View File
@@ -1,3 +1,4 @@
#[cfg(any(feature = "std", feature = "alloc"))]
use crate::lib::*; use crate::lib::*;
pub fn from_bounds<I>(iter: &I) -> Option<usize> pub fn from_bounds<I>(iter: &I) -> Option<usize>
-2
View File
@@ -175,9 +175,7 @@ mod lib {
} }
pub use self::core::{f32, f64}; pub use self::core::{f32, f64};
pub use self::core::{i16, i32, i64, i8, isize};
pub use self::core::{iter, num, ptr, str}; pub use self::core::{iter, num, ptr, str};
pub use self::core::{u16, u32, u64, u8, usize};
#[cfg(any(feature = "std", feature = "alloc"))] #[cfg(any(feature = "std", feature = "alloc"))]
pub use self::core::{cmp, mem, slice}; pub use self::core::{cmp, mem, slice};