Skip to content

Commit 98fc8c8

Browse files
authored
impl TryFrom<U256> for Chain (gakonst#1247)
1 parent 847110a commit 98fc8c8

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

ethers-core/src/types/chain.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use serde::Deserialize;
22
use thiserror::Error;
33

44
use core::convert::TryFrom;
5-
use std::{default, fmt, str::FromStr};
5+
use std::{convert::TryInto, default, fmt, str::FromStr};
66

77
use crate::types::U256;
88

@@ -128,6 +128,17 @@ impl TryFrom<u64> for Chain {
128128
}
129129
}
130130

131+
impl TryFrom<U256> for Chain {
132+
type Error = ParseChainError;
133+
134+
fn try_from(chain: U256) -> Result<Chain, Self::Error> {
135+
if chain.bits() > 64 {
136+
return Err(ParseChainError(chain.to_string()))
137+
}
138+
chain.as_u64().try_into()
139+
}
140+
}
141+
131142
impl FromStr for Chain {
132143
type Err = ParseChainError;
133144
fn from_str(chain: &str) -> Result<Self, Self::Err> {

0 commit comments

Comments
 (0)