pumpkin_core/statistics/
mod.rs

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