Skip to main content

topsoil_core/storage/
bounded_btree_map.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Traits, types and structs to support a bounded BTreeMap.
8
9use crate::storage::StorageDecodeLength;
10pub use subsoil::runtime::BoundedBTreeMap;
11
12impl<K, V, S> StorageDecodeLength for BoundedBTreeMap<K, V, S> {}
13
14#[cfg(test)]
15pub mod test {
16	use super::*;
17	use crate::Twox128;
18	use alloc::collections::btree_map::BTreeMap;
19	use subsoil::io::TestExternalities;
20	use topsoil_core::traits::{ConstU32, Get};
21
22	#[crate::storage_alias]
23	type Foo = StorageValue<Prefix, BoundedBTreeMap<u32, (), ConstU32<7>>>;
24
25	#[crate::storage_alias]
26	type FooMap = StorageMap<Prefix, Twox128, u32, BoundedBTreeMap<u32, (), ConstU32<7>>>;
27
28	#[crate::storage_alias]
29	type FooDoubleMap =
30		StorageDoubleMap<Prefix, Twox128, u32, Twox128, u32, BoundedBTreeMap<u32, (), ConstU32<7>>>;
31
32	fn map_from_keys<K>(keys: &[K]) -> BTreeMap<K, ()>
33	where
34		K: Ord + Copy,
35	{
36		keys.iter().copied().zip(std::iter::repeat(())).collect()
37	}
38
39	fn boundedmap_from_keys<K, S>(keys: &[K]) -> BoundedBTreeMap<K, (), S>
40	where
41		K: Ord + Copy,
42		S: Get<u32>,
43	{
44		map_from_keys(keys).try_into().unwrap()
45	}
46
47	#[test]
48	fn decode_len_works() {
49		TestExternalities::default().execute_with(|| {
50			let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
51			Foo::put(bounded);
52			assert_eq!(Foo::decode_len().unwrap(), 3);
53		});
54
55		TestExternalities::default().execute_with(|| {
56			let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
57			FooMap::insert(1, bounded);
58			assert_eq!(FooMap::decode_len(1).unwrap(), 3);
59			assert!(FooMap::decode_len(0).is_none());
60			assert!(FooMap::decode_len(2).is_none());
61		});
62
63		TestExternalities::default().execute_with(|| {
64			let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
65			FooDoubleMap::insert(1, 1, bounded);
66			assert_eq!(FooDoubleMap::decode_len(1, 1).unwrap(), 3);
67			assert!(FooDoubleMap::decode_len(2, 1).is_none());
68			assert!(FooDoubleMap::decode_len(1, 2).is_none());
69			assert!(FooDoubleMap::decode_len(2, 2).is_none());
70		});
71
72		TestExternalities::default().execute_with(|| {
73			let bounded = boundedmap_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
74			FooDoubleMap::insert(1, 1, bounded.clone());
75			FooDoubleMap::insert(2, 2, bounded); // duplicate value
76
77			assert_eq!(FooDoubleMap::decode_len(1, 1).unwrap(), 3);
78			assert_eq!(FooDoubleMap::decode_len(2, 2).unwrap(), 3);
79			assert!(FooDoubleMap::decode_len(2, 1).is_none());
80			assert!(FooDoubleMap::decode_len(1, 2).is_none());
81		});
82	}
83}