Skip to main content

pumpkin_core/statistics/
mod.rs

1//! Contains structures related to the statistic logging of the [`Solver`]
2pub mod moving_averages;
3pub(crate) mod statistic_logger;
4pub(crate) mod statistic_logging;
5
6use std::fmt::Display;
7
8pub use statistic_logger::StatisticLogger;
9pub use statistic_logging::StatisticOptions;
10pub use statistic_logging::configure_statistic_logging;
11pub use statistic_logging::log_statistic;
12pub use statistic_logging::log_statistic_postfix;
13pub use statistic_logging::should_log_statistics;
14
15#[cfg(doc)]
16use crate::Solver;
17#[cfg(doc)]
18use crate::create_statistics_struct;
19
20/// A simple trait for defining a loggable statistic.
21///
22/// See [`create_statistics_struct`] for creating a statistic struct automatically!
23pub trait Statistic {
24    /// Logs the [`Statistic`] using the provided [`StatisticLogger`].
25    fn log(&self, statistic_logger: StatisticLogger);
26}
27
28impl<Value: Display> Statistic for Value {
29    fn log(&self, statistic_logger: StatisticLogger) {
30        statistic_logger.log_statistic(self);
31    }
32}
33
34/// A macro for generating a struct for storing statistics.
35///
36/// # Example
37/// ```rust
38/// # use pumpkin_core::create_statistics_struct;
39/// create_statistics_struct!(Statistics {
40///     number_of_calls: usize
41/// });
42///
43/// let statistics = Statistics::default();
44///
45/// assert_eq!(statistics.number_of_calls, 0);
46/// ```
47#[macro_export]
48macro_rules! create_statistics_struct {
49    ($(#[$struct_documentation:meta])* $name:ident { $($(#[$variable_documentation:meta])* $field:ident : $type:ident $(< $( $lt:tt $( : $clt:tt $(+ $dlt:tt )* )? ),+ >)?),+ $(,)? }) => {
50        $(#[$struct_documentation])*
51        #[derive(Default, Debug, Copy, Clone)]
52        pub(crate) struct $name {
53            $($(#[$variable_documentation])* pub(crate) $field: $type $(< $( $lt $( : $clt $(+ $dlt )* )? ),+ >)?),+
54        }
55
56        impl $crate::statistics::Statistic for $name {
57            fn log(&self, statistic_logger: $crate::statistics::StatisticLogger) {
58                $(self.$field.log(statistic_logger.attach_to_prefix(stringify!($field),)));+
59            }
60        }
61    };
62}