sc_utils/
id_sequence.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
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//! Produce opaque sequential IDs.
20
21/// A Sequence of IDs.
22#[derive(Debug, Default)]
23// The `Clone` trait is intentionally not defined on this type.
24pub struct IDSequence {
25	next_id: u64,
26}
27
28/// A Sequential ID.
29///
30/// Its integer value is intentionally not public: it is supposed to be instantiated from within
31/// this module only.
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
33pub struct SeqID(u64);
34
35impl std::fmt::Display for SeqID {
36	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37		write!(f, "{}", self.0)
38	}
39}
40
41impl IDSequence {
42	/// Create a new ID-sequence.
43	pub fn new() -> Self {
44		Default::default()
45	}
46
47	/// Obtain another ID from this sequence.
48	pub fn next_id(&mut self) -> SeqID {
49		let id = SeqID(self.next_id);
50		self.next_id += 1;
51
52		id
53	}
54}