gear_common/storage/primitives/
counted.rs

1// This file is part of Gear.
2
3// Copyright (C) 2022-2025 Gear Technologies Inc.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Module for counting primitive.
20//!
21//! Counting primitives are able to return the information
22//! about amount of elements they contain.
23
24/// Represents default counting logic, by providing ability
25/// to return length of the object as specified (associated) type
26/// or answer the question is the object empty.
27pub trait Counted {
28    /// Returning length type.
29    type Length: Default + PartialEq;
30
31    /// Returns current Self's amount of elements as `Length` type.
32    fn len() -> Self::Length;
33
34    /// Returns bool, defining if Self doesn't contain elements.
35    fn is_empty() -> bool {
36        Self::len() == Default::default()
37    }
38}
39
40/// Represents default counting logic, by providing ability
41/// to return length of the object as specified (associated) type
42/// or answer the question is the object empty, by provided key of
43/// specified (associated) type.
44///
45/// Should be implemented on double map based types.
46pub trait CountedByKey {
47    /// Key type of counting target.
48    type Key;
49    /// Returning length type.
50    type Length: Default + PartialEq;
51
52    /// Returns current Self's amount of elements as `Length` type.
53    fn len(key: &Self::Key) -> Self::Length;
54
55    /// Returns bool, defining if Self doesn't contain elements.
56    fn is_empty(key: &Self::Key) -> bool {
57        Self::len(key) == Default::default()
58    }
59}