Skip to content
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

chore: stabilize store::LookupMap and store::UnorderedMap #922

Merged
merged 5 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [Unreleased]

### Changes
- Stabilize `store::LookupMap` and `store::UnorderedMap` collections. [PR 922](https://github.com/near/near-sdk-rs/pull/922).

### Removed
- Deleted `metadata` macro. Use https://github.com/near/abi instead. [PR 920](https://github.com/near/near-sdk-rs/pull/920)

Expand Down
2 changes: 0 additions & 2 deletions near-sdk/src/store/index_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,11 @@ where
}

/// Inserts a element at `index`, returns the evicted element.
#[cfg(feature = "unstable")]
pub fn insert(&mut self, index: u32, element: T) -> Option<T> {
self.get_mut_inner(index).replace(Some(element))
}

/// Removes value at index and returns existing value.
#[allow(dead_code)]
pub fn remove(&mut self, index: u32) -> Option<T> {
self.get_mut_inner(index).replace(None)
}
Expand Down
2 changes: 1 addition & 1 deletion near-sdk/src/store/lookup_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ERR_ELEMENT_SERIALIZATION: &str = "Cannot serialize element";
/// ```
/// use near_sdk::store::LookupMap;
///
/// // Initializes a map, the generic types can be inferred to `LookupMap<String, u8, Sha256>`
/// // Initializes a map, the generic types can be inferred to `LookupMap<String, u8, Identity>`
/// // The `b"a"` parameter is a prefix for the storage keys of this data structure.
/// let mut map = LookupMap::new(b"a");
///
Expand Down
12 changes: 2 additions & 10 deletions near-sdk/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
//!
//! Maps:
//!
//! - [`LookupMap`] (`unstable`): Wrapper around key-value storage interactions, similar to
//! - [`LookupMap`]: Wrapper around key-value storage interactions, similar to
//! [`UnorderedMap`]/[`std::collections::HashMap`] except that keys are not persisted and cannot be
//! iterated over.
//!
//! - [`UnorderedMap`] (`unstable`): Storage version of [`std::collections::HashMap`]. No ordering
//! - [`UnorderedMap`]: Storage version of [`std::collections::HashMap`]. No ordering
//! guarantees.
//!
//! - [`TreeMap`] (`unstable`): Storage version of [`std::collections::BTreeMap`]. Ordered by key,
Expand Down Expand Up @@ -71,19 +71,15 @@ pub use lazy_option::LazyOption;
pub mod vec;
pub use vec::Vector;

#[cfg(feature = "unstable")]
pub mod lookup_map;
#[cfg(feature = "unstable")]
pub use self::lookup_map::LookupMap;

#[cfg(feature = "unstable")]
mod lookup_set;
#[cfg(feature = "unstable")]
pub use self::lookup_set::LookupSet;

#[cfg(feature = "unstable")]
pub mod unordered_map;
#[cfg(feature = "unstable")]
pub use self::unordered_map::UnorderedMap;

#[cfg(feature = "unstable")]
Expand All @@ -99,18 +95,14 @@ pub use self::tree_map::TreeMap;
mod index_map;
pub(crate) use self::index_map::IndexMap;

#[cfg(feature = "unstable")]
pub(crate) mod free_list;
#[cfg(feature = "unstable")]
pub(crate) use self::free_list::FreeList;

/// Storage key hash function types and trait to override map hash functions.
#[cfg(feature = "unstable")]
pub mod key;

pub(crate) const ERR_INCONSISTENT_STATE: &str =
"The collection is an inconsistent state. Did previous smart \
contract execution terminate unexpectedly?";

#[cfg(feature = "unstable")]
pub(crate) const ERR_NOT_EXIST: &str = "Key does not exist in map";
4 changes: 1 addition & 3 deletions near-sdk/src/store/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ where
/// ```
pub fn pop(&mut self) -> Option<T> {
let new_idx = self.len.checked_sub(1)?;
let prev = self.values.get_mut_inner(new_idx).replace(None);
let prev = self.values.remove(new_idx);
self.len = new_idx;
prev
}
Expand All @@ -426,8 +426,6 @@ where
///
/// assert_eq!(vec.get(0), Some(&"replaced".to_string()));
/// ```
// TODO determine if this should be stabilized, included for backwards compat with old version
#[cfg(feature = "unstable")]
pub fn replace(&mut self, index: u32, element: T) -> T {
if index >= self.len {
env::panic_str(ERR_INDEX_OUT_OF_BOUNDS);
Expand Down
2 changes: 0 additions & 2 deletions near-sdk/src/utils/stable_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#[cfg(feature = "unstable")]
use std::borrow::Borrow;
use std::cell::RefCell;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -42,7 +41,6 @@ impl<K, V> StableMap<K, V> {
pub(crate) fn inner(&mut self) -> &mut BTreeMap<K, Box<V>> {
self.map.get_mut()
}
#[cfg(feature = "unstable")]
pub(crate) fn map_value_ref<Q: ?Sized, F, T>(&self, k: &Q, f: F) -> Option<T>
where
K: Borrow<Q> + Ord,
Expand Down