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