current_previous/
lib.rs

1//! # current_previous
2//!
3//! `current_previous` contains the `CurrentPrevious` struct, which tracks the
4//! current and previous values that it has held.
5
6#[derive(Clone, Copy, Debug)]
7pub struct CurrentPrevious<T> {
8	current: T,
9	previous: Option<T>
10}
11
12impl <T> CurrentPrevious<T> {
13	/// Creates a new `CurrentPrevious` holding the `initial` value as its
14	/// `current` value. The `previous` value is initially `None`.
15	///
16	/// # Examples
17	///
18	/// ```
19	/// # use current_previous::CurrentPrevious;
20	/// let current_previous = CurrentPrevious::new(0);
21	///
22	/// assert_eq!(current_previous.current(), &0);
23	/// assert_eq!(current_previous.previous(), None);
24	/// ```
25	pub fn new(initial: T) -> Self {
26		return Self {
27			current: initial,
28			previous: None
29		};
30	}
31
32	/// Gets a reference to the `current` value.
33	pub fn current(&self) -> &T {
34		return &self.current;
35	}
36
37	/// Gets an optional reference to the `previous` value.
38	pub fn previous(&self) -> Option<&T> {
39		return self.previous.as_ref();
40	}
41
42	/// Sets a new `current` value, replacing the `previous` value with the old
43	/// `current` value.
44	///
45	/// # Examples
46	///
47	/// ```
48	/// # use current_previous::CurrentPrevious;
49	/// let mut current_previous = CurrentPrevious::new(0);
50	///
51	/// assert_eq!(current_previous.current(), &0);
52	/// assert_eq!(current_previous.previous(), None);
53	///
54	/// current_previous.update(1);
55	///
56	/// assert_eq!(current_previous.current(), &1);
57	/// assert_eq!(current_previous.previous(), Some(&0));
58	/// ```
59	pub fn update(&mut self, new: T) {
60		self.previous = Some(std::mem::replace(&mut self.current, new));
61	}
62
63	/// Replaces `self` with a new `CurrentPrevious` constructed from the given
64	/// `new` value.
65	///
66	/// # Examples
67	///
68	/// ```
69	/// # use current_previous::CurrentPrevious;
70	/// let mut current_previous = CurrentPrevious::new(0);
71	///
72	/// assert_eq!(current_previous.current(), &0);
73	/// assert_eq!(current_previous.previous(), None);
74	///
75	/// current_previous.reset(1);
76	///
77	/// assert_eq!(current_previous.current(), &1);
78	/// assert_eq!(current_previous.previous(), None);
79	/// ```
80	pub fn reset(&mut self, new: T) {
81		*self = Self::new(new);
82	}
83
84	/// Sets the `previous` value to `None`.
85	///
86	/// # Examples
87	///
88	/// ```
89	/// # use current_previous::CurrentPrevious;
90	/// let mut current_previous = CurrentPrevious::new(0);
91	///
92	/// assert_eq!(current_previous.current(), &0);
93	/// assert_eq!(current_previous.previous(), None);
94	///
95	/// current_previous.update(1);
96	///
97	/// assert_eq!(current_previous.current(), &1);
98	/// assert_eq!(current_previous.previous(), Some(&0));
99	///
100	/// current_previous.clear_previous();
101	///
102	/// assert_eq!(current_previous.current(), &1);
103	/// assert_eq!(current_previous.previous(), None);
104	/// ```
105	pub fn clear_previous(&mut self) {
106		self.previous = None;
107	}
108}
109
110#[cfg(test)]
111mod tests {
112	use super::*;
113
114	#[test]
115	fn set_current() {
116		let current_previous = CurrentPrevious::new(0);
117
118		assert_eq!(current_previous.current(), &0);
119		assert_eq!(current_previous.previous(), None);
120	}
121
122	#[test]
123	fn set_current_twice() {
124		let mut current_previous = CurrentPrevious::new(0);
125
126		current_previous.update(1);
127
128		assert_eq!(current_previous.current(), &1);
129		assert_eq!(current_previous.previous(), Some(&0));
130	}
131
132	#[test]
133	fn set_current_thrice() {
134		let mut current_previous = CurrentPrevious::new(0);
135
136		current_previous.update(1);
137
138		current_previous.update(2);
139
140		assert_eq!(current_previous.current(), &2);
141		assert_eq!(current_previous.previous(), Some(&1));
142	}
143
144	#[test]
145	fn clone() {
146		let current_previous = CurrentPrevious::new(0);
147
148		let mut cloned_current_previous = current_previous.clone();
149
150		assert_eq!(current_previous.current(), &0);
151		assert_eq!(current_previous.previous(), None);
152
153		assert_eq!(cloned_current_previous.current(), &0);
154		assert_eq!(cloned_current_previous.previous(), None);
155
156		cloned_current_previous.update(1);
157
158		assert_eq!(current_previous.current(), &0);
159		assert_eq!(current_previous.previous(), None);
160
161		assert_eq!(cloned_current_previous.current(), &1);
162		assert_eq!(cloned_current_previous.previous(), Some(&0));
163	}
164
165	#[test]
166	fn debug_print() {
167		let mut current_previous = CurrentPrevious::new(0);
168
169		assert_eq!(format!("{current_previous:?}"), "CurrentPrevious { current: 0, previous: None }");
170
171		current_previous.update(1);
172
173		assert_eq!(format!("{current_previous:?}"), "CurrentPrevious { current: 1, previous: Some(0) }");
174	}
175
176	#[test]
177	fn reset() {
178		let mut current_previous = CurrentPrevious::new(0);
179
180		current_previous.reset(1);
181
182		assert_eq!(current_previous.current(), &1);
183		assert_eq!(current_previous.previous(), None);
184	}
185}