diff options
Diffstat (limited to 'vendor/typenum/build')
-rw-r--r-- | vendor/typenum/build/generic_const_mappings.rs | 91 | ||||
-rw-r--r-- | vendor/typenum/build/main.rs | 27 | ||||
-rw-r--r-- | vendor/typenum/build/op.rs | 1 |
3 files changed, 110 insertions, 9 deletions
diff --git a/vendor/typenum/build/generic_const_mappings.rs b/vendor/typenum/build/generic_const_mappings.rs new file mode 100644 index 000000000..20360e041 --- /dev/null +++ b/vendor/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(()) +} diff --git a/vendor/typenum/build/main.rs b/vendor/typenum/build/main.rs index 03c4697d4..68a97af82 100644 --- a/vendor/typenum/build/main.rs +++ b/vendor/typenum/build/main.rs @@ -4,6 +4,8 @@ use std::fs::File; use std::io::Write; use std::path::Path; +#[cfg(feature = "const-generics")] +mod generic_const_mappings; mod op; mod tests; @@ -75,21 +77,25 @@ pub fn gen_int(i: i64) -> IntCode { )] pub fn no_std() {} -// fixme: get a warning when testing without this -#[allow(dead_code)] -fn main() { - let highest: u64 = 1024; - +const HIGHEST: u64 = 1024; +fn uints() -> impl Iterator<Item = u64> { // Use hardcoded values to avoid issues with cross-compilation. // See https://github.com/paholg/typenum/issues/162 let first2: u32 = 11; // (highest as f64).log(2.0).round() as u32 + 1; let first10: u32 = 4; // (highest as f64).log(10.0) as u32 + 1; - let uints = (0..(highest + 1)) + (0..(HIGHEST + 1)) .chain((first2..64).map(|i| 2u64.pow(i))) - .chain((first10..20).map(|i| 10u64.pow(i))); + .chain((first10..20).map(|i| 10u64.pow(i))) +} + +// fixme: get a warning when testing without this +#[allow(dead_code)] +fn main() { + println!("cargo:rerun-if-changed=build/main.rs"); // Allow caching the generation if `src/*` files change. let out_dir = env::var("OUT_DIR").unwrap(); let dest = Path::new(&out_dir).join("consts.rs"); + #[cfg(not(feature = "force_unix_path_separator"))] println!("cargo:rustc-env=TYPENUM_BUILD_CONSTS={}", dest.display()); let mut f = File::create(&dest).unwrap(); @@ -162,11 +168,11 @@ pub mod consts {{ pub type True = B1; pub type False = B0; ", - highest = highest + highest = HIGHEST, ) .unwrap(); - for u in uints { + for u in uints() { writeln!(f, " pub type U{} = {};", u, gen_uint(u)).unwrap(); if u <= ::std::i64::MAX as u64 && u != 0 { let i = u as i64; @@ -183,4 +189,7 @@ pub mod consts {{ tests::build_tests().unwrap(); op::write_op_macro().unwrap(); + + #[cfg(feature = "const-generics")] + generic_const_mappings::emit_impls().unwrap(); } diff --git a/vendor/typenum/build/op.rs b/vendor/typenum/build/op.rs index 756f37229..f567a88c8 100644 --- a/vendor/typenum/build/op.rs +++ b/vendor/typenum/build/op.rs @@ -18,6 +18,7 @@ struct Op { pub fn write_op_macro() -> ::std::io::Result<()> { let out_dir = ::std::env::var("OUT_DIR").unwrap(); let dest = ::std::path::Path::new(&out_dir).join("op.rs"); + #[cfg(not(feature = "force_unix_path_separator"))] println!("cargo:rustc-env=TYPENUM_BUILD_OP={}", dest.display()); let mut f = ::std::fs::File::create(&dest).unwrap(); |