pub struct History<T> {
min_len: usize,
max_len: usize,
max_age: f32,
total_count: u64,
values: VecDeque<(f64, T)>,
}
Expand description
This struct tracks recent values of some time series.
It can be used as a smoothing filter for e.g. latency, fps etc, or to show a log or graph of recent events.
It has a minimum and maximum length, as well as a maximum storage time.
- The minimum length is to ensure you have enough data for an estimate.
- The maximum length is to make sure the history doesn’t take up too much space.
- The maximum age is to make sure the estimate isn’t outdated.
Time difference between values can be zero, but never negative.
This can be used for things like smoothed averages (for e.g. FPS) or for smoothed velocity (e.g. mouse pointer speed). All times are in seconds.
Fields§
§min_len: usize
§max_len: usize
§max_age: f32
§total_count: u64
§values: VecDeque<(f64, T)>
Implementations§
§impl<T> History<T>where
T: Copy,
impl<T> History<T>where
T: Copy,
pub fn new(length_range: Range<usize>, max_age: f32) -> History<T>
pub fn new(length_range: Range<usize>, max_age: f32) -> History<T>
Example:
// Drop events that are older than one second,
// as long we keep at least two events. Never keep more than a hundred events.
let mut history = History::new(2..100, 1.0);
assert_eq!(history.average(), None);
history.add(now(), 40.0_f32);
history.add(now(), 44.0_f32);
assert_eq!(history.average(), Some(42.0));
pub fn max_len(&self) -> usize
pub fn max_age(&self) -> f32
pub fn is_empty(&self) -> bool
pub fn total_count(&self) -> u64
pub fn total_count(&self) -> u64
Total number of values seen.
Includes those that have been discarded due to max_len
or max_age
.
pub fn latest(&self) -> Option<T>
pub fn latest_mut(&mut self) -> Option<&mut T>
pub fn iter(&self) -> impl ExactSizeIterator
pub fn iter(&self) -> impl ExactSizeIterator
(time, value)
pairs
Time difference between values can be zero, but never negative.
pub fn values(&self) -> impl ExactSizeIterator
pub fn clear(&mut self)
pub fn add(&mut self, now: f64, value: T)
pub fn add(&mut self, now: f64, value: T)
Values must be added with a monotonically increasing time, or at least not decreasing.
pub fn mean_time_interval(&self) -> Option<f32>
pub fn mean_time_interval(&self) -> Option<f32>
Mean time difference between values in this History
.
pub fn rate(&self) -> Option<f32>
Trait Implementations§
§impl<'de, T> Deserialize<'de> for History<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for History<T>where
T: Deserialize<'de>,
§fn deserialize<__D>(
__deserializer: __D
) -> Result<History<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<History<T>, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl<T> Serialize for History<T>where
T: Serialize,
impl<T> Serialize for History<T>where
T: Serialize,
§fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl<T> Freeze for History<T>
impl<T> RefUnwindSafe for History<T>where
T: RefUnwindSafe,
impl<T> Send for History<T>where
T: Send,
impl<T> Sync for History<T>where
T: Sync,
impl<T> Unpin for History<T>where
T: Unpin,
impl<T> UnwindSafe for History<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request