summaryrefslogtreecommitdiffstats
path: root/third_party/rust/typenum/build/generic_const_mappings.rs
blob: 20360e041674d7c0c56cfb5407ca1c89730c9867 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use super::*;

pub fn emit_impls() -> ::std::io::Result<()> {
    let out_dir = ::std::env::var("OUT_DIR").unwrap();
    let dest = ::std::path::Path::new(&out_dir).join("generic_const_mappings.rs");
    println!(
        "cargo:rustc-env=TYPENUM_BUILD_GENERIC_CONSTS={}",
        dest.display()
    );
    let mut f = ::std::fs::File::create(&dest).unwrap();

    #[allow(clippy::write_literal)]
    write!(f, "{}", "\
#[cfg(doc)]
use generic_const_mappings::*;

/// Module with some `const`-generics-friendly definitions, to help bridge the gap
/// between those and `typenum` types.
///
///   - It requires the `const-generics` crate feature to be enabled.
///
/// The main type to use here is [`U`], although [`Const`] and [`ToUInt`] may be needed
/// in a generic context.
#[allow(warnings)] // script-generated code
pub mod generic_const_mappings {
    use crate::*;

    /// The main mapping from a generic `const: usize` to a [`UInt`]: [`U<N>`] is expected to work like [`UN`].
    ///
    ///   - It requires the `const-generics` crate feature to be enabled.
    ///
    /// [`U<N>`]: `U`
    /// [`UN`]: `U42`
    ///
    /// # Example
    ///
    /// ```rust
    /// use typenum::*;
    ///
    /// assert_type_eq!(U<42>, U42);
    /// ```
    ///
    /// This can even be used in a generic `const N: usize` context, provided the
    /// genericity is guarded by a `where` clause:
    ///
    /// ```rust
    /// use typenum::*;
    ///
    /// struct MyStruct<const N: usize>;
    ///
    /// trait MyTrait { type AssocType; }
    ///
    /// impl<const N: usize> MyTrait
    ///     for MyStruct<N>
    /// where
    ///     Const<N> : ToUInt,
    /// {
    ///     type AssocType = U<N>;
    /// }
    ///
    /// assert_type_eq!(<MyStruct<42> as MyTrait>::AssocType, U42);
    /// ```
    pub type U<const N: usize> = <Const<N> as ToUInt>::Output;

    /// Used to allow the usage of [`U`] in a generic context.
    pub struct Const<const N: usize>;

    /// Used to allow the usage of [`U`] in a generic context.
    pub trait ToUInt {
        /// The [`UN`][`crate::U42`] type corresponding to `Self = Const<N>`.
        type Output;
    }
\
    ")?;

    for uint in uints() {
        write!(
            f,
            "
    impl ToUInt for Const<{uint}> {{
        type Output = U{uint};
    }}
\
            ",
            uint = uint,
        )?;
    }
    write!(f, "}}")?;
    f.flush()?;
    Ok(())
}