Skip to main content

soil_client/utils/
id_sequence.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: Apache-2.0 OR GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Produce opaque sequential IDs.
8
9/// A Sequence of IDs.
10#[derive(Debug, Default)]
11// The `Clone` trait is intentionally not defined on this type.
12pub struct IDSequence {
13	next_id: u64,
14}
15
16/// A Sequential ID.
17///
18/// Its integer value is intentionally not public: it is supposed to be instantiated from within
19/// this module only.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
21pub struct SeqID(u64);
22
23impl std::fmt::Display for SeqID {
24	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25		write!(f, "{}", self.0)
26	}
27}
28
29impl IDSequence {
30	/// Create a new ID-sequence.
31	pub fn new() -> Self {
32		Default::default()
33	}
34
35	/// Obtain another ID from this sequence.
36	pub fn next_id(&mut self) -> SeqID {
37		let id = SeqID(self.next_id);
38		self.next_id += 1;
39
40		id
41	}
42}
43
44impl Into<u64> for SeqID {
45	fn into(self) -> u64 {
46		self.0
47	}
48}
49
50impl From<u64> for SeqID {
51	fn from(value: u64) -> Self {
52		SeqID(value)
53	}
54}