From cd9405a38ca7746531e8b2bc5362a07172c93f7e Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Tue, 9 Apr 2024 12:05:01 -0700 Subject: [PATCH 1/5] use btreemap for type_map --- rust/candid/src/ser.rs | 6 +++--- rust/candid/src/types/internal.rs | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/rust/candid/src/ser.rs b/rust/candid/src/ser.rs index 8da313d0..8ec0bd07 100644 --- a/rust/candid/src/ser.rs +++ b/rust/candid/src/ser.rs @@ -7,7 +7,7 @@ use super::types::value::IDLValue; use super::types::{internal::Opcode, Field, Type, TypeEnv, TypeInner}; use byteorder::{LittleEndian, WriteBytesExt}; use leb128::write::{signed as sleb128_encode, unsigned as leb128_encode}; -use std::collections::HashMap; +use std::collections::BTreeMap; use std::io; use std::vec::Vec; @@ -224,7 +224,7 @@ impl<'a> types::Compound for Compound<'a> { #[derive(Default)] pub struct TypeSerialize { type_table: Vec>, - type_map: HashMap, + type_map: BTreeMap, env: TypeEnv, args: Vec, result: Vec, @@ -235,7 +235,7 @@ impl TypeSerialize { pub fn new() -> Self { TypeSerialize { type_table: Vec::new(), - type_map: HashMap::new(), + type_map: BTreeMap::new(), env: TypeEnv::new(), args: Vec::new(), result: Vec::new(), diff --git a/rust/candid/src/types/internal.rs b/rust/candid/src/types/internal.rs index 63608c95..c678e450 100644 --- a/rust/candid/src/types/internal.rs +++ b/rust/candid/src/types/internal.rs @@ -7,7 +7,7 @@ use std::fmt; // This is a re-implementation of std::any::TypeId to get rid of 'static constraint. // The current TypeId doesn't consider lifetime while computing the hash, which is // totally fine for Candid type, as we don't care about lifetime at all. -#[derive(Debug, PartialEq, Eq, Hash, Clone)] +#[derive(Debug, PartialEq, Eq, Hash, Clone, Ord, PartialOrd)] pub struct TypeId { id: usize, pub name: &'static str, @@ -164,10 +164,10 @@ impl TypeContainer { } } -#[derive(Debug, PartialEq, Hash, Eq, Clone)] +#[derive(Debug, PartialEq, Hash, Eq, Clone, PartialOrd, Ord)] pub struct Type(pub std::rc::Rc); -#[derive(Debug, PartialEq, Hash, Eq, Clone)] +#[derive(Debug, PartialEq, Hash, Eq, Clone, PartialOrd, Ord)] pub enum TypeInner { Null, Bool, @@ -382,7 +382,7 @@ pub fn text_size(t: &Type, limit: i32) -> Result { } } -#[derive(Debug, Eq, Clone)] +#[derive(Debug, Eq, Clone, PartialOrd, Ord)] pub enum Label { Id(u32), Named(String), @@ -423,7 +423,7 @@ impl std::hash::Hash for Label { pub type SharedLabel = std::rc::Rc