diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /third_party/rust/typenum/build/generic_const_mappings.rs | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/typenum/build/generic_const_mappings.rs')
-rw-r--r-- | third_party/rust/typenum/build/generic_const_mappings.rs | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/third_party/rust/typenum/build/generic_const_mappings.rs b/third_party/rust/typenum/build/generic_const_mappings.rs new file mode 100644 index 0000000000..20360e0416 --- /dev/null +++ b/third_party/rust/typenum/build/generic_const_mappings.rs @@ -0,0 +1,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(()) +} |