Struct ProgressRecord

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

Every step of the underlying iterator, one of these is generated. It contains all the information of how this iterator is progresing. Use the methods to access data on it.

Implementations§

Source§

impl ProgressRecord

Source

pub fn duration_since_start(&self) -> Duration

Duration since iteration started

Source

pub fn num_done(&self) -> usize

Number of items we’ve generated so far. Will be 0 for the first element

let mut progressor = (0..1_000).progress();
let (state, num) = progressor.next().unwrap();
assert_eq!(state.num_done(), 1);
let mut progressor = (0..1_000).progress().skip(10);
let (state, num) = progressor.next().unwrap();
assert_eq!(state.num_done(), 11);
Source

pub fn previous_record_tm(&self) -> Option<Instant>

The Instant for when the previous record was generated. None if there was no previous record.

This can be useful for calculating fine-grained rates

Source

pub fn started_iterating(&self) -> Instant

Return the time Instant that this iterator started

Source

pub fn rate(&self) -> f64

Number of items per second, calculated from the start

Source

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

How far through the iterator as a fraction, if known. First looks at the assumed_fraction if you have overridden that. Uses the underlying iterator’s .size_hint() method if that is an exact value, falling back to any assumed size (set with .assume_size(...)). Otherwise returns None.

use iter_progress::ProgressableIter;
let mut progressor = (0..1_000).progress().skip(120);
let (state, num) = progressor.next().unwrap();
assert_eq!(num, 120);
assert_eq!(state.fraction(), Some(0.121));

Returns None if we cannot know, e.g. for an infinite iterator

let mut progressor = (0..).progress().skip(120);
let (state, num) = progressor.next().unwrap();
assert_eq!(state.fraction(), None);
Source

pub fn assume_fraction(&mut self, f: impl Into<f64>)

Assume that this is actually at this fraction through If the underlying Iterator doesn’t provide a useful size_hint, but you “know” the real fraction (e.g. if reading from a file), you can override the value for this ProgressRecord. This new value is used for rate & time calculations.

let mut progressor = (0..).progress();
let (mut state, _num) = progressor.next().unwrap();
assert_eq!(state.fraction(), None);     // No fraction possible
// Be we know we're 12% the way through
state.assume_fraction(0.12);
assert_eq!(state.fraction(), Some(0.12));
Source

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

Percentage progress through the iterator, if known.

use iter_progress::ProgressableIter;
let mut progressor = (0..1_000).progress().skip(120);
let (state, num) = progressor.next().unwrap();
assert_eq!(state.percent(), Some(12.1));

Returns None if we cannot know, e.g. for an infinite iterator

let mut progressor = (0..).progress().skip(120);
let (state, num) = progressor.next().unwrap();
assert_eq!(state.percent(), None);
Source

pub fn print_every_n_sec<T: Display>(&self, n: f32, msg: T)

Print out msg, but only if there has been n seconds since last printout. (uses print!(), so newline not included)

Source

pub fn do_every_n_sec<F: Fn(&Self)>(&self, n: impl Into<f32>, f: F)

Call this function, but only every n sec (as close as possible). Could be a print statement.

Source

pub fn should_do_every_n_sec(&self, n: impl Into<f32>) -> bool

If we want to do every n sec, should we do it now?

Source

pub fn should_do_every_n_items(&self, n: usize) -> bool

If we want to do every n items, should we do it now?

Source

pub fn print_every_n_items<T: Display>(&self, n: usize, msg: T)

Print out msg, but only if there has been n items. Often you want to print out a debug message every 1,000 items or so. This function does that.

Source

pub fn do_every_n_items<F: Fn(&Self)>(&self, n: usize, f: F)

Do thing but only every n items. Could be a print statement.

takes 2 arguments, n and the function (f) which takes a &ProgressState. f will only be called every n items that pass through the iterator.

for (state, _) in (0..150).progress() {
   state.do_every_n_items(5, |state| {
       println!("Current progress: {}%", state.percent().unwrap());
   });
}
Source

pub fn rolling_average_duration(&self) -> &Option<Duration>

Rolling average time to process each item this iterator is processing if it is recording that. None if it’s not being recorded, or it’s too soon to know (e.g. for the first item).

Source

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

Rolling average number of items per second this iterator is processing if it is recording that. None if it’s not being recorded, or it’s too soon to know (e.g. for the first item).

Source

pub fn exp_average_duration(&self) -> &Option<Duration>

Exponential average time to process each item this iterator is processing if it is recording that. None if it’s not being recorded, or it’s too soon to know (e.g. for the first item).

Source

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

Exponential average number of items per second this iterator is processing if it is recording that. None if it’s not being recorded, or it’s too soon to know (e.g. for the first item).

Source

pub fn eta(&self) -> Option<Duration>

If the total size is know (i.e. we know the .fraction()), calculate the estimated time to arrival, i.e. how long before this is finished.

Source

pub fn estimated_total_time(&self) -> Option<Duration>

If the total size is know (i.e. we know the .fraction()), calculate how long, in total, this iterator would run for. i.e. how long it’s run plus how much longer it has left

Trait Implementations§

Source§

impl Debug for ProgressRecord

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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.