gear_common/storage/primitives/counted.rs
1// Copyright (C) Gear Technologies Inc.
2// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
3
4//! Module for counting primitive.
5//!
6//! Counting primitives are able to return the information
7//! about amount of elements they contain.
8
9/// Represents default counting logic, by providing ability
10/// to return length of the object as specified (associated) type
11/// or answer the question is the object empty.
12pub trait Counted {
13 /// Returning length type.
14 type Length: Default + PartialEq;
15
16 /// Returns current Self's amount of elements as `Length` type.
17 fn len() -> Self::Length;
18
19 /// Returns bool, defining if Self doesn't contain elements.
20 fn is_empty() -> bool {
21 Self::len() == Default::default()
22 }
23}
24
25/// Represents default counting logic, by providing ability
26/// to return length of the object as specified (associated) type
27/// or answer the question is the object empty, by provided key of
28/// specified (associated) type.
29///
30/// Should be implemented on double map based types.
31pub trait CountedByKey {
32 /// Key type of counting target.
33 type Key;
34 /// Returning length type.
35 type Length: Default + PartialEq;
36
37 /// Returns current Self's amount of elements as `Length` type.
38 fn len(key: &Self::Key) -> Self::Length;
39
40 /// Returns bool, defining if Self doesn't contain elements.
41 fn is_empty(key: &Self::Key) -> bool {
42 Self::len(key) == Default::default()
43 }
44}