Skip to content

Commit d15e624

Browse files
authored
Merge pull request #574 from Emurgo/evgenii/ordered_datum_map
Change struct behind PlutusMap
2 parents 38bbca6 + b337499 commit d15e624

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

rust/src/plutus.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
use std::hash::Hash;
12
use super::*;
23
use std::io::{BufRead, Seek, Write};
4+
use linked_hash_map::LinkedHashMap;
5+
use core::hash::Hasher;
36

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

210213
#[wasm_bindgen]
211-
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd,)]
214+
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
212215
pub struct ConstrPlutusData {
213216
alternative: BigNum,
214217
data: PlutusList,
@@ -555,15 +558,15 @@ impl Languages {
555558
}
556559

557560
#[wasm_bindgen]
558-
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd,)]
559-
pub struct PlutusMap(std::collections::BTreeMap<PlutusData, PlutusData>);
561+
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
562+
pub struct PlutusMap(LinkedHashMap<PlutusData, PlutusData>);
560563

561564
to_from_bytes!(PlutusMap);
562565

563566
#[wasm_bindgen]
564567
impl PlutusMap {
565568
pub fn new() -> Self {
566-
Self(std::collections::BTreeMap::new())
569+
Self(LinkedHashMap::new())
567570
}
568571

569572
pub fn len(&self) -> usize {
@@ -597,7 +600,7 @@ pub enum PlutusDataKind {
597600
}
598601

599602
#[derive(
600-
Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
603+
Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
601604
pub enum PlutusDataEnum {
602605
ConstrPlutusData(ConstrPlutusData),
603606
Map(PlutusMap),
@@ -621,6 +624,12 @@ impl std::cmp::PartialEq<Self> for PlutusData {
621624
}
622625
}
623626

627+
impl Hash for PlutusData {
628+
fn hash<H: Hasher>(&self, state: &mut H) {
629+
self.datum.hash(state)
630+
}
631+
}
632+
624633
impl std::cmp::Eq for PlutusData {}
625634

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

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

16251634
impl Deserialize for PlutusMap {
16261635
fn deserialize<R: BufRead + Seek>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
1627-
let mut table = std::collections::BTreeMap::new();
1636+
let mut table = LinkedHashMap::new();
16281637
(|| -> Result<_, DeserializeError> {
16291638
let len = raw.map()?;
16301639
while match len {

rust/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,7 @@ pub(crate) fn read_bounded_bytes<R: BufRead + Seek>(
881881
}
882882

883883
#[wasm_bindgen]
884-
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
884+
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
885885
pub struct BigInt(num_bigint::BigInt);
886886

887887
impl_to_from!(BigInt);

0 commit comments

Comments
 (0)