1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
pub struct Context<F: crate::SequenceFactory = crate::TimeSequenceFactory> {
sequence_factory: F,
sequence: F::Sequence,
session: Session,
}
impl Default for Context<crate::TimeSequenceFactory> {
fn default() -> Self {
Self::new()
}
}
impl<SequenceFactory: crate::SequenceFactory + Default> Context<SequenceFactory> {
pub fn new() -> Self {
Context {
sequence_factory: Default::default(),
sequence: SequenceFactory::base(),
session: Session::new(),
}
}
}
impl<SequenceFactory: crate::SequenceFactory> Context<SequenceFactory> {
pub fn next_sequence(&mut self) -> Sequence<SequenceFactory> {
let next = self.sequence_factory.acquire(self.sequence.clone());
assert!(next > self.sequence);
self.sequence = next.clone();
Sequence {
value: next,
session: self.session.clone(),
}
}
pub fn session(&self) -> Session {
self.session.clone()
}
}
pub struct Sequence<SequenceFactory: crate::SequenceFactory> {
value: SequenceFactory::Sequence,
session: Session,
}
impl<T: crate::SequenceFactory> std::fmt::Debug for Sequence<T>
where T::Sequence: std::fmt::Debug
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Sequence({:?}, {:?})", self.value, self.session)
}
}
impl<SequenceFactory: crate::SequenceFactory> Default for Sequence<SequenceFactory> {
fn default() -> Self {
Sequence {
value: SequenceFactory::base(),
session: Session(u64::min_value()),
}
}
}
impl<SequenceFactory: crate::SequenceFactory> Clone for Sequence<SequenceFactory> {
fn clone(&self) -> Self {
Sequence {
value: self.value.clone(),
session: self.session.clone(),
}
}
}
impl<SequenceFactory: crate::SequenceFactory> std::cmp::PartialEq for Sequence<SequenceFactory> {
fn eq(&self, that: &Self) -> bool {
let Sequence{ref value, ref session} = self;
(value, session) == (&that.value, &that.session)
}
}
impl<SequenceFactory: crate::SequenceFactory> std::cmp::Eq for Sequence<SequenceFactory> {
}
impl<SequenceFactory: crate::SequenceFactory> std::cmp::PartialOrd for Sequence<SequenceFactory> {
fn partial_cmp(&self, that: &Self) -> Option<std::cmp::Ordering> {
let Sequence{ref value, ref session} = self;
std::cmp::PartialOrd::partial_cmp(
&(value, session),
&(&that.value, &that.session))
}
}
impl<SequenceFactory: crate::SequenceFactory> std::cmp::Ord for Sequence<SequenceFactory> {
fn cmp(&self, that: &Self) -> std::cmp::Ordering {
let Sequence{ref value, ref session} = self;
(value, session).cmp(&(&that.value, &that.session))
}
}
#[derive(Clone,Eq,PartialEq,Hash,Ord,PartialOrd)]
pub struct Session(u64);
impl Session {
pub fn new() -> Self {
Self::unsafe_from_u64(rand::Rng::gen(&mut rand::thread_rng()))
}
pub fn unsafe_from_u64(session: u64) -> Self {
Session(session)
}
}
impl std::fmt::Debug for Session {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "Session({:x})", self.0)
}
}