Skip to main content

Surge

Struct Surge 

Source
pub struct Surge { /* private fields */ }
Expand description

Warns when a reading changes faster than a safe rate.

This is the primitive behind “warn me before it is too late”: a river level rising fast enough to mean a flash flood, a gas reading spiking toward an explosive level, or a tank pressure collapsing. Feed it successive readings and it reports the rate whenever the change since the previous sample, in the direction being watched, exceeds a limit. The technique one layer down is a first difference between consecutive samples, so a noisy signal pairs well with a Smoother on the input to avoid false alarms.

§Examples

use pamoja_kit::Surge;

// A river gauge in metres, sampled each minute: alarm if it rises faster than
// 0.5 m per sample.
let mut flood = Surge::rising(0.5);
assert_eq!(flood.update(1.0), None); // first reading: no rate yet
assert_eq!(flood.update(1.25), None); // a gentle rise is fine
assert_eq!(flood.update(2.0), Some(0.75)); // a 0.75 m jump: a flash flood

Implementations§

Source§

impl Surge

Source

pub fn rising(limit: f32) -> Self

Creates an alarm for a value rising too fast.

§Arguments
  • limit - the largest safe increase per sample; its magnitude is used.
§Returns

An alarm awaiting its first reading.

Source

pub fn falling(limit: f32) -> Self

Creates an alarm for a value falling too fast.

§Arguments
  • limit - the largest safe decrease per sample; its magnitude is used.
§Returns

An alarm awaiting its first reading.

Source

pub fn update(&mut self, value: f32) -> Option<f32>

Records a reading and reports the rate if it changed too fast.

§Arguments
  • value - the latest reading.
§Returns

Some(rate) for the change since the previous sample when it exceeds the limit in the watched direction, where rate is that change as a positive number; None if the change is within the limit, is in the other direction, or this is the first reading.

Trait Implementations§

Source§

impl Clone for Surge

Source§

fn clone(&self) -> Surge

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Surge

Source§

impl Debug for Surge

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Surge

Source§

fn eq(&self, other: &Surge) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Surge

Auto Trait Implementations§

§

impl Freeze for Surge

§

impl RefUnwindSafe for Surge

§

impl Send for Surge

§

impl Sync for Surge

§

impl Unpin for Surge

§

impl UnsafeUnpin for Surge

§

impl UnwindSafe for Surge

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.