evobench_tools/stats_tables/
dynamic_typing.rs

1//! Wrappers to allow to use various types generically at runtime
2
3use std::{borrow::Cow, fmt::Debug};
4
5use super::{
6    stats::{Stats, SubStats, ToStatsString},
7    tables::table_view::{ColumnFormatting, Highlight, TableViewRow, Unit},
8};
9
10#[derive(Debug)]
11pub enum StatsOrCount<ViewType: Debug, const TILE_COUNT: usize> {
12    Stats(Stats<ViewType, TILE_COUNT>),
13    Count(usize),
14}
15
16impl<ViewType: From<u64> + ToStatsString + Debug, const TILE_COUNT: usize> TableViewRow<()>
17    for StatsOrCount<ViewType, TILE_COUNT>
18{
19    fn table_view_header(_: ()) -> Box<dyn AsRef<[(Cow<'static, str>, Unit, ColumnFormatting)]>> {
20        Stats::<ViewType, TILE_COUNT>::table_view_header(())
21    }
22    fn table_view_row(&self, out: &mut Vec<(Cow<str>, Highlight)>) {
23        match self {
24            StatsOrCount::Stats(stats) => stats.table_view_row(out),
25            StatsOrCount::Count(count) => {
26                out.push((
27                    count.to_string().into(),
28                    // XX?
29                    Highlight::Neutral,
30                ));
31            }
32        }
33    }
34}
35
36// XX can one do the same via GATs? For now just do a runtime switch.
37pub enum StatsOrCountOrSubStats<ViewType: Debug, const TILE_COUNT: usize> {
38    StatsOrCount(StatsOrCount<ViewType, TILE_COUNT>),
39    SubStats(SubStats<ViewType, TILE_COUNT>),
40}
41
42impl<ViewType: Debug, const TILE_COUNT: usize> From<StatsOrCount<ViewType, TILE_COUNT>>
43    for StatsOrCountOrSubStats<ViewType, TILE_COUNT>
44{
45    fn from(value: StatsOrCount<ViewType, TILE_COUNT>) -> Self {
46        Self::StatsOrCount(value)
47    }
48}
49
50impl<ViewType: Debug, const TILE_COUNT: usize> From<SubStats<ViewType, TILE_COUNT>>
51    for StatsOrCountOrSubStats<ViewType, TILE_COUNT>
52{
53    fn from(value: SubStats<ViewType, TILE_COUNT>) -> Self {
54        Self::SubStats(value)
55    }
56}
57
58impl<ViewType: Debug + From<u64> + ToStatsString, const TILE_COUNT: usize> TableViewRow<()>
59    for StatsOrCountOrSubStats<ViewType, TILE_COUNT>
60{
61    fn table_view_header(_: ()) -> Box<dyn AsRef<[(Cow<'static, str>, Unit, ColumnFormatting)]>> {
62        // XXX oh, which one to choose from? Are they the same?-- now
63        // could base this on TableViewRow type parameter
64        StatsOrCount::<ViewType, TILE_COUNT>::table_view_header(())
65    }
66
67    fn table_view_row(&self, out: &mut Vec<(Cow<str>, Highlight)>) {
68        match self {
69            StatsOrCountOrSubStats::StatsOrCount(stats_or_count) => {
70                stats_or_count.table_view_row(out)
71            }
72            StatsOrCountOrSubStats::SubStats(sub_stats) => sub_stats.table_view_row(out),
73        }
74    }
75}