Use TryFrom trait

This commit is contained in:
Wei Tang 2019-01-24 16:30:52 +01:00
parent ace89ceacb
commit e4654dc9b9
2 changed files with 93 additions and 17 deletions

View File

@ -0,0 +1,65 @@
// Copyright 2015-2019 Parity Technologies
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! Error, `TryFrom` and `TryInto` trait, should be removed when standard library stablize those.
/// Error type for conversion.
pub enum Error {
/// Overflow encountered.
Overflow,
}
/// An attempted conversion that consumes `self`, which may or may not be
/// expensive.
///
/// Library authors should not directly implement this trait, but should prefer
/// implementing the [`TryFrom`] trait, which offers greater flexibility and
/// provides an equivalent `TryInto` implementation for free, thanks to a
/// blanket implementation in the standard library. For more information on this,
/// see the documentation for [`Into`].
///
/// [`TryFrom`]: trait.TryFrom.html
/// [`Into`]: trait.Into.html
pub trait TryInto<T>: Sized {
/// The type returned in the event of a conversion error.
type Error;
/// Performs the conversion.
fn try_into(self) -> Result<T, Self::Error>;
}
/// Attempt to construct `Self` via a conversion.
pub trait TryFrom<T>: Sized {
/// The type returned in the event of a conversion error.
type Error;
/// Performs the conversion.
fn try_from(value: T) -> Result<Self, Self::Error>;
}
// TryFrom implies TryInto
impl<T, U> TryInto<U> for T where U: TryFrom<T>
{
type Error = U::Error;
fn try_into(self) -> Result<U, U::Error> {
U::try_from(self)
}
}
pub enum Never { }
// Infallible conversions are semantically equivalent to fallible conversions
// with an uninhabited error type.
impl<T, U> TryFrom<U> for T where T: From<U> {
type Error = Never;
fn try_from(value: U) -> Result<Self, Self::Error> {
Ok(T::from(value))
}
}

View File

@ -14,6 +14,10 @@
#![cfg_attr(not(feature = "std"), no_std)]
mod error;
pub use error::{Error, TryFrom, TryInto, Never};
#[macro_use]
extern crate uint;
@ -97,7 +101,6 @@ mod rlp {
impl_fixed_hash_rlp!(H512, 64);
}
impl_fixed_hash_conversions!(H256, H160);
impl U256 {
@ -121,44 +124,50 @@ impl From<U256> for U512 {
}
}
impl From<U256> for U128 {
fn from(value: U256) -> U128 {
impl TryFrom<U256> for U128 {
type Error = Error;
fn try_from(value: U256) -> Result<U128, Error> {
let U256(ref arr) = value;
if arr[2] | arr[3] != 0 {
panic!("From<U256> for U128: encountered overflow")
return Err(Error::Overflow);
}
let mut ret = [0; 2];
ret[0] = arr[0];
ret[1] = arr[1];
U128(ret)
Ok(U128(ret))
}
}
impl From<U512> for U256 {
fn from(value: U512) -> U256 {
impl TryFrom<U512> for U256 {
type Error = Error;
fn try_from(value: U512) -> Result<U256, Error> {
let U512(ref arr) = value;
if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
panic!("From<U512> for U256: encountered overflow")
return Err(Error::Overflow);
}
let mut ret = [0; 4];
ret[0] = arr[0];
ret[1] = arr[1];
ret[2] = arr[2];
ret[3] = arr[3];
U256(ret)
Ok(U256(ret))
}
}
impl From<U512> for U128 {
fn from(value: U512) -> U128 {
impl TryFrom<U512> for U128 {
type Error = Error;
fn try_from(value: U512) -> Result<U128, Error> {
let U512(ref arr) = value;
if arr[2] | arr[3] | arr[4] | arr[5] | arr[6] | arr[7] != 0 {
panic!("From<U512> for U128: encountered overflow")
return Err(Error::Overflow);
}
let mut ret = [0; 2];
ret[0] = arr[0];
ret[1] = arr[1];
U128(ret)
Ok(U128(ret))
}
}
@ -194,17 +203,19 @@ impl<'a> From<&'a U256> for U512 {
}
}
impl<'a> From<&'a U512> for U256 {
fn from(value: &'a U512) -> U256 {
impl<'a> TryFrom<&'a U512> for U256 {
type Error = Error;
fn try_from(value: &'a U512) -> Result<U256, Error> {
let U512(ref arr) = *value;
if arr[4] | arr[5] | arr[6] | arr[7] != 0 {
panic!("From<&U512> for U256: encountered overflow")
return Err(Error::Overflow);
}
let mut ret = [0; 4];
ret[0] = arr[0];
ret[1] = arr[1];
ret[2] = arr[2];
ret[3] = arr[3];
U256(ret)
Ok(U256(ret))
}
}