Skip to content

use BTreeMap as backing store for objects #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions crates/runestick/src/object.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::collections::HashMap;
use crate::{
FromValue, InstallWith, Item, Mut, Named, RawMut, RawRef, RawStr, Ref, ToValue,
UnsafeFromValue, Value, Vm, VmError,
};
use std::borrow;
use std::cmp;
use std::collections::BTreeMap as HashMap;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe alias it to something more... neutral? :)

use std::fmt;
use std::hash;

Expand All @@ -15,7 +15,7 @@ use std::hash;
///
/// [`into_iter`]: struct.Object.html#method.into_iter
/// [`Object`]: struct.Object.html
pub type IntoIter = crate::collections::hash_map::IntoIter<String, Value>;
pub type IntoIter = std::collections::btree_map::IntoIter<String, Value>;

/// A mutable iterator over the entries of a `Object`.
///
Expand All @@ -24,7 +24,7 @@ pub type IntoIter = crate::collections::hash_map::IntoIter<String, Value>;
///
/// [`iter_mut`]: struct.Object.html#method.iter_mut
/// [`Object`]: struct.Object.html
pub type IterMut<'a> = crate::collections::hash_map::IterMut<'a, String, Value>;
pub type IterMut<'a> = std::collections::btree_map::IterMut<'a, String, Value>;

/// An iterator over the entries of a `Object`.
///
Expand All @@ -33,7 +33,7 @@ pub type IterMut<'a> = crate::collections::hash_map::IterMut<'a, String, Value>;
///
/// [`iter`]: struct.Object.html#method.iter
/// [`Object`]: struct.Object.html
pub type Iter<'a> = crate::collections::hash_map::Iter<'a, String, Value>;
pub type Iter<'a> = std::collections::btree_map::Iter<'a, String, Value>;

/// An iterator over the keys of a `HashMap`.
///
Expand All @@ -42,7 +42,7 @@ pub type Iter<'a> = crate::collections::hash_map::Iter<'a, String, Value>;
///
/// [`keys`]: struct.Object.html#method.keys
/// [`Object`]: struct.Object.html
pub type Keys<'a> = crate::collections::hash_map::Keys<'a, String, Value>;
pub type Keys<'a> = std::collections::btree_map::Keys<'a, String, Value>;

/// An iterator over the values of a `HashMap`.
///
Expand All @@ -51,7 +51,7 @@ pub type Keys<'a> = crate::collections::hash_map::Keys<'a, String, Value>;
///
/// [`values`]: struct.Object.html#method.values
/// [`Object`]: struct.Object.html
pub type Values<'a> = crate::collections::hash_map::Values<'a, String, Value>;
pub type Values<'a> = std::collections::btree_map::Values<'a, String, Value>;

/// Struct representing a dynamic anonymous object.
///
Expand Down Expand Up @@ -88,9 +88,10 @@ impl Object {

/// Construct a new object with the given capacity.
#[inline]
pub fn with_capacity(cap: usize) -> Self {
pub fn with_capacity(_cap: usize) -> Self {
/* BTreeMap doesn't support setting capacity on creation but we keep this here in case we want to switch store later */
Self {
inner: HashMap::with_capacity(cap),
inner: HashMap::new(),
}
}

Expand All @@ -111,7 +112,7 @@ impl Object {
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&Value>
where
String: borrow::Borrow<Q>,
Q: hash::Hash + cmp::Eq,
Q: hash::Hash + cmp::Eq + cmp::Ord,
{
self.inner.get(k)
}
Expand All @@ -120,7 +121,7 @@ impl Object {
pub fn get_value<Q: ?Sized, T>(&self, k: &Q) -> Result<Option<T>, VmError>
where
String: borrow::Borrow<Q>,
Q: hash::Hash + cmp::Eq,
Q: hash::Hash + cmp::Eq + cmp::Ord,
T: FromValue,
{
let value = match self.inner.get(k) {
Expand All @@ -136,7 +137,7 @@ impl Object {
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut Value>
where
String: borrow::Borrow<Q>,
Q: hash::Hash + cmp::Eq,
Q: hash::Hash + cmp::Eq + cmp::Ord,
{
self.inner.get_mut(k)
}
Expand All @@ -146,7 +147,7 @@ impl Object {
pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
where
String: borrow::Borrow<Q>,
Q: hash::Hash + cmp::Eq,
Q: hash::Hash + cmp::Eq + cmp::Ord,
{
self.inner.contains_key(k)
}
Expand All @@ -157,7 +158,7 @@ impl Object {
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<Value>
where
String: borrow::Borrow<Q>,
Q: hash::Hash + cmp::Eq,
Q: hash::Hash + cmp::Eq + cmp::Ord,
{
self.inner.remove(k)
}
Expand Down Expand Up @@ -357,7 +358,7 @@ pub(crate) fn map_ptr_eq<K>(
b: &HashMap<K, Value>,
) -> Result<bool, VmError>
where
K: cmp::Eq,
K: cmp::Eq + cmp::Ord,
K: hash::Hash,
{
if a.len() != b.len() {
Expand Down
4 changes: 2 additions & 2 deletions crates/runestick/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl Struct {
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&Value>
where
String: std::borrow::Borrow<Q>,
Q: std::hash::Hash + std::cmp::Eq,
Q: std::hash::Hash + std::cmp::Eq + std::cmp::Ord,
{
self.data.get(k)
}
Expand All @@ -130,7 +130,7 @@ impl Struct {
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut Value>
where
String: std::borrow::Borrow<Q>,
Q: std::hash::Hash + std::cmp::Eq,
Q: std::hash::Hash + std::cmp::Eq + std::cmp::Ord,
{
self.data.get_mut(k)
}
Expand Down