mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 22:41:06 +00:00
implement more convertion on NumberOrHex (#7682)
This commit is contained in:
committed by
GitHub
parent
588461f52a
commit
f6198b4c1b
@@ -18,7 +18,7 @@
|
|||||||
//! A number type that can be serialized both as a number or a string that encodes a number in a
|
//! A number type that can be serialized both as a number or a string that encodes a number in a
|
||||||
//! string.
|
//! string.
|
||||||
|
|
||||||
use std::{convert::TryFrom, fmt::Debug};
|
use std::{convert::{TryFrom, TryInto}, fmt::Debug};
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
use sp_core::U256;
|
use sp_core::U256;
|
||||||
|
|
||||||
@@ -67,24 +67,27 @@ pub struct TryFromIntError(pub(crate) ());
|
|||||||
impl TryFrom<NumberOrHex> for u32 {
|
impl TryFrom<NumberOrHex> for u32 {
|
||||||
type Error = TryFromIntError;
|
type Error = TryFromIntError;
|
||||||
fn try_from(num_or_hex: NumberOrHex) -> Result<u32, TryFromIntError> {
|
fn try_from(num_or_hex: NumberOrHex) -> Result<u32, TryFromIntError> {
|
||||||
let num_or_hex = num_or_hex.into_u256();
|
num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
|
||||||
if num_or_hex > U256::from(u32::max_value()) {
|
|
||||||
return Err(TryFromIntError(()));
|
|
||||||
} else {
|
|
||||||
Ok(num_or_hex.as_u32())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<NumberOrHex> for u64 {
|
impl TryFrom<NumberOrHex> for u64 {
|
||||||
type Error = TryFromIntError;
|
type Error = TryFromIntError;
|
||||||
fn try_from(num_or_hex: NumberOrHex) -> Result<u64, TryFromIntError> {
|
fn try_from(num_or_hex: NumberOrHex) -> Result<u64, TryFromIntError> {
|
||||||
let num_or_hex = num_or_hex.into_u256();
|
num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
|
||||||
if num_or_hex > U256::from(u64::max_value()) {
|
}
|
||||||
return Err(TryFromIntError(()));
|
}
|
||||||
} else {
|
|
||||||
Ok(num_or_hex.as_u64())
|
impl TryFrom<NumberOrHex> for u128 {
|
||||||
}
|
type Error = TryFromIntError;
|
||||||
|
fn try_from(num_or_hex: NumberOrHex) -> Result<u128, TryFromIntError> {
|
||||||
|
num_or_hex.into_u256().try_into().map_err(|_| TryFromIntError(()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<NumberOrHex> for U256 {
|
||||||
|
fn from(num_or_hex: NumberOrHex) -> U256 {
|
||||||
|
num_or_hex.into_u256()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user