tabled/grid/dimension/
const_dimension.rs

1//! Module contains a dimension estimator for [`CompactTable`]
2//!
3//! [`CompactTable`]: crate::tables::CompactTable
4
5use crate::grid::dimension::{Dimension, Estimate};
6
7/// A constant size dimension or a value dimension.
8#[derive(Debug, Clone, Copy)]
9pub struct ConstDimension<const COLUMNS: usize, const ROWS: usize> {
10    height: ConstSize<ROWS>,
11    width: ConstSize<COLUMNS>,
12}
13
14impl<const COLUMNS: usize, const ROWS: usize> ConstDimension<COLUMNS, ROWS> {
15    /// Returns a new dimension object with a given estimates.
16    pub const fn new(width: ConstSize<COLUMNS>, height: ConstSize<ROWS>) -> Self {
17        Self { width, height }
18    }
19}
20
21impl<const COLUMNS: usize, const ROWS: usize> Dimension for ConstDimension<COLUMNS, ROWS> {
22    fn get_width(&self, column: usize) -> usize {
23        match self.width {
24            ConstSize::List(list) => list[column],
25            ConstSize::Value(val) => val,
26        }
27    }
28
29    fn get_height(&self, row: usize) -> usize {
30        match self.height {
31            ConstSize::List(list) => list[row],
32            ConstSize::Value(val) => val,
33        }
34    }
35}
36
37impl<const COLUMNS: usize, const ROWS: usize> From<ConstDimension<COLUMNS, ROWS>>
38    for (ConstSize<COLUMNS>, ConstSize<ROWS>)
39{
40    fn from(value: ConstDimension<COLUMNS, ROWS>) -> Self {
41        (value.width, value.height)
42    }
43}
44
45impl<R, D, const COLUMNS: usize, const ROWS: usize> Estimate<R, D>
46    for ConstDimension<COLUMNS, ROWS>
47{
48    fn estimate(&mut self, _: R, _: &D) {}
49}
50
51/// Const size represents either a const array values or a single value which responsible for the whole list.
52#[derive(Debug, Clone, Copy)]
53pub enum ConstSize<const N: usize> {
54    /// A constant array of estimates.
55    List([usize; N]),
56    /// A value which act as a single estimate for all entries.
57    Value(usize),
58}
59
60impl From<usize> for ConstSize<0> {
61    fn from(value: usize) -> Self {
62        ConstSize::Value(value)
63    }
64}
65
66impl<const N: usize> From<[usize; N]> for ConstSize<N> {
67    fn from(value: [usize; N]) -> Self {
68        ConstSize::List(value)
69    }
70}