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};
|
pub use crate::zir::uint::{ShouldReduce, UExpression, UExpressionInner, UMetadata};
|
||||||
|
|
||||||
use crate::zir::types::Signature;
|
use crate::zir::types::Signature;
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use zokrates_field::Field;
|
use zokrates_field::Field;
|
||||||
|
|
@ -464,37 +463,29 @@ impl<'ast, T> BooleanExpression<'ast, T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Downcasts
|
// Downcasts
|
||||||
impl<'ast, T> TryFrom<ZirExpression<'ast, T>> for FieldElementExpression<'ast, T> {
|
impl<'ast, T> From<ZirExpression<'ast, T>> for FieldElementExpression<'ast, T> {
|
||||||
type Error = ();
|
fn from(e: ZirExpression<'ast, T>) -> FieldElementExpression<'ast, T> {
|
||||||
|
match e {
|
||||||
fn try_from(
|
ZirExpression::FieldElement(e) => e,
|
||||||
te: ZirExpression<'ast, T>,
|
_ => unreachable!("downcast failed"),
|
||||||
) -> Result<FieldElementExpression<'ast, T>, Self::Error> {
|
|
||||||
match te {
|
|
||||||
ZirExpression::FieldElement(e) => Ok(e),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, T> TryFrom<ZirExpression<'ast, T>> for BooleanExpression<'ast, T> {
|
impl<'ast, T> From<ZirExpression<'ast, T>> for BooleanExpression<'ast, T> {
|
||||||
type Error = ();
|
fn from(e: ZirExpression<'ast, T>) -> BooleanExpression<'ast, T> {
|
||||||
|
match e {
|
||||||
fn try_from(te: ZirExpression<'ast, T>) -> Result<BooleanExpression<'ast, T>, Self::Error> {
|
ZirExpression::Boolean(e) => e,
|
||||||
match te {
|
_ => unreachable!("downcast failed"),
|
||||||
ZirExpression::Boolean(e) => Ok(e),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'ast, T> TryFrom<ZirExpression<'ast, T>> for UExpression<'ast, T> {
|
impl<'ast, T> From<ZirExpression<'ast, T>> for UExpression<'ast, T> {
|
||||||
type Error = ();
|
fn from(e: ZirExpression<'ast, T>) -> UExpression<'ast, T> {
|
||||||
|
match e {
|
||||||
fn try_from(te: ZirExpression<'ast, T>) -> Result<UExpression<'ast, T>, Self::Error> {
|
ZirExpression::Uint(e) => e,
|
||||||
match te {
|
_ => unreachable!("downcast failed"),
|
||||||
ZirExpression::Uint(e) => Ok(e),
|
|
||||||
_ => Err(()),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -609,8 +600,8 @@ impl<'ast, T: fmt::Debug> fmt::Debug for ZirExpressionList<'ast, T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common behaviour accross expressions
|
// Common behaviour across expressions
|
||||||
pub trait Expr<'ast, T>: fmt::Display + PartialEq + TryFrom<ZirExpression<'ast, T>> {
|
pub trait Expr<'ast, T>: fmt::Display + PartialEq + From<ZirExpression<'ast, T>> {
|
||||||
type Inner;
|
type Inner;
|
||||||
type Ty: Clone + IntoType;
|
type Ty: Clone + IntoType;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ use std::collections::{
|
||||||
hash_map::{Entry, HashMap},
|
hash_map::{Entry, HashMap},
|
||||||
VecDeque,
|
VecDeque,
|
||||||
};
|
};
|
||||||
use std::convert::TryFrom;
|
|
||||||
use zokrates_ast::common::embed::*;
|
use zokrates_ast::common::embed::*;
|
||||||
use zokrates_ast::common::FlatEmbed;
|
use zokrates_ast::common::FlatEmbed;
|
||||||
use zokrates_ast::common::{RuntimeError, Variable};
|
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`
|
// 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>:
|
trait Flatten<'ast, T: Field>: From<ZirExpression<'ast, T>> + Conditional<'ast, T> {
|
||||||
TryFrom<ZirExpression<'ast, T>, Error = ()> + Conditional<'ast, T>
|
|
||||||
{
|
|
||||||
type Output: FlattenOutput<T>;
|
type Output: FlattenOutput<T>;
|
||||||
|
|
||||||
fn flatten(
|
fn flatten(
|
||||||
|
|
|
||||||
|
|
@ -132,12 +132,7 @@ impl<'ast, T: Field> ResultFolder<'ast, T> for ZirPropagator<'ast, T> {
|
||||||
id: IdentifierExpression<'ast, E>,
|
id: IdentifierExpression<'ast, E>,
|
||||||
) -> Result<IdentifierOrExpression<'ast, T, E>, Self::Error> {
|
) -> Result<IdentifierOrExpression<'ast, T, E>, Self::Error> {
|
||||||
match self.constants.get(&id.id).cloned() {
|
match self.constants.get(&id.id).cloned() {
|
||||||
Some(e) => Ok(IdentifierOrExpression::Expression(
|
Some(e) => Ok(IdentifierOrExpression::Expression(E::from(e).into_inner())),
|
||||||
E::try_from(e)
|
|
||||||
.map_err(|_| "downcast failed")
|
|
||||||
.unwrap()
|
|
||||||
.into_inner(),
|
|
||||||
)),
|
|
||||||
None => Ok(IdentifierOrExpression::Identifier(id)),
|
None => Ok(IdentifierOrExpression::Identifier(id)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue