Wed, 07 Dec 2022 07:00:27 +0200
Added tag v0.1.0 for changeset 51bfde513cfa
0 | 1 | /*! |
2 | Logging routines for intermediate computational results. | |
3 | */ | |
4 | ||
5 | use serde::Serialize; | |
6 | use crate::tabledump::TableDump; | |
7 | ||
8 | /// A log of items of type `T` along with log configuration. | |
9 | /// The constructor takes no arguments; create the log with `Log{T}()` for `T` your | |
10 | /// data type, e.g., `Float64`. | |
11 | #[derive(Debug, Clone)] | |
12 | pub struct Logger<V> { | |
13 | data : Vec<V>, | |
14 | } | |
15 | ||
16 | impl<V> Logger<V> { | |
17 | /// Create new log | |
18 | pub fn new() -> Logger<V> { | |
19 | Logger{ data : Vec::new() } | |
20 | } | |
21 | ||
5 | 22 | /// Store the value `v` in the log. |
0 | 23 | pub fn log(&mut self, v : V) -> () { |
24 | self.data.push(v); | |
25 | } | |
26 | ||
5 | 27 | /// Get the logged data as a [`Vec`] array. |
0 | 28 | pub fn data(&self) -> &Vec<V> { |
29 | &self.data | |
30 | } | |
31 | } | |
32 | ||
33 | impl<'a, V : Serialize + 'a> TableDump<'a> for Logger<V> { | |
34 | type Iter = std::slice::Iter<'a, V>; | |
35 | ||
36 | fn tabledump_entries(&'a self) -> Self::Iter { | |
37 | self.data.iter() | |
38 | } | |
39 | } |