frame_support/storage/
bounded_btree_set.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Traits, types and structs to support a bounded `BTreeSet`.
19
20use frame_support::storage::StorageDecodeNonDedupLength;
21pub use sp_runtime::BoundedBTreeSet;
22
23impl<T, S> StorageDecodeNonDedupLength for BoundedBTreeSet<T, S> {}
24
25#[cfg(test)]
26pub mod test {
27	use super::*;
28	use crate::Twox128;
29	use alloc::collections::btree_set::BTreeSet;
30	use frame_support::traits::{ConstU32, Get};
31	use sp_io::TestExternalities;
32
33	#[crate::storage_alias]
34	type Foo = StorageValue<Prefix, BoundedBTreeSet<u32, ConstU32<7>>>;
35
36	#[crate::storage_alias]
37	type FooMap = StorageMap<Prefix, Twox128, u32, BoundedBTreeSet<u32, ConstU32<7>>>;
38
39	#[crate::storage_alias]
40	type FooDoubleMap =
41		StorageDoubleMap<Prefix, Twox128, u32, Twox128, u32, BoundedBTreeSet<u32, ConstU32<7>>>;
42
43	fn set_from_keys<T>(keys: &[T]) -> BTreeSet<T>
44	where
45		T: Ord + Copy,
46	{
47		keys.iter().copied().collect()
48	}
49
50	fn boundedset_from_keys<T, S>(keys: &[T]) -> BoundedBTreeSet<T, S>
51	where
52		T: Ord + Copy,
53		S: Get<u32>,
54	{
55		set_from_keys(keys).try_into().unwrap()
56	}
57
58	#[test]
59	fn decode_non_dedup_len_works() {
60		TestExternalities::default().execute_with(|| {
61			let bounded = boundedset_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
62			Foo::put(bounded);
63			assert_eq!(Foo::decode_non_dedup_len().unwrap(), 3);
64		});
65
66		TestExternalities::default().execute_with(|| {
67			let bounded = boundedset_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
68			FooMap::insert(1, bounded);
69			assert_eq!(FooMap::decode_non_dedup_len(1).unwrap(), 3);
70			assert!(FooMap::decode_non_dedup_len(0).is_none());
71			assert!(FooMap::decode_non_dedup_len(2).is_none());
72		});
73
74		TestExternalities::default().execute_with(|| {
75			let bounded = boundedset_from_keys::<u32, ConstU32<7>>(&[1, 2, 3]);
76			FooDoubleMap::insert(1, 1, bounded);
77			assert_eq!(FooDoubleMap::decode_non_dedup_len(1, 1).unwrap(), 3);
78			assert!(FooDoubleMap::decode_non_dedup_len(2, 1).is_none());
79			assert!(FooDoubleMap::decode_non_dedup_len(1, 2).is_none());
80			assert!(FooDoubleMap::decode_non_dedup_len(2, 2).is_none());
81		});
82	}
83}