-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Offer (opt-in) const-generic
mappings. Mainly, a U<N>
type alias.
#176
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f3e1972
Add generic `const` mappings
danielhenrymantilla 7d3ee8f
Dev QoL: allow caching the `build.rs` execution
danielhenrymantilla 077aa83
Improve the readability of the main page docs, w.r.t. the consts expo…
danielhenrymantilla 2ad6e88
Bump the version
danielhenrymantilla cf9f811
Merge branch 'main' into main
paholg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: re-export the negative numbers in the right order: