Skip to content

Commit

Permalink
feat: prefer BTreeMap to HashMap
Browse files Browse the repository at this point in the history
The latter is not nostd compliant.
  • Loading branch information
Mirko-von-Leipzig committed Jul 17, 2024
1 parent d02fffc commit 2e65e00
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
4 changes: 2 additions & 2 deletions objects/src/accounts/delta/vault.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloc::{string::ToString, vec::Vec};
use std::collections::HashMap;
use std::collections::BTreeMap;

use super::{
AccountDeltaError, Asset, ByteReader, ByteWriter, Deserializable, DeserializationError,
Expand Down Expand Up @@ -52,7 +52,7 @@ impl AccountVaultDelta {
.chain(self.removed_assets.into_iter().map(|asset| (asset, true)))
.chain(other.added_assets.into_iter().map(|asset| (asset, false)))
.chain(other.removed_assets.into_iter().map(|asset| (asset, false)))
.collect::<HashMap<_, _>>();
.collect::<BTreeMap<_, _>>();

let added = assets.iter().filter_map(|(asset, was_added)| was_added.then_some(*asset));
let removed = assets.iter().filter_map(|(asset, was_added)| (!was_added).then_some(*asset));
Expand Down
2 changes: 1 addition & 1 deletion objects/src/assets/fungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use super::{
///
/// A fungible asset consists of a faucet ID of the faucet which issued the asset as well as the
/// asset amount. Asset amount is guaranteed to be 2^63 - 1 or smaller.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct FungibleAsset {
faucet_id: AccountId,
Expand Down
2 changes: 1 addition & 1 deletion objects/src/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub use vault::AssetVault;
/// as the faucet_id is included in the description of the non-fungible asset and this is guaranteed
/// to be different as per the faucet creation logic. Collision resistance for non-fungible assets
/// issued by the same faucet is ~2^95.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Asset {
Fungible(FungibleAsset),
Expand Down
22 changes: 18 additions & 4 deletions objects/src/assets/nonfungible.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,25 @@ const FAUCET_ID_POS: usize = 1;
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct NonFungibleAsset(Word);

impl std::hash::Hash for NonFungibleAsset {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
for felt in self.0 {
felt.inner().hash(state)
impl PartialOrd for NonFungibleAsset {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
/// This wrapper allows us to use iterators builtin partial ord implementation.
/// This is much safer than attempting to do this correctly.
#[derive(PartialEq, Eq)]
struct Helper<'a>(&'a Felt);
impl PartialOrd for Helper<'_> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
self.0.inner().partial_cmp(&other.0.inner())
}
}

self.0.iter().map(Helper).partial_cmp(other.0.iter().map(Helper))
}
}

impl Ord for NonFungibleAsset {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.partial_cmp(other).expect("NonFungibleAsset should always be Orderable")
}
}

Expand Down

0 comments on commit 2e65e00

Please sign in to comment.