impl TryFrom<U256> for Chain (#1247)

This commit is contained in:
Remco Bloemen 2022-05-11 07:35:30 -07:00 committed by GitHub
parent 847110a3fe
commit 98fc8c88e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 1 deletions

View File

@ -2,7 +2,7 @@ use serde::Deserialize;
use thiserror::Error; use thiserror::Error;
use core::convert::TryFrom; use core::convert::TryFrom;
use std::{default, fmt, str::FromStr}; use std::{convert::TryInto, default, fmt, str::FromStr};
use crate::types::U256; use crate::types::U256;
@ -128,6 +128,17 @@ impl TryFrom<u64> for Chain {
} }
} }
impl TryFrom<U256> for Chain {
type Error = ParseChainError;
fn try_from(chain: U256) -> Result<Chain, Self::Error> {
if chain.bits() > 64 {
return Err(ParseChainError(chain.to_string()))
}
chain.as_u64().try_into()
}
}
impl FromStr for Chain { impl FromStr for Chain {
type Err = ParseChainError; type Err = ParseChainError;
fn from_str(chain: &str) -> Result<Self, Self::Err> { fn from_str(chain: &str) -> Result<Self, Self::Err> {