Struct fyi_msg::BeforeAfter

source ·
pub struct BeforeAfter { /* private fields */ }
Available on crate feature progress only.
Expand description

Before and After.

This is a potentially useful companion to Progless that tracks an arbitrary non-zero before and after state. It was created to make it easire to track before/after file sizes from minification-type tasks, but it doesn’t ascribe any particular meaning to the data it holds.

Examples

Usage is as simple as:

use fyi_msg::BeforeAfter;

let mut ba = BeforeAfter::start(123_u64);

// Do some stuff.

ba.stop(50_u64);

Once before and after are set, you can use the getter methods BeforeAfter::before and BeforeAfter::after to obtain the values.

For relative changes where after is expected to be smaller than before, there is BeforeAfter::less and BeforeAfter::less_percent to obtain the relative difference.

For cases where after is expected to be larger, use BeforeAfter::more and BeforeAfter::more_percent instead.

Implementations§

source§

impl BeforeAfter

source

pub const fn start(before: u64) -> Self

New Instance: Set Before.

This creates a new instance with the defined starting point.

A before value of 0_u64 is equivalent to None. The instance will still be created, but the difference methods won’t return any values.

source

pub fn stop(&mut self, after: u64)

Finish Instance: Set After.

This sets the after value of an existing instance, closing it out.

An after value of 0_u64 is equivalent to None, meaning the difference methods won’t return any values.

source

pub const fn before(&self) -> Option<NonZeroU64>

Get Before.

Return the before value if non-zero, otherwise None.

source

pub const fn after(&self) -> Option<NonZeroU64>

Get After.

Return the after value if non-zero, otherwise None.

source

pub const fn less(&self) -> Option<NonZeroU64>

Get Difference (After < Before).

If the after state is expected to be smaller than the before state, return the difference. If either state is unset/zero, or after is larger, None is returned.

Examples
use fyi_msg::BeforeAfter;
use std::num::NonZeroU64;

let ba = BeforeAfter::from((100_u64, 90_u64));
assert_eq!(ba.less(), NonZeroU64::new(10));
source

pub fn less_percent(&self) -> Option<f64>

Percentage Difference (After < Before).

This is the same as BeforeAfter::less, but returns a percentage of the difference over before.

source

pub const fn more(&self) -> Option<NonZeroU64>

Get Difference (After > Before).

If the after state is expected to be larger than the before state, return the difference. If either state is unset/zero, or after is smaller, None is returned.

Examples
use fyi_msg::BeforeAfter;
use std::num::NonZeroU64;

let ba = BeforeAfter::from((100_u64, 130_u64));
assert_eq!(ba.more(), NonZeroU64::new(30));
source

pub fn more_percent(&self) -> Option<f64>

Percentage Difference (After > Before).

This is the same as BeforeAfter::more, but returns a percentage of the difference over before.

Trait Implementations§

source§

impl Clone for BeforeAfter

source§

fn clone(&self) -> BeforeAfter

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for BeforeAfter

source§

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

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

impl From<(u64, u64)> for BeforeAfter

source§

fn from(src: (u64, u64)) -> Self

Converts to this type from the input type.
source§

impl Copy for BeforeAfter

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.