Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

impl TryFrom<U256> for Chain #1247

Merged
merged 1 commit into from
May 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ethers-core/src/types/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use serde::Deserialize;
use thiserror::Error;

use core::convert::TryFrom;
use std::{default, fmt, str::FromStr};
use std::{convert::TryInto, default, fmt, str::FromStr};

use crate::types::U256;

Expand Down Expand Up @@ -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 {
type Err = ParseChainError;
fn from_str(chain: &str) -> Result<Self, Self::Err> {
Expand Down