mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Remove in-tree bounded types and use bounded-collections crate (#13243)
* Remove in-tree bounded types and use bounded-collections crate * Fixes * Bump bounded-collections version * cargo fmt * Bump bounded-collections * Only export non-bounded types at the top level * Fixes * Bump bounded-collections
This commit is contained in:
Generated
+15
-2
@@ -699,6 +699,18 @@ version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae"
|
||||
|
||||
[[package]]
|
||||
name = "bounded-collections"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "de2aff4807e40f478132150d80b031f2461d88f061851afcab537d7600c24120"
|
||||
dependencies = [
|
||||
"log",
|
||||
"parity-scale-codec",
|
||||
"scale-info",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bs58"
|
||||
version = "0.4.0"
|
||||
@@ -6722,9 +6734,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "parity-scale-codec"
|
||||
version = "3.2.2"
|
||||
version = "3.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7ab01d0f889e957861bc65888d5ccbe82c158d0270136ba46820d43837cdf72"
|
||||
checksum = "c3840933452adf7b3b9145e27086a5a3376c619dca1a21b1e5a5af0d54979bed"
|
||||
dependencies = [
|
||||
"arrayvec 0.7.2",
|
||||
"bitvec",
|
||||
@@ -9849,6 +9861,7 @@ dependencies = [
|
||||
"base58",
|
||||
"bitflags",
|
||||
"blake2",
|
||||
"bounded-collections",
|
||||
"criterion",
|
||||
"dyn-clonable",
|
||||
"ed25519-zebra",
|
||||
|
||||
@@ -20,6 +20,7 @@ codec = { package = "parity-scale-codec", version = "3.2.2", default-features =
|
||||
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
|
||||
log = { version = "0.4.17", default-features = false }
|
||||
serde = { version = "1.0.136", optional = true, features = ["derive"] }
|
||||
bounded-collections = { version = "0.1.4", default-features = false }
|
||||
primitive-types = { version = "0.12.0", default-features = false, features = ["codec", "scale-info"] }
|
||||
impl-serde = { version = "0.4.0", optional = true }
|
||||
hash-db = { version = "0.15.2", default-features = false }
|
||||
@@ -80,6 +81,7 @@ std = [
|
||||
"thiserror",
|
||||
"lazy_static",
|
||||
"parking_lot",
|
||||
"bounded-collections/std",
|
||||
"primitive-types/std",
|
||||
"primitive-types/serde",
|
||||
"primitive-types/byteorder",
|
||||
|
||||
@@ -1,625 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Traits, types and structs to support a bounded BTreeMap.
|
||||
|
||||
use crate::{Get, TryCollect};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use sp_std::{borrow::Borrow, collections::btree_map::BTreeMap, marker::PhantomData, ops::Deref};
|
||||
|
||||
/// A bounded map based on a B-Tree.
|
||||
///
|
||||
/// B-Trees represent a fundamental compromise between cache-efficiency and actually minimizing
|
||||
/// the amount of work performed in a search. See [`BTreeMap`] for more details.
|
||||
///
|
||||
/// Unlike a standard `BTreeMap`, there is an enforced upper limit to the number of items in the
|
||||
/// map. All internal operations ensure this bound is respected.
|
||||
#[derive(Encode, scale_info::TypeInfo)]
|
||||
#[scale_info(skip_type_params(S))]
|
||||
pub struct BoundedBTreeMap<K, V, S>(BTreeMap<K, V>, PhantomData<S>);
|
||||
|
||||
impl<K, V, S> Decode for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
K: Decode + Ord,
|
||||
V: Decode,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||
let inner = BTreeMap::<K, V>::decode(input)?;
|
||||
if inner.len() > S::get() as usize {
|
||||
return Err("BoundedBTreeMap exceeds its limit".into())
|
||||
}
|
||||
Ok(Self(inner, PhantomData))
|
||||
}
|
||||
|
||||
fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
|
||||
BTreeMap::<K, V>::skip(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
S: Get<u32>,
|
||||
{
|
||||
/// Get the bound of the type in `usize`.
|
||||
pub fn bound() -> usize {
|
||||
S::get() as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
K: Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
/// Create `Self` from `t` without any checks.
|
||||
fn unchecked_from(t: BTreeMap<K, V>) -> Self {
|
||||
Self(t, Default::default())
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as `BTreeMap::retain`.
|
||||
///
|
||||
/// The is a safe `&mut self` borrow because `retain` can only ever decrease the length of the
|
||||
/// inner map.
|
||||
pub fn retain<F: FnMut(&K, &mut V) -> bool>(&mut self, f: F) {
|
||||
self.0.retain(f)
|
||||
}
|
||||
|
||||
/// Create a new `BoundedBTreeMap`.
|
||||
///
|
||||
/// Does not allocate.
|
||||
pub fn new() -> Self {
|
||||
BoundedBTreeMap(BTreeMap::new(), PhantomData)
|
||||
}
|
||||
|
||||
/// Consume self, and return the inner `BTreeMap`.
|
||||
///
|
||||
/// This is useful when a mutating API of the inner type is desired, and closure-based mutation
|
||||
/// such as provided by [`try_mutate`][Self::try_mutate] is inconvenient.
|
||||
pub fn into_inner(self) -> BTreeMap<K, V> {
|
||||
debug_assert!(self.0.len() <= Self::bound());
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Consumes self and mutates self via the given `mutate` function.
|
||||
///
|
||||
/// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is
|
||||
/// returned.
|
||||
///
|
||||
/// This is essentially a *consuming* shorthand [`Self::into_inner`] -> `...` ->
|
||||
/// [`Self::try_from`].
|
||||
pub fn try_mutate(mut self, mut mutate: impl FnMut(&mut BTreeMap<K, V>)) -> Option<Self> {
|
||||
mutate(&mut self.0);
|
||||
(self.0.len() <= Self::bound()).then(move || self)
|
||||
}
|
||||
|
||||
/// Clears the map, removing all elements.
|
||||
pub fn clear(&mut self) {
|
||||
self.0.clear()
|
||||
}
|
||||
|
||||
/// Return a mutable reference to the value corresponding to the key.
|
||||
///
|
||||
/// The key may be any borrowed form of the map's key type, but the ordering on the borrowed
|
||||
/// form _must_ match the ordering on the key type.
|
||||
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Ord + ?Sized,
|
||||
{
|
||||
self.0.get_mut(key)
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`BTreeMap::insert`], but returns an `Err` (and is a noop) if
|
||||
/// the new length of the map exceeds `S`.
|
||||
///
|
||||
/// In the `Err` case, returns the inserted pair so it can be further used without cloning.
|
||||
pub fn try_insert(&mut self, key: K, value: V) -> Result<Option<V>, (K, V)> {
|
||||
if self.len() < Self::bound() || self.0.contains_key(&key) {
|
||||
Ok(self.0.insert(key, value))
|
||||
} else {
|
||||
Err((key, value))
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove a key from the map, returning the value at the key if the key was previously in the
|
||||
/// map.
|
||||
///
|
||||
/// The key may be any borrowed form of the map's key type, but the ordering on the borrowed
|
||||
/// form _must_ match the ordering on the key type.
|
||||
pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Ord + ?Sized,
|
||||
{
|
||||
self.0.remove(key)
|
||||
}
|
||||
|
||||
/// Remove a key from the map, returning the value at the key if the key was previously in the
|
||||
/// map.
|
||||
///
|
||||
/// The key may be any borrowed form of the map's key type, but the ordering on the borrowed
|
||||
/// form _must_ match the ordering on the key type.
|
||||
pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
|
||||
where
|
||||
K: Borrow<Q>,
|
||||
Q: Ord + ?Sized,
|
||||
{
|
||||
self.0.remove_entry(key)
|
||||
}
|
||||
|
||||
/// Gets a mutable iterator over the entries of the map, sorted by key.
|
||||
///
|
||||
/// See [`BTreeMap::iter_mut`] for more information.
|
||||
pub fn iter_mut(&mut self) -> sp_std::collections::btree_map::IterMut<K, V> {
|
||||
self.0.iter_mut()
|
||||
}
|
||||
|
||||
/// Consume the map, applying `f` to each of it's values and returning a new map.
|
||||
pub fn map<T, F>(self, mut f: F) -> BoundedBTreeMap<K, T, S>
|
||||
where
|
||||
F: FnMut((&K, V)) -> T,
|
||||
{
|
||||
BoundedBTreeMap::<K, T, S>::unchecked_from(
|
||||
self.0
|
||||
.into_iter()
|
||||
.map(|(k, v)| {
|
||||
let t = f((&k, v));
|
||||
(k, t)
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Consume the map, applying `f` to each of it's values as long as it returns successfully. If
|
||||
/// an `Err(E)` is ever encountered, the mapping is short circuited and the error is returned;
|
||||
/// otherwise, a new map is returned in the contained `Ok` value.
|
||||
pub fn try_map<T, E, F>(self, mut f: F) -> Result<BoundedBTreeMap<K, T, S>, E>
|
||||
where
|
||||
F: FnMut((&K, V)) -> Result<T, E>,
|
||||
{
|
||||
Ok(BoundedBTreeMap::<K, T, S>::unchecked_from(
|
||||
self.0
|
||||
.into_iter()
|
||||
.map(|(k, v)| (f((&k, v)).map(|t| (k, t))))
|
||||
.collect::<Result<BTreeMap<_, _>, _>>()?,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Default for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
K: Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Clone for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
BTreeMap<K, V>: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
BoundedBTreeMap(self.0.clone(), PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> sp_std::fmt::Debug for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
BTreeMap<K, V>: sp_std::fmt::Debug,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
|
||||
f.debug_tuple("BoundedBTreeMap").field(&self.0).field(&Self::bound()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S1, S2> PartialEq<BoundedBTreeMap<K, V, S1>> for BoundedBTreeMap<K, V, S2>
|
||||
where
|
||||
BTreeMap<K, V>: PartialEq,
|
||||
S1: Get<u32>,
|
||||
S2: Get<u32>,
|
||||
{
|
||||
fn eq(&self, other: &BoundedBTreeMap<K, V, S1>) -> bool {
|
||||
S1::get() == S2::get() && self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Eq for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
BTreeMap<K, V>: Eq,
|
||||
S: Get<u32>,
|
||||
{
|
||||
}
|
||||
|
||||
impl<K, V, S> PartialEq<BTreeMap<K, V>> for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
BTreeMap<K, V>: PartialEq,
|
||||
{
|
||||
fn eq(&self, other: &BTreeMap<K, V>) -> bool {
|
||||
self.0 == *other
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> PartialOrd for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
BTreeMap<K, V>: PartialOrd,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn partial_cmp(&self, other: &Self) -> Option<sp_std::cmp::Ordering> {
|
||||
self.0.partial_cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Ord for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
BTreeMap<K, V>: Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn cmp(&self, other: &Self) -> sp_std::cmp::Ordering {
|
||||
self.0.cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> IntoIterator for BoundedBTreeMap<K, V, S> {
|
||||
type Item = (K, V);
|
||||
type IntoIter = sp_std::collections::btree_map::IntoIter<K, V>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K, V, S> IntoIterator for &'a BoundedBTreeMap<K, V, S> {
|
||||
type Item = (&'a K, &'a V);
|
||||
type IntoIter = sp_std::collections::btree_map::Iter<'a, K, V>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, K, V, S> IntoIterator for &'a mut BoundedBTreeMap<K, V, S> {
|
||||
type Item = (&'a K, &'a mut V);
|
||||
type IntoIter = sp_std::collections::btree_map::IterMut<'a, K, V>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.iter_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> MaxEncodedLen for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
K: MaxEncodedLen,
|
||||
V: MaxEncodedLen,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn max_encoded_len() -> usize {
|
||||
Self::bound()
|
||||
.saturating_mul(K::max_encoded_len().saturating_add(V::max_encoded_len()))
|
||||
.saturating_add(codec::Compact(S::get()).encoded_size())
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> Deref for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
K: Ord,
|
||||
{
|
||||
type Target = BTreeMap<K, V>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> AsRef<BTreeMap<K, V>> for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
K: Ord,
|
||||
{
|
||||
fn as_ref(&self) -> &BTreeMap<K, V> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> From<BoundedBTreeMap<K, V, S>> for BTreeMap<K, V>
|
||||
where
|
||||
K: Ord,
|
||||
{
|
||||
fn from(map: BoundedBTreeMap<K, V, S>) -> Self {
|
||||
map.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> TryFrom<BTreeMap<K, V>> for BoundedBTreeMap<K, V, S>
|
||||
where
|
||||
K: Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: BTreeMap<K, V>) -> Result<Self, Self::Error> {
|
||||
(value.len() <= Self::bound())
|
||||
.then(move || BoundedBTreeMap(value, PhantomData))
|
||||
.ok_or(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> codec::DecodeLength for BoundedBTreeMap<K, V, S> {
|
||||
fn len(self_encoded: &[u8]) -> Result<usize, codec::Error> {
|
||||
// `BoundedBTreeMap<K, V, S>` is stored just a `BTreeMap<K, V>`, which is stored as a
|
||||
// `Compact<u32>` with its length followed by an iteration of its items. We can just use
|
||||
// the underlying implementation.
|
||||
<BTreeMap<K, V> as codec::DecodeLength>::len(self_encoded)
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V, S> codec::EncodeLike<BTreeMap<K, V>> for BoundedBTreeMap<K, V, S> where
|
||||
BTreeMap<K, V>: Encode
|
||||
{
|
||||
}
|
||||
|
||||
impl<I, K, V, Bound> TryCollect<BoundedBTreeMap<K, V, Bound>> for I
|
||||
where
|
||||
K: Ord,
|
||||
I: ExactSizeIterator + Iterator<Item = (K, V)>,
|
||||
Bound: Get<u32>,
|
||||
{
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_collect(self) -> Result<BoundedBTreeMap<K, V, Bound>, Self::Error> {
|
||||
if self.len() > Bound::get() as usize {
|
||||
Err("iterator length too big")
|
||||
} else {
|
||||
Ok(BoundedBTreeMap::<K, V, Bound>::unchecked_from(self.collect::<BTreeMap<K, V>>()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use super::*;
|
||||
use crate::ConstU32;
|
||||
|
||||
fn map_from_keys<K>(keys: &[K]) -> BTreeMap<K, ()>
|
||||
where
|
||||
K: Ord + Copy,
|
||||
{
|
||||
keys.iter().copied().zip(std::iter::repeat(())).collect()
|
||||
}
|
||||
|
||||
fn boundedmap_from_keys<K, S>(keys: &[K]) -> BoundedBTreeMap<K, (), S>
|
||||
where
|
||||
K: Ord + Copy,
|
||||
S: Get<u32>,
|
||||
{
|
||||
map_from_keys(keys).try_into().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_insert_works() {
|
||||
let mut bounded = boundedmap_from_keys::<u32, ConstU32<4>>(&[1, 2, 3]);
|
||||
bounded.try_insert(0, ()).unwrap();
|
||||
assert_eq!(*bounded, map_from_keys(&[1, 0, 2, 3]));
|
||||
|
||||
assert!(bounded.try_insert(9, ()).is_err());
|
||||
assert_eq!(*bounded, map_from_keys(&[1, 0, 2, 3]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deref_coercion_works() {
|
||||
let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
|
||||
// these methods come from deref-ed vec.
|
||||
assert_eq!(bounded.len(), 3);
|
||||
assert!(bounded.iter().next().is_some());
|
||||
assert!(!bounded.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_mutate_works() {
|
||||
let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3, 4, 5, 6]);
|
||||
let bounded = bounded
|
||||
.try_mutate(|v| {
|
||||
v.insert(7, ());
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(bounded.len(), 7);
|
||||
assert!(bounded
|
||||
.try_mutate(|v| {
|
||||
v.insert(8, ());
|
||||
})
|
||||
.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn btree_map_eq_works() {
|
||||
let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3, 4, 5, 6]);
|
||||
assert_eq!(bounded, map_from_keys(&[1, 2, 3, 4, 5, 6]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_big_fail_to_decode() {
|
||||
let v: Vec<(u32, u32)> = vec![(1, 1), (2, 2), (3, 3), (4, 4), (5, 5)];
|
||||
assert_eq!(
|
||||
BoundedBTreeMap::<u32, u32, ConstU32<4>>::decode(&mut &v.encode()[..]),
|
||||
Err("BoundedBTreeMap exceeds its limit".into()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unequal_eq_impl_insert_works() {
|
||||
// given a struct with a strange notion of equality
|
||||
#[derive(Debug)]
|
||||
struct Unequal(u32, bool);
|
||||
|
||||
impl PartialEq for Unequal {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
impl Eq for Unequal {}
|
||||
|
||||
impl Ord for Unequal {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.0.cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Unequal {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
let mut map = BoundedBTreeMap::<Unequal, u32, ConstU32<4>>::new();
|
||||
|
||||
// when the set is full
|
||||
|
||||
for i in 0..4 {
|
||||
map.try_insert(Unequal(i, false), i).unwrap();
|
||||
}
|
||||
|
||||
// can't insert a new distinct member
|
||||
map.try_insert(Unequal(5, false), 5).unwrap_err();
|
||||
|
||||
// but _can_ insert a distinct member which compares equal, though per the documentation,
|
||||
// neither the set length nor the actual member are changed, but the value is
|
||||
map.try_insert(Unequal(0, true), 6).unwrap();
|
||||
assert_eq!(map.len(), 4);
|
||||
let (zero_key, zero_value) = map.get_key_value(&Unequal(0, true)).unwrap();
|
||||
assert_eq!(zero_key.0, 0);
|
||||
assert_eq!(zero_key.1, false);
|
||||
assert_eq!(*zero_value, 6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eq_works() {
|
||||
// of same type
|
||||
let b1 = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2]);
|
||||
let b2 = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2]);
|
||||
assert_eq!(b1, b2);
|
||||
|
||||
// of different type, but same value and bound.
|
||||
crate::parameter_types! {
|
||||
B1: u32 = 7;
|
||||
B2: u32 = 7;
|
||||
}
|
||||
let b1 = boundedmap_from_keys::<u32, B1>(&[1, 2]);
|
||||
let b2 = boundedmap_from_keys::<u32, B2>(&[1, 2]);
|
||||
assert_eq!(b1, b2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_be_collected() {
|
||||
let b1 = boundedmap_from_keys::<u32, ConstU32<5>>(&[1, 2, 3, 4]);
|
||||
let b2: BoundedBTreeMap<u32, (), ConstU32<5>> =
|
||||
b1.iter().map(|(k, v)| (k + 1, *v)).try_collect().unwrap();
|
||||
assert_eq!(b2.into_iter().map(|(k, _)| k).collect::<Vec<_>>(), vec![2, 3, 4, 5]);
|
||||
|
||||
// can also be collected into a collection of length 4.
|
||||
let b2: BoundedBTreeMap<u32, (), ConstU32<4>> =
|
||||
b1.iter().map(|(k, v)| (k + 1, *v)).try_collect().unwrap();
|
||||
assert_eq!(b2.into_iter().map(|(k, _)| k).collect::<Vec<_>>(), vec![2, 3, 4, 5]);
|
||||
|
||||
// can be mutated further into iterators that are `ExactSizedIterator`.
|
||||
let b2: BoundedBTreeMap<u32, (), ConstU32<5>> =
|
||||
b1.iter().map(|(k, v)| (k + 1, *v)).rev().skip(2).try_collect().unwrap();
|
||||
// note that the binary tree will re-sort this, so rev() is not really seen
|
||||
assert_eq!(b2.into_iter().map(|(k, _)| k).collect::<Vec<_>>(), vec![2, 3]);
|
||||
|
||||
let b2: BoundedBTreeMap<u32, (), ConstU32<5>> =
|
||||
b1.iter().map(|(k, v)| (k + 1, *v)).take(2).try_collect().unwrap();
|
||||
assert_eq!(b2.into_iter().map(|(k, _)| k).collect::<Vec<_>>(), vec![2, 3]);
|
||||
|
||||
// but these won't work
|
||||
let b2: Result<BoundedBTreeMap<u32, (), ConstU32<3>>, _> =
|
||||
b1.iter().map(|(k, v)| (k + 1, *v)).try_collect();
|
||||
assert!(b2.is_err());
|
||||
|
||||
let b2: Result<BoundedBTreeMap<u32, (), ConstU32<1>>, _> =
|
||||
b1.iter().map(|(k, v)| (k + 1, *v)).skip(2).try_collect();
|
||||
assert!(b2.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iter_mut() {
|
||||
let mut b1: BoundedBTreeMap<u8, u8, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, k)).try_collect().unwrap();
|
||||
|
||||
let b2: BoundedBTreeMap<u8, u8, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, k * 2)).try_collect().unwrap();
|
||||
|
||||
b1.iter_mut().for_each(|(_, v)| *v *= 2);
|
||||
|
||||
assert_eq!(b1, b2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_retains_size() {
|
||||
let b1 = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2]);
|
||||
let b2 = b1.clone();
|
||||
|
||||
assert_eq!(b1.len(), b2.map(|(_, _)| 5_u32).len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn map_maps_properly() {
|
||||
let b1: BoundedBTreeMap<u32, u32, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, k * 2)).try_collect().unwrap();
|
||||
let b2: BoundedBTreeMap<u32, u32, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, k)).try_collect().unwrap();
|
||||
|
||||
assert_eq!(b1, b2.map(|(_, v)| v * 2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_map_retains_size() {
|
||||
let b1 = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2]);
|
||||
let b2 = b1.clone();
|
||||
|
||||
assert_eq!(b1.len(), b2.try_map::<_, (), _>(|(_, _)| Ok(5_u32)).unwrap().len());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_map_maps_properly() {
|
||||
let b1: BoundedBTreeMap<u32, u32, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, k * 2)).try_collect().unwrap();
|
||||
let b2: BoundedBTreeMap<u32, u32, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, k)).try_collect().unwrap();
|
||||
|
||||
assert_eq!(b1, b2.try_map::<_, (), _>(|(_, v)| Ok(v * 2)).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_map_short_circuit() {
|
||||
let b1: BoundedBTreeMap<u8, u8, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, k)).try_collect().unwrap();
|
||||
|
||||
assert_eq!(Err("overflow"), b1.try_map(|(_, v)| v.checked_mul(100).ok_or("overflow")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_map_ok() {
|
||||
let b1: BoundedBTreeMap<u8, u8, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, k)).try_collect().unwrap();
|
||||
let b2: BoundedBTreeMap<u8, u16, ConstU32<7>> =
|
||||
[1, 2, 3, 4].into_iter().map(|k| (k, (k as u16) * 100)).try_collect().unwrap();
|
||||
|
||||
assert_eq!(Ok(b2), b1.try_map(|(_, v)| (v as u16).checked_mul(100_u16).ok_or("overflow")));
|
||||
}
|
||||
}
|
||||
@@ -1,482 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Traits, types and structs to support a bounded `BTreeSet`.
|
||||
|
||||
use crate::{Get, TryCollect};
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use sp_std::{borrow::Borrow, collections::btree_set::BTreeSet, marker::PhantomData, ops::Deref};
|
||||
|
||||
/// A bounded set based on a B-Tree.
|
||||
///
|
||||
/// B-Trees represent a fundamental compromise between cache-efficiency and actually minimizing
|
||||
/// the amount of work performed in a search. See [`BTreeSet`] for more details.
|
||||
///
|
||||
/// Unlike a standard `BTreeSet`, there is an enforced upper limit to the number of items in the
|
||||
/// set. All internal operations ensure this bound is respected.
|
||||
#[derive(Encode, scale_info::TypeInfo)]
|
||||
#[scale_info(skip_type_params(S))]
|
||||
pub struct BoundedBTreeSet<T, S>(BTreeSet<T>, PhantomData<S>);
|
||||
|
||||
impl<T, S> Decode for BoundedBTreeSet<T, S>
|
||||
where
|
||||
T: Decode + Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||
let inner = BTreeSet::<T>::decode(input)?;
|
||||
if inner.len() > S::get() as usize {
|
||||
return Err("BoundedBTreeSet exceeds its limit".into())
|
||||
}
|
||||
Ok(Self(inner, PhantomData))
|
||||
}
|
||||
|
||||
fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
|
||||
BTreeSet::<T>::skip(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> BoundedBTreeSet<T, S>
|
||||
where
|
||||
S: Get<u32>,
|
||||
{
|
||||
/// Get the bound of the type in `usize`.
|
||||
pub fn bound() -> usize {
|
||||
S::get() as usize
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> BoundedBTreeSet<T, S>
|
||||
where
|
||||
T: Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
/// Create `Self` from `t` without any checks.
|
||||
fn unchecked_from(t: BTreeSet<T>) -> Self {
|
||||
Self(t, Default::default())
|
||||
}
|
||||
|
||||
/// Create a new `BoundedBTreeSet`.
|
||||
///
|
||||
/// Does not allocate.
|
||||
pub fn new() -> Self {
|
||||
BoundedBTreeSet(BTreeSet::new(), PhantomData)
|
||||
}
|
||||
|
||||
/// Consume self, and return the inner `BTreeSet`.
|
||||
///
|
||||
/// This is useful when a mutating API of the inner type is desired, and closure-based mutation
|
||||
/// such as provided by [`try_mutate`][Self::try_mutate] is inconvenient.
|
||||
pub fn into_inner(self) -> BTreeSet<T> {
|
||||
debug_assert!(self.0.len() <= Self::bound());
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Consumes self and mutates self via the given `mutate` function.
|
||||
///
|
||||
/// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is
|
||||
/// returned.
|
||||
///
|
||||
/// This is essentially a *consuming* shorthand [`Self::into_inner`] -> `...` ->
|
||||
/// [`Self::try_from`].
|
||||
pub fn try_mutate(mut self, mut mutate: impl FnMut(&mut BTreeSet<T>)) -> Option<Self> {
|
||||
mutate(&mut self.0);
|
||||
(self.0.len() <= Self::bound()).then(move || self)
|
||||
}
|
||||
|
||||
/// Clears the set, removing all elements.
|
||||
pub fn clear(&mut self) {
|
||||
self.0.clear()
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`BTreeSet::insert`], but returns an `Err` (and is a noop) if
|
||||
/// the new length of the set exceeds `S`.
|
||||
///
|
||||
/// In the `Err` case, returns the inserted item so it can be further used without cloning.
|
||||
pub fn try_insert(&mut self, item: T) -> Result<bool, T> {
|
||||
if self.len() < Self::bound() || self.0.contains(&item) {
|
||||
Ok(self.0.insert(item))
|
||||
} else {
|
||||
Err(item)
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove an item from the set, returning whether it was previously in the set.
|
||||
///
|
||||
/// The item may be any borrowed form of the set's item type, but the ordering on the borrowed
|
||||
/// form _must_ match the ordering on the item type.
|
||||
pub fn remove<Q>(&mut self, item: &Q) -> bool
|
||||
where
|
||||
T: Borrow<Q>,
|
||||
Q: Ord + ?Sized,
|
||||
{
|
||||
self.0.remove(item)
|
||||
}
|
||||
|
||||
/// Removes and returns the value in the set, if any, that is equal to the given one.
|
||||
///
|
||||
/// The value may be any borrowed form of the set's value type, but the ordering on the borrowed
|
||||
/// form _must_ match the ordering on the value type.
|
||||
pub fn take<Q>(&mut self, value: &Q) -> Option<T>
|
||||
where
|
||||
T: Borrow<Q> + Ord,
|
||||
Q: Ord + ?Sized,
|
||||
{
|
||||
self.0.take(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Default for BoundedBTreeSet<T, S>
|
||||
where
|
||||
T: Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Clone for BoundedBTreeSet<T, S>
|
||||
where
|
||||
BTreeSet<T>: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
BoundedBTreeSet(self.0.clone(), PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> sp_std::fmt::Debug for BoundedBTreeSet<T, S>
|
||||
where
|
||||
BTreeSet<T>: sp_std::fmt::Debug,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
|
||||
f.debug_tuple("BoundedBTreeSet").field(&self.0).field(&Self::bound()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S1, S2> PartialEq<BoundedBTreeSet<T, S1>> for BoundedBTreeSet<T, S2>
|
||||
where
|
||||
BTreeSet<T>: PartialEq,
|
||||
S1: Get<u32>,
|
||||
S2: Get<u32>,
|
||||
{
|
||||
fn eq(&self, other: &BoundedBTreeSet<T, S1>) -> bool {
|
||||
S1::get() == S2::get() && self.0 == other.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Eq for BoundedBTreeSet<T, S>
|
||||
where
|
||||
BTreeSet<T>: Eq,
|
||||
S: Get<u32>,
|
||||
{
|
||||
}
|
||||
|
||||
impl<T, S> PartialEq<BTreeSet<T>> for BoundedBTreeSet<T, S>
|
||||
where
|
||||
BTreeSet<T>: PartialEq,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn eq(&self, other: &BTreeSet<T>) -> bool {
|
||||
self.0 == *other
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> PartialOrd for BoundedBTreeSet<T, S>
|
||||
where
|
||||
BTreeSet<T>: PartialOrd,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn partial_cmp(&self, other: &Self) -> Option<sp_std::cmp::Ordering> {
|
||||
self.0.partial_cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Ord for BoundedBTreeSet<T, S>
|
||||
where
|
||||
BTreeSet<T>: Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn cmp(&self, other: &Self) -> sp_std::cmp::Ordering {
|
||||
self.0.cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> IntoIterator for BoundedBTreeSet<T, S> {
|
||||
type Item = T;
|
||||
type IntoIter = sp_std::collections::btree_set::IntoIter<T>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> IntoIterator for &'a BoundedBTreeSet<T, S> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = sp_std::collections::btree_set::Iter<'a, T>;
|
||||
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> MaxEncodedLen for BoundedBTreeSet<T, S>
|
||||
where
|
||||
T: MaxEncodedLen,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn max_encoded_len() -> usize {
|
||||
Self::bound()
|
||||
.saturating_mul(T::max_encoded_len())
|
||||
.saturating_add(codec::Compact(S::get()).encoded_size())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Deref for BoundedBTreeSet<T, S>
|
||||
where
|
||||
T: Ord,
|
||||
{
|
||||
type Target = BTreeSet<T>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> AsRef<BTreeSet<T>> for BoundedBTreeSet<T, S>
|
||||
where
|
||||
T: Ord,
|
||||
{
|
||||
fn as_ref(&self) -> &BTreeSet<T> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> From<BoundedBTreeSet<T, S>> for BTreeSet<T>
|
||||
where
|
||||
T: Ord,
|
||||
{
|
||||
fn from(set: BoundedBTreeSet<T, S>) -> Self {
|
||||
set.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> TryFrom<BTreeSet<T>> for BoundedBTreeSet<T, S>
|
||||
where
|
||||
T: Ord,
|
||||
S: Get<u32>,
|
||||
{
|
||||
type Error = ();
|
||||
|
||||
fn try_from(value: BTreeSet<T>) -> Result<Self, Self::Error> {
|
||||
(value.len() <= Self::bound())
|
||||
.then(move || BoundedBTreeSet(value, PhantomData))
|
||||
.ok_or(())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> codec::DecodeLength for BoundedBTreeSet<T, S> {
|
||||
fn len(self_encoded: &[u8]) -> Result<usize, codec::Error> {
|
||||
// `BoundedBTreeSet<T, S>` is stored just a `BTreeSet<T>`, which is stored as a
|
||||
// `Compact<u32>` with its length followed by an iteration of its items. We can just use
|
||||
// the underlying implementation.
|
||||
<BTreeSet<T> as codec::DecodeLength>::len(self_encoded)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> codec::EncodeLike<BTreeSet<T>> for BoundedBTreeSet<T, S> where BTreeSet<T>: Encode {}
|
||||
|
||||
impl<I, T, Bound> TryCollect<BoundedBTreeSet<T, Bound>> for I
|
||||
where
|
||||
T: Ord,
|
||||
I: ExactSizeIterator + Iterator<Item = T>,
|
||||
Bound: Get<u32>,
|
||||
{
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_collect(self) -> Result<BoundedBTreeSet<T, Bound>, Self::Error> {
|
||||
if self.len() > Bound::get() as usize {
|
||||
Err("iterator length too big")
|
||||
} else {
|
||||
Ok(BoundedBTreeSet::<T, Bound>::unchecked_from(self.collect::<BTreeSet<T>>()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use super::*;
|
||||
use crate::ConstU32;
|
||||
|
||||
fn set_from_keys<T>(keys: &[T]) -> BTreeSet<T>
|
||||
where
|
||||
T: Ord + Copy,
|
||||
{
|
||||
keys.iter().copied().collect()
|
||||
}
|
||||
|
||||
fn boundedset_from_keys<T, S>(keys: &[T]) -> BoundedBTreeSet<T, S>
|
||||
where
|
||||
T: Ord + Copy,
|
||||
S: Get<u32>,
|
||||
{
|
||||
set_from_keys(keys).try_into().unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_insert_works() {
|
||||
let mut bounded = boundedset_from_keys::<u32, ConstU32<4>>(&[1, 2, 3]);
|
||||
bounded.try_insert(0).unwrap();
|
||||
assert_eq!(*bounded, set_from_keys(&[1, 0, 2, 3]));
|
||||
|
||||
assert!(bounded.try_insert(9).is_err());
|
||||
assert_eq!(*bounded, set_from_keys(&[1, 0, 2, 3]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deref_coercion_works() {
|
||||
let bounded = boundedset_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
|
||||
// these methods come from deref-ed vec.
|
||||
assert_eq!(bounded.len(), 3);
|
||||
assert!(bounded.iter().next().is_some());
|
||||
assert!(!bounded.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_mutate_works() {
|
||||
let bounded = boundedset_from_keys::<u32, ConstU32<7>>(&[1, 2, 3, 4, 5, 6]);
|
||||
let bounded = bounded
|
||||
.try_mutate(|v| {
|
||||
v.insert(7);
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(bounded.len(), 7);
|
||||
assert!(bounded
|
||||
.try_mutate(|v| {
|
||||
v.insert(8);
|
||||
})
|
||||
.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn btree_map_eq_works() {
|
||||
let bounded = boundedset_from_keys::<u32, ConstU32<7>>(&[1, 2, 3, 4, 5, 6]);
|
||||
assert_eq!(bounded, set_from_keys(&[1, 2, 3, 4, 5, 6]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_big_fail_to_decode() {
|
||||
let v: Vec<u32> = vec![1, 2, 3, 4, 5];
|
||||
assert_eq!(
|
||||
BoundedBTreeSet::<u32, ConstU32<4>>::decode(&mut &v.encode()[..]),
|
||||
Err("BoundedBTreeSet exceeds its limit".into()),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unequal_eq_impl_insert_works() {
|
||||
// given a struct with a strange notion of equality
|
||||
#[derive(Debug)]
|
||||
struct Unequal(u32, bool);
|
||||
|
||||
impl PartialEq for Unequal {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0
|
||||
}
|
||||
}
|
||||
impl Eq for Unequal {}
|
||||
|
||||
impl Ord for Unequal {
|
||||
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
|
||||
self.0.cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for Unequal {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
let mut set = BoundedBTreeSet::<Unequal, ConstU32<4>>::new();
|
||||
|
||||
// when the set is full
|
||||
|
||||
for i in 0..4 {
|
||||
set.try_insert(Unequal(i, false)).unwrap();
|
||||
}
|
||||
|
||||
// can't insert a new distinct member
|
||||
set.try_insert(Unequal(5, false)).unwrap_err();
|
||||
|
||||
// but _can_ insert a distinct member which compares equal, though per the documentation,
|
||||
// neither the set length nor the actual member are changed
|
||||
set.try_insert(Unequal(0, true)).unwrap();
|
||||
assert_eq!(set.len(), 4);
|
||||
let zero_item = set.get(&Unequal(0, true)).unwrap();
|
||||
assert_eq!(zero_item.0, 0);
|
||||
assert_eq!(zero_item.1, false);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn eq_works() {
|
||||
// of same type
|
||||
let b1 = boundedset_from_keys::<u32, ConstU32<7>>(&[1, 2]);
|
||||
let b2 = boundedset_from_keys::<u32, ConstU32<7>>(&[1, 2]);
|
||||
assert_eq!(b1, b2);
|
||||
|
||||
// of different type, but same value and bound.
|
||||
crate::parameter_types! {
|
||||
B1: u32 = 7;
|
||||
B2: u32 = 7;
|
||||
}
|
||||
let b1 = boundedset_from_keys::<u32, B1>(&[1, 2]);
|
||||
let b2 = boundedset_from_keys::<u32, B2>(&[1, 2]);
|
||||
assert_eq!(b1, b2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_be_collected() {
|
||||
let b1 = boundedset_from_keys::<u32, ConstU32<5>>(&[1, 2, 3, 4]);
|
||||
let b2: BoundedBTreeSet<u32, ConstU32<5>> = b1.iter().map(|k| k + 1).try_collect().unwrap();
|
||||
assert_eq!(b2.into_iter().collect::<Vec<_>>(), vec![2, 3, 4, 5]);
|
||||
|
||||
// can also be collected into a collection of length 4.
|
||||
let b2: BoundedBTreeSet<u32, ConstU32<4>> = b1.iter().map(|k| k + 1).try_collect().unwrap();
|
||||
assert_eq!(b2.into_iter().collect::<Vec<_>>(), vec![2, 3, 4, 5]);
|
||||
|
||||
// can be mutated further into iterators that are `ExactSizedIterator`.
|
||||
let b2: BoundedBTreeSet<u32, ConstU32<5>> =
|
||||
b1.iter().map(|k| k + 1).rev().skip(2).try_collect().unwrap();
|
||||
// note that the binary tree will re-sort this, so rev() is not really seen
|
||||
assert_eq!(b2.into_iter().collect::<Vec<_>>(), vec![2, 3]);
|
||||
|
||||
let b2: BoundedBTreeSet<u32, ConstU32<5>> =
|
||||
b1.iter().map(|k| k + 1).take(2).try_collect().unwrap();
|
||||
assert_eq!(b2.into_iter().collect::<Vec<_>>(), vec![2, 3]);
|
||||
|
||||
// but these worn't work
|
||||
let b2: Result<BoundedBTreeSet<u32, ConstU32<3>>, _> =
|
||||
b1.iter().map(|k| k + 1).try_collect();
|
||||
assert!(b2.is_err());
|
||||
|
||||
let b2: Result<BoundedBTreeSet<u32, ConstU32<1>>, _> =
|
||||
b1.iter().map(|k| k + 1).skip(2).try_collect();
|
||||
assert!(b2.is_err());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,524 +0,0 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Traits, types and structs to support putting a bounded vector into storage, as a raw value, map
|
||||
//! or a double map.
|
||||
|
||||
use super::{BoundedSlice, BoundedVec};
|
||||
use crate::Get;
|
||||
use codec::{Decode, Encode, MaxEncodedLen};
|
||||
use core::{
|
||||
ops::{Deref, Index, IndexMut},
|
||||
slice::SliceIndex,
|
||||
};
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{
|
||||
de::{Error, SeqAccess, Visitor},
|
||||
Deserialize, Deserializer, Serialize,
|
||||
};
|
||||
use sp_std::{marker::PhantomData, prelude::*};
|
||||
|
||||
/// A weakly bounded vector.
|
||||
///
|
||||
/// It has implementations for efficient append and length decoding, as with a normal `Vec<_>`, once
|
||||
/// put into storage as a raw value, map or double-map.
|
||||
///
|
||||
/// The length of the vec is not strictly bounded. Decoding a vec with more element that the bound
|
||||
/// is accepted, and some method allow to bypass the restriction with warnings.
|
||||
#[cfg_attr(feature = "std", derive(Serialize), serde(transparent))]
|
||||
#[derive(Encode, scale_info::TypeInfo)]
|
||||
#[scale_info(skip_type_params(S))]
|
||||
pub struct WeakBoundedVec<T, S>(
|
||||
pub(super) Vec<T>,
|
||||
#[cfg_attr(feature = "std", serde(skip_serializing))] PhantomData<S>,
|
||||
);
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<'de, T, S: Get<u32>> Deserialize<'de> for WeakBoundedVec<T, S>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
struct VecVisitor<T, S: Get<u32>>(PhantomData<(T, S)>);
|
||||
|
||||
impl<'de, T, S: Get<u32>> Visitor<'de> for VecVisitor<T, S>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
{
|
||||
type Value = Vec<T>;
|
||||
|
||||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
formatter.write_str("a sequence")
|
||||
}
|
||||
|
||||
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
|
||||
where
|
||||
A: SeqAccess<'de>,
|
||||
{
|
||||
let size = seq.size_hint().unwrap_or(0);
|
||||
let max = match usize::try_from(S::get()) {
|
||||
Ok(n) => n,
|
||||
Err(_) => return Err(A::Error::custom("can't convert to usize")),
|
||||
};
|
||||
if size > max {
|
||||
log::warn!(
|
||||
target: "runtime",
|
||||
"length of a bounded vector while deserializing is not respected.",
|
||||
);
|
||||
}
|
||||
let mut values = Vec::with_capacity(size);
|
||||
|
||||
while let Some(value) = seq.next_element()? {
|
||||
values.push(value);
|
||||
if values.len() > max {
|
||||
log::warn!(
|
||||
target: "runtime",
|
||||
"length of a bounded vector while deserializing is not respected.",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(values)
|
||||
}
|
||||
}
|
||||
|
||||
let visitor: VecVisitor<T, S> = VecVisitor(PhantomData);
|
||||
deserializer.deserialize_seq(visitor).map(|v| {
|
||||
WeakBoundedVec::<T, S>::try_from(v).map_err(|_| Error::custom("out of bounds"))
|
||||
})?
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Decode, S: Get<u32>> Decode for WeakBoundedVec<T, S> {
|
||||
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
|
||||
let inner = Vec::<T>::decode(input)?;
|
||||
Ok(Self::force_from(inner, Some("decode")))
|
||||
}
|
||||
|
||||
fn skip<I: codec::Input>(input: &mut I) -> Result<(), codec::Error> {
|
||||
Vec::<T>::skip(input)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> WeakBoundedVec<T, S> {
|
||||
/// Create `Self` from `t` without any checks.
|
||||
fn unchecked_from(t: Vec<T>) -> Self {
|
||||
Self(t, Default::default())
|
||||
}
|
||||
|
||||
/// Consume self, and return the inner `Vec`. Henceforth, the `Vec<_>` can be altered in an
|
||||
/// arbitrary way. At some point, if the reverse conversion is required, `TryFrom<Vec<_>>` can
|
||||
/// be used.
|
||||
///
|
||||
/// This is useful for cases if you need access to an internal API of the inner `Vec<_>` which
|
||||
/// is not provided by the wrapper `WeakBoundedVec`.
|
||||
pub fn into_inner(self) -> Vec<T> {
|
||||
self.0
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::remove`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `index` is out of bounds.
|
||||
pub fn remove(&mut self, index: usize) -> T {
|
||||
self.0.remove(index)
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::swap_remove`].
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `index` is out of bounds.
|
||||
pub fn swap_remove(&mut self, index: usize) -> T {
|
||||
self.0.swap_remove(index)
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::retain`].
|
||||
pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F) {
|
||||
self.0.retain(f)
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`slice::get_mut`].
|
||||
pub fn get_mut<I: SliceIndex<[T]>>(
|
||||
&mut self,
|
||||
index: I,
|
||||
) -> Option<&mut <I as SliceIndex<[T]>>::Output> {
|
||||
self.0.get_mut(index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: Get<u32>> WeakBoundedVec<T, S> {
|
||||
/// Get the bound of the type in `usize`.
|
||||
pub fn bound() -> usize {
|
||||
S::get() as usize
|
||||
}
|
||||
|
||||
/// Create `Self` from `t` without any checks. Logs warnings if the bound is not being
|
||||
/// respected. The additional scope can be used to indicate where a potential overflow is
|
||||
/// happening.
|
||||
pub fn force_from(t: Vec<T>, scope: Option<&'static str>) -> Self {
|
||||
if t.len() > Self::bound() {
|
||||
log::warn!(
|
||||
target: "runtime",
|
||||
"length of a bounded vector in scope {} is not respected.",
|
||||
scope.unwrap_or("UNKNOWN"),
|
||||
);
|
||||
}
|
||||
|
||||
Self::unchecked_from(t)
|
||||
}
|
||||
|
||||
/// Consumes self and mutates self via the given `mutate` function.
|
||||
///
|
||||
/// If the outcome of mutation is within bounds, `Some(Self)` is returned. Else, `None` is
|
||||
/// returned.
|
||||
///
|
||||
/// This is essentially a *consuming* shorthand [`Self::into_inner`] -> `...` ->
|
||||
/// [`Self::try_from`].
|
||||
pub fn try_mutate(mut self, mut mutate: impl FnMut(&mut Vec<T>)) -> Option<Self> {
|
||||
mutate(&mut self.0);
|
||||
(self.0.len() <= Self::bound()).then(move || self)
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::insert`], but returns an `Err` (and is a noop) if the
|
||||
/// new length of the vector exceeds `S`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if `index > len`.
|
||||
pub fn try_insert(&mut self, index: usize, element: T) -> Result<(), ()> {
|
||||
if self.len() < Self::bound() {
|
||||
self.0.insert(index, element);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Exactly the same semantics as [`Vec::push`], but returns an `Err` (and is a noop) if the
|
||||
/// new length of the vector exceeds `S`.
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// Panics if the new capacity exceeds isize::MAX bytes.
|
||||
pub fn try_push(&mut self, element: T) -> Result<(), ()> {
|
||||
if self.len() < Self::bound() {
|
||||
self.0.push(element);
|
||||
Ok(())
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Default for WeakBoundedVec<T, S> {
|
||||
fn default() -> Self {
|
||||
// the bound cannot be below 0, which is satisfied by an empty vector
|
||||
Self::unchecked_from(Vec::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> sp_std::fmt::Debug for WeakBoundedVec<T, S>
|
||||
where
|
||||
Vec<T>: sp_std::fmt::Debug,
|
||||
S: Get<u32>,
|
||||
{
|
||||
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
|
||||
f.debug_tuple("WeakBoundedVec").field(&self.0).field(&Self::bound()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> Clone for WeakBoundedVec<T, S>
|
||||
where
|
||||
T: Clone,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
// bound is retained
|
||||
Self::unchecked_from(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: Get<u32>> TryFrom<Vec<T>> for WeakBoundedVec<T, S> {
|
||||
type Error = ();
|
||||
fn try_from(t: Vec<T>) -> Result<Self, Self::Error> {
|
||||
if t.len() <= Self::bound() {
|
||||
// explicit check just above
|
||||
Ok(Self::unchecked_from(t))
|
||||
} else {
|
||||
Err(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// It is okay to give a non-mutable reference of the inner vec to anyone.
|
||||
impl<T, S> AsRef<Vec<T>> for WeakBoundedVec<T, S> {
|
||||
fn as_ref(&self) -> &Vec<T> {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> AsRef<[T]> for WeakBoundedVec<T, S> {
|
||||
fn as_ref(&self) -> &[T] {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> AsMut<[T]> for WeakBoundedVec<T, S> {
|
||||
fn as_mut(&mut self) -> &mut [T] {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
// will allow for immutable all operations of `Vec<T>` on `WeakBoundedVec<T>`.
|
||||
impl<T, S> Deref for WeakBoundedVec<T, S> {
|
||||
type Target = Vec<T>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
// Allows for indexing similar to a normal `Vec`. Can panic if out of bound.
|
||||
impl<T, S, I> Index<I> for WeakBoundedVec<T, S>
|
||||
where
|
||||
I: SliceIndex<[T]>,
|
||||
{
|
||||
type Output = I::Output;
|
||||
|
||||
#[inline]
|
||||
fn index(&self, index: I) -> &Self::Output {
|
||||
self.0.index(index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S, I> IndexMut<I> for WeakBoundedVec<T, S>
|
||||
where
|
||||
I: SliceIndex<[T]>,
|
||||
{
|
||||
#[inline]
|
||||
fn index_mut(&mut self, index: I) -> &mut Self::Output {
|
||||
self.0.index_mut(index)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> sp_std::iter::IntoIterator for WeakBoundedVec<T, S> {
|
||||
type Item = T;
|
||||
type IntoIter = sp_std::vec::IntoIter<T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> sp_std::iter::IntoIterator for &'a WeakBoundedVec<T, S> {
|
||||
type Item = &'a T;
|
||||
type IntoIter = sp_std::slice::Iter<'a, T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, S> sp_std::iter::IntoIterator for &'a mut WeakBoundedVec<T, S> {
|
||||
type Item = &'a mut T;
|
||||
type IntoIter = sp_std::slice::IterMut<'a, T>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.iter_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> codec::DecodeLength for WeakBoundedVec<T, S> {
|
||||
fn len(self_encoded: &[u8]) -> Result<usize, codec::Error> {
|
||||
// `WeakBoundedVec<T, _>` stored just a `Vec<T>`, thus the length is at the beginning in
|
||||
// `Compact` form, and same implementation as `Vec<T>` can be used.
|
||||
<Vec<T> as codec::DecodeLength>::len(self_encoded)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, BoundSelf, BoundRhs> PartialEq<WeakBoundedVec<T, BoundRhs>> for WeakBoundedVec<T, BoundSelf>
|
||||
where
|
||||
T: PartialEq,
|
||||
BoundSelf: Get<u32>,
|
||||
BoundRhs: Get<u32>,
|
||||
{
|
||||
fn eq(&self, rhs: &WeakBoundedVec<T, BoundRhs>) -> bool {
|
||||
self.0 == rhs.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, BoundSelf, BoundRhs> PartialEq<BoundedVec<T, BoundRhs>> for WeakBoundedVec<T, BoundSelf>
|
||||
where
|
||||
T: PartialEq,
|
||||
BoundSelf: Get<u32>,
|
||||
BoundRhs: Get<u32>,
|
||||
{
|
||||
fn eq(&self, rhs: &BoundedVec<T, BoundRhs>) -> bool {
|
||||
self.0 == rhs.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, BoundSelf, BoundRhs> PartialEq<BoundedSlice<'a, T, BoundRhs>>
|
||||
for WeakBoundedVec<T, BoundSelf>
|
||||
where
|
||||
T: PartialEq,
|
||||
BoundSelf: Get<u32>,
|
||||
BoundRhs: Get<u32>,
|
||||
{
|
||||
fn eq(&self, rhs: &BoundedSlice<'a, T, BoundRhs>) -> bool {
|
||||
self.0 == rhs.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: PartialEq, S: Get<u32>> PartialEq<Vec<T>> for WeakBoundedVec<T, S> {
|
||||
fn eq(&self, other: &Vec<T>) -> bool {
|
||||
&self.0 == other
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S: Get<u32>> Eq for WeakBoundedVec<T, S> where T: Eq {}
|
||||
|
||||
impl<T, BoundSelf, BoundRhs> PartialOrd<WeakBoundedVec<T, BoundRhs>>
|
||||
for WeakBoundedVec<T, BoundSelf>
|
||||
where
|
||||
T: PartialOrd,
|
||||
BoundSelf: Get<u32>,
|
||||
BoundRhs: Get<u32>,
|
||||
{
|
||||
fn partial_cmp(&self, other: &WeakBoundedVec<T, BoundRhs>) -> Option<sp_std::cmp::Ordering> {
|
||||
self.0.partial_cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, BoundSelf, BoundRhs> PartialOrd<BoundedVec<T, BoundRhs>> for WeakBoundedVec<T, BoundSelf>
|
||||
where
|
||||
T: PartialOrd,
|
||||
BoundSelf: Get<u32>,
|
||||
BoundRhs: Get<u32>,
|
||||
{
|
||||
fn partial_cmp(&self, other: &BoundedVec<T, BoundRhs>) -> Option<sp_std::cmp::Ordering> {
|
||||
self.0.partial_cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T, BoundSelf, BoundRhs> PartialOrd<BoundedSlice<'a, T, BoundRhs>>
|
||||
for WeakBoundedVec<T, BoundSelf>
|
||||
where
|
||||
T: PartialOrd,
|
||||
BoundSelf: Get<u32>,
|
||||
BoundRhs: Get<u32>,
|
||||
{
|
||||
fn partial_cmp(&self, other: &BoundedSlice<'a, T, BoundRhs>) -> Option<sp_std::cmp::Ordering> {
|
||||
(&*self.0).partial_cmp(other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Ord, S: Get<u32>> Ord for WeakBoundedVec<T, S> {
|
||||
fn cmp(&self, other: &Self) -> sp_std::cmp::Ordering {
|
||||
self.0.cmp(&other.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, S> MaxEncodedLen for WeakBoundedVec<T, S>
|
||||
where
|
||||
T: MaxEncodedLen,
|
||||
S: Get<u32>,
|
||||
WeakBoundedVec<T, S>: Encode,
|
||||
{
|
||||
fn max_encoded_len() -> usize {
|
||||
// WeakBoundedVec<T, S> encodes like Vec<T> which encodes like [T], which is a compact u32
|
||||
// plus each item in the slice:
|
||||
// See: https://docs.substrate.io/reference/scale-codec/
|
||||
codec::Compact(S::get())
|
||||
.encoded_size()
|
||||
.saturating_add(Self::bound().saturating_mul(T::max_encoded_len()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod test {
|
||||
use super::*;
|
||||
use crate::ConstU32;
|
||||
|
||||
#[test]
|
||||
fn bound_returns_correct_value() {
|
||||
assert_eq!(WeakBoundedVec::<u32, ConstU32<7>>::bound(), 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_insert_works() {
|
||||
let mut bounded: WeakBoundedVec<u32, ConstU32<4>> = vec![1, 2, 3].try_into().unwrap();
|
||||
bounded.try_insert(1, 0).unwrap();
|
||||
assert_eq!(*bounded, vec![1, 0, 2, 3]);
|
||||
|
||||
assert!(bounded.try_insert(0, 9).is_err());
|
||||
assert_eq!(*bounded, vec![1, 0, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "insertion index (is 9) should be <= len (is 3)")]
|
||||
fn try_inert_panics_if_oob() {
|
||||
let mut bounded: WeakBoundedVec<u32, ConstU32<4>> = vec![1, 2, 3].try_into().unwrap();
|
||||
bounded.try_insert(9, 0).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_push_works() {
|
||||
let mut bounded: WeakBoundedVec<u32, ConstU32<4>> = vec![1, 2, 3].try_into().unwrap();
|
||||
bounded.try_push(0).unwrap();
|
||||
assert_eq!(*bounded, vec![1, 2, 3, 0]);
|
||||
|
||||
assert!(bounded.try_push(9).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deref_coercion_works() {
|
||||
let bounded: WeakBoundedVec<u32, ConstU32<7>> = vec![1, 2, 3].try_into().unwrap();
|
||||
// these methods come from deref-ed vec.
|
||||
assert_eq!(bounded.len(), 3);
|
||||
assert!(bounded.iter().next().is_some());
|
||||
assert!(!bounded.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn try_mutate_works() {
|
||||
let bounded: WeakBoundedVec<u32, ConstU32<7>> = vec![1, 2, 3, 4, 5, 6].try_into().unwrap();
|
||||
let bounded = bounded.try_mutate(|v| v.push(7)).unwrap();
|
||||
assert_eq!(bounded.len(), 7);
|
||||
assert!(bounded.try_mutate(|v| v.push(8)).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slice_indexing_works() {
|
||||
let bounded: WeakBoundedVec<u32, ConstU32<7>> = vec![1, 2, 3, 4, 5, 6].try_into().unwrap();
|
||||
assert_eq!(&bounded[0..=2], &[1, 2, 3]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn vec_eq_works() {
|
||||
let bounded: WeakBoundedVec<u32, ConstU32<7>> = vec![1, 2, 3, 4, 5, 6].try_into().unwrap();
|
||||
assert_eq!(bounded, vec![1, 2, 3, 4, 5, 6]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn too_big_succeed_to_decode() {
|
||||
let v: Vec<u32> = vec![1, 2, 3, 4, 5];
|
||||
let w = WeakBoundedVec::<u32, ConstU32<4>>::decode(&mut &v.encode()[..]).unwrap();
|
||||
assert_eq!(v, *w);
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,6 @@ pub mod hashing;
|
||||
|
||||
#[cfg(feature = "full_crypto")]
|
||||
pub use hashing::{blake2_128, blake2_256, keccak_256, twox_128, twox_256, twox_64};
|
||||
pub mod bounded;
|
||||
pub mod crypto;
|
||||
pub mod hexdisplay;
|
||||
|
||||
@@ -81,6 +80,13 @@ pub use self::hasher::blake2::Blake2Hasher;
|
||||
pub use self::hasher::keccak::KeccakHasher;
|
||||
pub use hash_db::Hasher;
|
||||
|
||||
pub use bounded_collections as bounded;
|
||||
#[cfg(feature = "std")]
|
||||
pub use bounded_collections::{bounded_btree_map, bounded_vec};
|
||||
pub use bounded_collections::{
|
||||
parameter_types, ConstBool, ConstI128, ConstI16, ConstI32, ConstI64, ConstI8, ConstU128,
|
||||
ConstU16, ConstU32, ConstU64, ConstU8, Get, GetDefault, TryCollect, TypedGet,
|
||||
};
|
||||
pub use sp_storage as storage;
|
||||
|
||||
#[doc(hidden)]
|
||||
@@ -387,242 +393,6 @@ macro_rules! impl_maybe_marker {
|
||||
// everybody.
|
||||
pub const MAX_POSSIBLE_ALLOCATION: u32 = 33554432; // 2^25 bytes, 32 MiB
|
||||
|
||||
/// A trait for querying a single value from a type defined in the trait.
|
||||
///
|
||||
/// It is not required that the value is constant.
|
||||
pub trait TypedGet {
|
||||
/// The type which is returned.
|
||||
type Type;
|
||||
/// Return the current value.
|
||||
fn get() -> Self::Type;
|
||||
}
|
||||
|
||||
/// A trait for querying a single value from a type.
|
||||
///
|
||||
/// It is not required that the value is constant.
|
||||
pub trait Get<T> {
|
||||
/// Return the current value.
|
||||
fn get() -> T;
|
||||
}
|
||||
|
||||
impl<T: Default> Get<T> for () {
|
||||
fn get() -> T {
|
||||
T::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement Get by returning Default for any type that implements Default.
|
||||
pub struct GetDefault;
|
||||
impl<T: Default> Get<T> for GetDefault {
|
||||
fn get() -> T {
|
||||
T::default()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_const_get {
|
||||
($name:ident, $t:ty) => {
|
||||
#[doc = "Const getter for a basic type."]
|
||||
#[derive($crate::RuntimeDebug)]
|
||||
pub struct $name<const T: $t>;
|
||||
impl<const T: $t> Get<$t> for $name<T> {
|
||||
fn get() -> $t {
|
||||
T
|
||||
}
|
||||
}
|
||||
impl<const T: $t> Get<Option<$t>> for $name<T> {
|
||||
fn get() -> Option<$t> {
|
||||
Some(T)
|
||||
}
|
||||
}
|
||||
impl<const T: $t> TypedGet for $name<T> {
|
||||
type Type = $t;
|
||||
fn get() -> $t {
|
||||
T
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_const_get!(ConstBool, bool);
|
||||
impl_const_get!(ConstU8, u8);
|
||||
impl_const_get!(ConstU16, u16);
|
||||
impl_const_get!(ConstU32, u32);
|
||||
impl_const_get!(ConstU64, u64);
|
||||
impl_const_get!(ConstU128, u128);
|
||||
impl_const_get!(ConstI8, i8);
|
||||
impl_const_get!(ConstI16, i16);
|
||||
impl_const_get!(ConstI32, i32);
|
||||
impl_const_get!(ConstI64, i64);
|
||||
impl_const_get!(ConstI128, i128);
|
||||
|
||||
/// Try and collect into a collection `C`.
|
||||
pub trait TryCollect<C> {
|
||||
/// The error type that gets returned when a collection can't be made from `self`.
|
||||
type Error;
|
||||
/// Consume self and try to collect the results into `C`.
|
||||
///
|
||||
/// This is useful in preventing the undesirable `.collect().try_into()` call chain on
|
||||
/// collections that need to be converted into a bounded type (e.g. `BoundedVec`).
|
||||
fn try_collect(self) -> Result<C, Self::Error>;
|
||||
}
|
||||
|
||||
/// Create new implementations of the [`Get`](crate::Get) trait.
|
||||
///
|
||||
/// The so-called parameter type can be created in four different ways:
|
||||
///
|
||||
/// - Using `const` to create a parameter type that provides a `const` getter. It is required that
|
||||
/// the `value` is const.
|
||||
///
|
||||
/// - Declare the parameter type without `const` to have more freedom when creating the value.
|
||||
///
|
||||
/// NOTE: A more substantial version of this macro is available in `frame_support` crate which
|
||||
/// allows mutable and persistant variants.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use sp_core::Get;
|
||||
/// # use sp_core::parameter_types;
|
||||
/// // This function cannot be used in a const context.
|
||||
/// fn non_const_expression() -> u64 { 99 }
|
||||
///
|
||||
/// const FIXED_VALUE: u64 = 10;
|
||||
/// parameter_types! {
|
||||
/// pub const Argument: u64 = 42 + FIXED_VALUE;
|
||||
/// /// Visibility of the type is optional
|
||||
/// OtherArgument: u64 = non_const_expression();
|
||||
/// }
|
||||
///
|
||||
/// trait Config {
|
||||
/// type Parameter: Get<u64>;
|
||||
/// type OtherParameter: Get<u64>;
|
||||
/// }
|
||||
///
|
||||
/// struct Runtime;
|
||||
/// impl Config for Runtime {
|
||||
/// type Parameter = Argument;
|
||||
/// type OtherParameter = OtherArgument;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// # Invalid example:
|
||||
///
|
||||
/// ```compile_fail
|
||||
/// # use sp_core::Get;
|
||||
/// # use sp_core::parameter_types;
|
||||
/// // This function cannot be used in a const context.
|
||||
/// fn non_const_expression() -> u64 { 99 }
|
||||
///
|
||||
/// parameter_types! {
|
||||
/// pub const Argument: u64 = non_const_expression();
|
||||
/// }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! parameter_types {
|
||||
(
|
||||
$( #[ $attr:meta ] )*
|
||||
$vis:vis const $name:ident: $type:ty = $value:expr;
|
||||
$( $rest:tt )*
|
||||
) => (
|
||||
$( #[ $attr ] )*
|
||||
$vis struct $name;
|
||||
$crate::parameter_types!(@IMPL_CONST $name , $type , $value);
|
||||
$crate::parameter_types!( $( $rest )* );
|
||||
);
|
||||
(
|
||||
$( #[ $attr:meta ] )*
|
||||
$vis:vis $name:ident: $type:ty = $value:expr;
|
||||
$( $rest:tt )*
|
||||
) => (
|
||||
$( #[ $attr ] )*
|
||||
$vis struct $name;
|
||||
$crate::parameter_types!(@IMPL $name, $type, $value);
|
||||
$crate::parameter_types!( $( $rest )* );
|
||||
);
|
||||
() => ();
|
||||
(@IMPL_CONST $name:ident, $type:ty, $value:expr) => {
|
||||
impl $name {
|
||||
/// Returns the value of this parameter type.
|
||||
pub const fn get() -> $type {
|
||||
$value
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: From<$type>> $crate::Get<I> for $name {
|
||||
fn get() -> I {
|
||||
I::from(Self::get())
|
||||
}
|
||||
}
|
||||
|
||||
impl $crate::TypedGet for $name {
|
||||
type Type = $type;
|
||||
fn get() -> $type {
|
||||
Self::get()
|
||||
}
|
||||
}
|
||||
};
|
||||
(@IMPL $name:ident, $type:ty, $value:expr) => {
|
||||
impl $name {
|
||||
/// Returns the value of this parameter type.
|
||||
pub fn get() -> $type {
|
||||
$value
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: From<$type>> $crate::Get<I> for $name {
|
||||
fn get() -> I {
|
||||
I::from(Self::get())
|
||||
}
|
||||
}
|
||||
|
||||
impl $crate::TypedGet for $name {
|
||||
type Type = $type;
|
||||
fn get() -> $type {
|
||||
Self::get()
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Build a bounded vec from the given literals.
|
||||
///
|
||||
/// The type of the outcome must be known.
|
||||
///
|
||||
/// Will not handle any errors and just panic if the given literals cannot fit in the corresponding
|
||||
/// bounded vec type. Thus, this is only suitable for testing and non-consensus code.
|
||||
#[macro_export]
|
||||
#[cfg(feature = "std")]
|
||||
macro_rules! bounded_vec {
|
||||
($ ($values:expr),* $(,)?) => {
|
||||
{
|
||||
$crate::sp_std::vec![$($values),*].try_into().unwrap()
|
||||
}
|
||||
};
|
||||
( $value:expr ; $repetition:expr ) => {
|
||||
{
|
||||
$crate::sp_std::vec![$value ; $repetition].try_into().unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build a bounded btree-map from the given literals.
|
||||
///
|
||||
/// The type of the outcome must be known.
|
||||
///
|
||||
/// Will not handle any errors and just panic if the given literals cannot fit in the corresponding
|
||||
/// bounded vec type. Thus, this is only suitable for testing and non-consensus code.
|
||||
#[macro_export]
|
||||
#[cfg(feature = "std")]
|
||||
macro_rules! bounded_btree_map {
|
||||
($ ( $key:expr => $value:expr ),* $(,)?) => {
|
||||
{
|
||||
$crate::TryCollect::<$crate::bounded::BoundedBTreeMap<_, _, _>>::try_collect(
|
||||
$crate::sp_std::vec![$(($key, $value)),*].into_iter()
|
||||
).unwrap()
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Generates a macro for checking if a certain feature is enabled.
|
||||
///
|
||||
/// These feature checking macros can be used to conditionally enable/disable code in a dependent
|
||||
|
||||
Reference in New Issue
Block a user