Commit eb3e9af3 authored by Hero Bird's avatar Hero Bird
Browse files

[pdsl_core] Replace usages of memory::HashMap in SyncChunk

The problem with memory::HashMap is that it is not compatible with no_std environments.
It requires random state which is currently not a thing in the SRML and also it might use floats internally (e.g. for fill rate) which are also currently disabled due to non-determinism.
parent d2a62542
......@@ -23,15 +23,12 @@ use crate::{
},
Allocator,
},
memory::collections::hash_map::{
HashMap,
memory::collections::btree_map::{
BTreeMap,
Entry,
},
};
#[cfg(not(feature = "std"))]
use crate::memory::collections::hash_map::DefaultHashBuilder;
use core::cell::RefCell;
/// A chunk of synchronized cells.
......@@ -56,7 +53,7 @@ pub struct SyncChunk<T> {
/// A single cache entry for a copy chunk cell.
#[cfg(not(feature = "std"))]
type CacheEntry<'a, T> = Entry<'a, u32, Option<T>, DefaultHashBuilder>;
type CacheEntry<'a, T> = Entry<'a, u32, Option<T>>;
/// A single cache entry for a copy chunk cell.
#[cfg(feature = "std")]
......@@ -224,12 +221,12 @@ where
#[derive(Debug, PartialEq, Eq)]
struct Cache<T> {
/// The synchronized values of associated cells.
elems: RefCell<HashMap<u32, Option<T>>>,
elems: RefCell<BTreeMap<u32, Option<T>>>,
}
impl<T> Default for Cache<T> {
fn default() -> Self {
Self{ elems: RefCell::new(HashMap::new()) }
Self{ elems: RefCell::new(BTreeMap::new()) }
}
}
......@@ -247,8 +244,7 @@ impl<T> Cache<T> {
///
/// Returns an immutable reference to the new value.
pub fn upsert(&self, n: u32, val: Option<T>) -> Option<&T> {
use crate::memory::collections::hash_map::Entry;
let elems: &mut HashMap<u32, Option<T>> = unsafe {
let elems: &mut BTreeMap<u32, Option<T>> = unsafe {
&mut *self.elems.as_ptr()
};
match elems.entry(n) {
......@@ -264,7 +260,7 @@ impl<T> Cache<T> {
/// Returns the synchronized value of the `n`-th cell if any.
pub fn get(&self, n: u32) -> Cached<&T> {
let elems: &mut HashMap<u32, Option<T>> = unsafe {
let elems: &mut BTreeMap<u32, Option<T>> = unsafe {
&mut *self.elems.as_ptr()
};
match elems.get(&n) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment