Skip to content

Commit

Permalink
Merge pull request #574 from Emurgo/evgenii/ordered_datum_map
Browse files Browse the repository at this point in the history
Change struct behind PlutusMap
  • Loading branch information
lisicky authored Mar 3, 2023
2 parents 38bbca6 + b337499 commit d15e624
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions rust/src/plutus.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::hash::Hash;
use super::*;
use std::io::{BufRead, Seek, Write};
use linked_hash_map::LinkedHashMap;
use core::hash::Hasher;

// This library was code-generated using an experimental CDDL to rust tool:
// https://github.com/Emurgo/cddl-codegen
Expand Down Expand Up @@ -208,7 +211,7 @@ impl PlutusScripts {
}

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd,)]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct ConstrPlutusData {
alternative: BigNum,
data: PlutusList,
Expand Down Expand Up @@ -555,15 +558,15 @@ impl Languages {
}

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd,)]
pub struct PlutusMap(std::collections::BTreeMap<PlutusData, PlutusData>);
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct PlutusMap(LinkedHashMap<PlutusData, PlutusData>);

to_from_bytes!(PlutusMap);

#[wasm_bindgen]
impl PlutusMap {
pub fn new() -> Self {
Self(std::collections::BTreeMap::new())
Self(LinkedHashMap::new())
}

pub fn len(&self) -> usize {
Expand Down Expand Up @@ -597,7 +600,7 @@ pub enum PlutusDataKind {
}

#[derive(
Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum PlutusDataEnum {
ConstrPlutusData(ConstrPlutusData),
Map(PlutusMap),
Expand All @@ -621,6 +624,12 @@ impl std::cmp::PartialEq<Self> for PlutusData {
}
}

impl Hash for PlutusData {
fn hash<H: Hasher>(&self, state: &mut H) {
self.datum.hash(state)
}
}

impl std::cmp::Eq for PlutusData {}

to_from_bytes!(PlutusData);
Expand Down Expand Up @@ -758,7 +767,7 @@ impl <'de> serde::de::Deserialize<'de> for PlutusData {
}

#[wasm_bindgen]
#[derive(Clone, Debug, Ord, PartialOrd, serde::Serialize, serde::Deserialize, JsonSchema)]
#[derive(Clone, Debug, Ord, PartialOrd, Hash, serde::Serialize, serde::Deserialize, JsonSchema)]
pub struct PlutusList {
elems: Vec<PlutusData>,
// We should always preserve the original datums when deserialized as this is NOT canonicized
Expand Down Expand Up @@ -1624,7 +1633,7 @@ impl cbor_event::se::Serialize for PlutusMap {

impl Deserialize for PlutusMap {
fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
let mut table = std::collections::BTreeMap::new();
let mut table = LinkedHashMap::new();
(|| -> Result<_, DeserializeError> {
let len = raw.map()?;
while match len {
Expand Down
2 changes: 1 addition & 1 deletion rust/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ pub(crate) fn read_bounded_bytes<R: BufRead + Seek>(
}

#[wasm_bindgen]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct BigInt(num_bigint::BigInt);

impl_to_from!(BigInt);
Expand Down

0 comments on commit d15e624

Please sign in to comment.