current_previous/lib.rs
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
//! # current_previous
//!
//! `current_previous` contains the `CurrentPrevious` struct, which tracks the
//! current and previous values that it has held.
#[derive(Clone, Copy, Debug)]
pub struct CurrentPrevious<T> {
current: T,
previous: Option<T>
}
impl <T> CurrentPrevious<T> {
/// Creates a new `CurrentPrevious` holding the `initial` value as its
/// `current` value. The `previous` value is initially `None`.
///
/// # Examples
///
/// ```
/// # use current_previous::CurrentPrevious;
/// let current_previous = CurrentPrevious::new(0);
///
/// assert_eq!(current_previous.current(), &0);
/// assert_eq!(current_previous.previous(), None);
/// ```
pub fn new(initial: T) -> Self {
return Self {
current: initial,
previous: None
};
}
/// Gets a reference to the `current` value.
pub fn current(&self) -> &T {
return &self.current;
}
/// Gets an optional reference to the `previous` value.
pub fn previous(&self) -> Option<&T> {
return self.previous.as_ref();
}
/// Sets a new `current` value, replacing the `previous` value with the old
/// `current` value.
///
/// # Examples
///
/// ```
/// # use current_previous::CurrentPrevious;
/// let mut current_previous = CurrentPrevious::new(0);
///
/// assert_eq!(current_previous.current(), &0);
/// assert_eq!(current_previous.previous(), None);
///
/// current_previous.update(1);
///
/// assert_eq!(current_previous.current(), &1);
/// assert_eq!(current_previous.previous(), Some(&0));
/// ```
pub fn update(&mut self, new: T) {
self.previous = Some(std::mem::replace(&mut self.current, new));
}
/// Replaces `self` with a new `CurrentPrevious` constructed from the given
/// `new` value.
///
/// # Examples
///
/// ```
/// # use current_previous::CurrentPrevious;
/// let mut current_previous = CurrentPrevious::new(0);
///
/// assert_eq!(current_previous.current(), &0);
/// assert_eq!(current_previous.previous(), None);
///
/// current_previous.reset(1);
///
/// assert_eq!(current_previous.current(), &1);
/// assert_eq!(current_previous.previous(), None);
/// ```
pub fn reset(&mut self, new: T) {
*self = Self::new(new);
}
/// Sets the `previous` value to `None`.
///
/// # Examples
///
/// ```
/// # use current_previous::CurrentPrevious;
/// let mut current_previous = CurrentPrevious::new(0);
///
/// assert_eq!(current_previous.current(), &0);
/// assert_eq!(current_previous.previous(), None);
///
/// current_previous.update(1);
///
/// assert_eq!(current_previous.current(), &1);
/// assert_eq!(current_previous.previous(), Some(&0));
///
/// current_previous.clear_previous();
///
/// assert_eq!(current_previous.current(), &1);
/// assert_eq!(current_previous.previous(), None);
/// ```
pub fn clear_previous(&mut self) {
self.previous = None;
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn set_current() {
let current_previous = CurrentPrevious::new(0);
assert_eq!(current_previous.current(), &0);
assert_eq!(current_previous.previous(), None);
}
#[test]
fn set_current_twice() {
let mut current_previous = CurrentPrevious::new(0);
current_previous.update(1);
assert_eq!(current_previous.current(), &1);
assert_eq!(current_previous.previous(), Some(&0));
}
#[test]
fn set_current_thrice() {
let mut current_previous = CurrentPrevious::new(0);
current_previous.update(1);
current_previous.update(2);
assert_eq!(current_previous.current(), &2);
assert_eq!(current_previous.previous(), Some(&1));
}
#[test]
fn clone() {
let current_previous = CurrentPrevious::new(0);
let mut cloned_current_previous = current_previous.clone();
assert_eq!(current_previous.current(), &0);
assert_eq!(current_previous.previous(), None);
assert_eq!(cloned_current_previous.current(), &0);
assert_eq!(cloned_current_previous.previous(), None);
cloned_current_previous.update(1);
assert_eq!(current_previous.current(), &0);
assert_eq!(current_previous.previous(), None);
assert_eq!(cloned_current_previous.current(), &1);
assert_eq!(cloned_current_previous.previous(), Some(&0));
}
#[test]
fn debug_print() {
let mut current_previous = CurrentPrevious::new(0);
assert_eq!(format!("{current_previous:?}"), "CurrentPrevious { current: 0, previous: None }");
current_previous.update(1);
assert_eq!(format!("{current_previous:?}"), "CurrentPrevious { current: 1, previous: Some(0) }");
}
#[test]
fn reset() {
let mut current_previous = CurrentPrevious::new(0);
current_previous.reset(1);
assert_eq!(current_previous.current(), &1);
assert_eq!(current_previous.previous(), None);
}
}