implement from instead of try_from on zir expression
This commit is contained in:
parent
e51600579f
commit
b3a27bed38
3 changed files with 19 additions and 36 deletions
|
@ -15,7 +15,6 @@ use crate::typed::ConcreteType;
|
|||
pub use crate::zir::uint::{ShouldReduce, UExpression, UExpressionInner, UMetadata};
|
||||
|
||||
use crate::zir::types::Signature;
|
||||
use std::convert::TryFrom;
|
||||
use std::fmt;
|
||||
use std::marker::PhantomData;
|
||||
use zokrates_field::Field;
|
||||
|
@ -464,37 +463,29 @@ impl<'ast, T> BooleanExpression<'ast, T> {
|
|||
}
|
||||
|
||||
// Downcasts
|
||||
impl<'ast, T> TryFrom<ZirExpression<'ast, T>> for FieldElementExpression<'ast, T> {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(
|
||||
te: ZirExpression<'ast, T>,
|
||||
) -> Result<FieldElementExpression<'ast, T>, Self::Error> {
|
||||
match te {
|
||||
ZirExpression::FieldElement(e) => Ok(e),
|
||||
_ => Err(()),
|
||||
impl<'ast, T> From<ZirExpression<'ast, T>> for FieldElementExpression<'ast, T> {
|
||||
fn from(e: ZirExpression<'ast, T>) -> FieldElementExpression<'ast, T> {
|
||||
match e {
|
||||
ZirExpression::FieldElement(e) => e,
|
||||
_ => unreachable!("downcast failed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, T> TryFrom<ZirExpression<'ast, T>> for BooleanExpression<'ast, T> {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(te: ZirExpression<'ast, T>) -> Result<BooleanExpression<'ast, T>, Self::Error> {
|
||||
match te {
|
||||
ZirExpression::Boolean(e) => Ok(e),
|
||||
_ => Err(()),
|
||||
impl<'ast, T> From<ZirExpression<'ast, T>> for BooleanExpression<'ast, T> {
|
||||
fn from(e: ZirExpression<'ast, T>) -> BooleanExpression<'ast, T> {
|
||||
match e {
|
||||
ZirExpression::Boolean(e) => e,
|
||||
_ => unreachable!("downcast failed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ast, T> TryFrom<ZirExpression<'ast, T>> for UExpression<'ast, T> {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(te: ZirExpression<'ast, T>) -> Result<UExpression<'ast, T>, Self::Error> {
|
||||
match te {
|
||||
ZirExpression::Uint(e) => Ok(e),
|
||||
_ => Err(()),
|
||||
impl<'ast, T> From<ZirExpression<'ast, T>> for UExpression<'ast, T> {
|
||||
fn from(e: ZirExpression<'ast, T>) -> UExpression<'ast, T> {
|
||||
match e {
|
||||
ZirExpression::Uint(e) => e,
|
||||
_ => unreachable!("downcast failed"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -609,8 +600,8 @@ impl<'ast, T: fmt::Debug> fmt::Debug for ZirExpressionList<'ast, T> {
|
|||
}
|
||||
}
|
||||
|
||||
// Common behaviour accross expressions
|
||||
pub trait Expr<'ast, T>: fmt::Display + PartialEq + TryFrom<ZirExpression<'ast, T>> {
|
||||
// Common behaviour across expressions
|
||||
pub trait Expr<'ast, T>: fmt::Display + PartialEq + From<ZirExpression<'ast, T>> {
|
||||
type Inner;
|
||||
type Ty: Clone + IntoType;
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ use std::collections::{
|
|||
hash_map::{Entry, HashMap},
|
||||
VecDeque,
|
||||
};
|
||||
use std::convert::TryFrom;
|
||||
use zokrates_ast::common::embed::*;
|
||||
use zokrates_ast::common::FlatEmbed;
|
||||
use zokrates_ast::common::{RuntimeError, Variable};
|
||||
|
@ -119,9 +118,7 @@ impl<T: Field> FlattenOutput<T> for FlatUExpression<T> {
|
|||
|
||||
// We introduce a trait in order to make it possible to make flattening `e` generic over the type of `e`
|
||||
|
||||
trait Flatten<'ast, T: Field>:
|
||||
TryFrom<ZirExpression<'ast, T>, Error = ()> + Conditional<'ast, T>
|
||||
{
|
||||
trait Flatten<'ast, T: Field>: From<ZirExpression<'ast, T>> + Conditional<'ast, T> {
|
||||
type Output: FlattenOutput<T>;
|
||||
|
||||
fn flatten(
|
||||
|
|
|
@ -132,12 +132,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for ZirPropagator<'ast, T> {
|
|||
id: IdentifierExpression<'ast, E>,
|
||||
) -> Result<IdentifierOrExpression<'ast, T, E>, Self::Error> {
|
||||
match self.constants.get(&id.id).cloned() {
|
||||
Some(e) => Ok(IdentifierOrExpression::Expression(
|
||||
E::try_from(e)
|
||||
.map_err(|_| "downcast failed")
|
||||
.unwrap()
|
||||
.into_inner(),
|
||||
)),
|
||||
Some(e) => Ok(IdentifierOrExpression::Expression(E::from(e).into_inner())),
|
||||
None => Ok(IdentifierOrExpression::Identifier(id)),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue