Skip to content

Commit

Permalink
extract ConstantPlaceholder into a module;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Oct 9, 2020
1 parent 3e1b65c commit 982c148
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 66 deletions.
117 changes: 51 additions & 66 deletions ergo-lib/src/ast/constant.rs
@@ -1,14 +1,18 @@
use crate::chain::{Base16DecodedBytes, Base16EncodedBytes};
use crate::{
chain::ergo_box::ErgoBox,
serialization::{op_code::OpCode, SerializationError, SigmaSerializable},
serialization::{SerializationError, SigmaSerializable},
sigma_protocol::{dlog_group::EcPoint, sigma_boolean::SigmaProp},
types::{LiftIntoSType, SType},
};
#[cfg(feature = "json")]
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;

mod constant_placeholder;

pub use constant_placeholder::*;

#[derive(PartialEq, Eq, Debug, Clone)]
/// Collection for primitive values (i.e byte array)
pub enum CollPrim {
Expand Down Expand Up @@ -225,6 +229,52 @@ impl Into<Constant> for EcPoint {
}
}

/// Marker trait to select types for which CollElems::NonPrimitive is used to store elements as Vec<ConstantVal>
pub trait StoredNonPrimitive {}

impl StoredNonPrimitive for bool {}
impl StoredNonPrimitive for i16 {}
impl StoredNonPrimitive for i32 {}
impl StoredNonPrimitive for i64 {}

impl<T: LiftIntoSType + StoredNonPrimitive + Into<ConstantVal>> Into<ConstantVal> for Vec<T> {
fn into(self) -> ConstantVal {
ConstantVal::Coll(ConstantColl::NonPrimitive {
elem_tpe: T::stype(),
v: self.into_iter().map(|i| i.into()).collect(),
})
}
}

impl<T: LiftIntoSType + StoredNonPrimitive + Into<ConstantVal>> Into<Constant> for Vec<T> {
fn into(self) -> Constant {
Constant {
tpe: Self::stype(),
v: self.into(),
}
}
}

impl Into<Constant> for Vec<u8> {
fn into(self) -> Constant {
Constant {
tpe: SType::SColl(Box::new(SType::SByte)),
v: ConstantVal::Coll(ConstantColl::Primitive(CollPrim::CollByte(
self.into_iter().map(|b| b as i8).collect(),
))),
}
}
}

impl Into<Constant> for Vec<i8> {
fn into(self) -> Constant {
Constant {
tpe: SType::SColl(Box::new(SType::SByte)),
v: ConstantVal::Coll(ConstantColl::Primitive(CollPrim::CollByte(self))),
}
}
}

/// Underlying type is different from requested value type
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct TryExtractFromError(String);
Expand Down Expand Up @@ -350,71 +400,6 @@ impl TryExtractFrom<Constant> for Vec<u8> {
}
}

/// Marker trait to select types for which CollElems::NonPrimitive is used to store elements as Vec<ConstantVal>
pub trait StoredNonPrimitive {}

impl StoredNonPrimitive for bool {}
impl StoredNonPrimitive for i16 {}
impl StoredNonPrimitive for i32 {}
impl StoredNonPrimitive for i64 {}

impl<T: LiftIntoSType + StoredNonPrimitive + Into<ConstantVal>> Into<ConstantVal> for Vec<T> {
fn into(self) -> ConstantVal {
ConstantVal::Coll(ConstantColl::NonPrimitive {
elem_tpe: T::stype(),
v: self.into_iter().map(|i| i.into()).collect(),
})
}
}

impl<T: LiftIntoSType + StoredNonPrimitive + Into<ConstantVal>> Into<Constant> for Vec<T> {
fn into(self) -> Constant {
Constant {
tpe: Self::stype(),
v: self.into(),
}
}
}

impl Into<Constant> for Vec<u8> {
fn into(self) -> Constant {
Constant {
tpe: SType::SColl(Box::new(SType::SByte)),
v: ConstantVal::Coll(ConstantColl::Primitive(CollPrim::CollByte(
self.into_iter().map(|b| b as i8).collect(),
))),
}
}
}

impl Into<Constant> for Vec<i8> {
fn into(self) -> Constant {
Constant {
tpe: SType::SColl(Box::new(SType::SByte)),
v: ConstantVal::Coll(ConstantColl::Primitive(CollPrim::CollByte(self))),
}
}
}

/// Placeholder for a constant in ErgoTree.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ConstantPlaceholder {
/// Zero based index in ErgoTree.constants array.
pub id: u32,
/// Type of the constant value
pub tpe: SType,
}

impl ConstantPlaceholder {
/// OpCode value
pub const OP_CODE: OpCode = OpCode::CONSTANT_PLACEHOLDER;

/// OpCode for the serialization
pub fn op_code(&self) -> OpCode {
OpCode::CONSTANT_PLACEHOLDER
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
21 changes: 21 additions & 0 deletions ergo-lib/src/ast/constant/constant_placeholder.rs
@@ -0,0 +1,21 @@
use crate::serialization::op_code::OpCode;
use crate::types::SType;

/// Placeholder for a constant in ErgoTree.
#[derive(PartialEq, Eq, Debug, Clone)]
pub struct ConstantPlaceholder {
/// Zero based index in ErgoTree.constants array.
pub id: u32,
/// Type of the constant value
pub tpe: SType,
}

impl ConstantPlaceholder {
/// OpCode value
pub const OP_CODE: OpCode = OpCode::CONSTANT_PLACEHOLDER;

/// OpCode for the serialization
pub fn op_code(&self) -> OpCode {
OpCode::CONSTANT_PLACEHOLDER
}
}

0 comments on commit 982c148

Please sign in to comment.