Skip to main content

cgp_field/types/
index.rs

1use core::fmt::{Debug, Display};
2
3/**
4    The `Index` type, a.k.a. `δ`, is used to represent a `usize` value at
5    the _type level_.
6
7    `Index` is simply defined to be parameterized by a _const-generic_ value
8    of type `usize`. It is most often used to access generic fields by their
9    _index_, instead of by their _name_.
10
11    `Index` is also shown as `δ` to improve the readability of compiler error
12    messages.
13
14    ## Example
15
16    Given the following struct definition:
17
18    ```rust,ignore
19    pub struct MyContext(pub u32);
20    ```
21
22    The following `HasField` implementation would be generated, with use of
23    `Index<0>` as the field tag:
24
25    ```rust,ignore
26    impl HasField<Index<0>> for MyContext {
27        type Value = u32;
28
29        fn get_field(&self) -> &u32 {
30            &self.0
31        }
32    }
33*/
34#[derive(Eq, PartialEq, Clone, Copy, Default)]
35pub struct δ<const I: usize>;
36
37pub use δ as Index;
38
39impl<const I: usize> Display for Index<I> {
40    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
41        write!(f, "{I}")
42    }
43}
44
45impl<const I: usize> Debug for Index<I> {
46    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
47        write!(f, "{I}")
48    }
49}