unwrap: comments, names

This commit is contained in:
Robert Jördens 2020-12-06 17:07:38 +01:00
parent e3e786cfdb
commit 2ae0bdfd8d

View File

@ -18,14 +18,14 @@ pub fn overflowing_sub(y: i32, x: i32) -> (i32, i8) {
/// Overflow unwrapper. /// Overflow unwrapper.
/// ///
/// This is unwrapping as in the phase unwrapping context, not unwrapping as /// This is unwrapping as in the phase and overflow unwrapping context, not
/// in the `Result`/`Option` context. /// unwrapping as in the `Result`/`Option` context.
#[derive(Copy, Clone, Default, Deserialize, Serialize)] #[derive(Copy, Clone, Default, Deserialize, Serialize)]
pub struct Unwrapper { pub struct Unwrapper {
// last input // last input
x: i32, x: i32,
// last wraps // last wraps
v: i32, w: i32,
} }
impl Unwrapper { impl Unwrapper {
@ -35,13 +35,13 @@ impl Unwrapper {
/// * `x`: New sample /// * `x`: New sample
/// ///
/// Returns: /// Returns:
/// A tuple containing the (wrapped) difference `x - x_old` and the signed /// A tuple containing the (wrapped) difference `x - x_old` and the
/// number of wraps accumulated by `x`. /// signed number of wraps accumulated by the new sample.
pub fn update(&mut self, x: i32) -> (i32, i32) { pub fn update(&mut self, x: i32) -> (i32, i32) {
let (dx, v) = overflowing_sub(x, self.x); let (dx, dw) = overflowing_sub(x, self.x);
self.x = x; self.x = x;
self.v = self.v.saturating_add(v as i32); self.w = self.w.wrapping_add(dw as i32);
(dx, self.v) (dx, self.w)
} }
} }