-
-
Notifications
You must be signed in to change notification settings - Fork 2
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
Support for version-based code generation #17
+330
−157
Merged
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -29,60 +29,34 @@ The current const_fn requires Rust 1.31 or later. | |
|
||
## Examples | ||
|
||
When using like the following functions to control unstable features: | ||
|
||
```toml | ||
[features] | ||
const_unstable = [] | ||
``` | ||
|
||
It can be written as follows: | ||
|
||
```rust | ||
#![cfg_attr(feature = "const_unstable", feature(const_fn))] | ||
use const_fn::const_fn; | ||
|
||
pub struct Foo<T> { | ||
x:T, | ||
// 1.36 and later compiler (including beta and nightly) | ||
#[const_fn("1.36")] | ||
pub const fn version() { | ||
/* ... */ | ||
} | ||
|
||
impl<T: Iterator> Foo<T> { | ||
/// Constructs a new `Foo`. | ||
#[const_fn(feature = "const_unstable")] | ||
pub const fn new(x: T) -> Self { | ||
Self { x } | ||
} | ||
// nightly compiler (including dev build) | ||
#[const_fn(nightly)] | ||
pub const fn nightly() { | ||
/* ... */ | ||
Comment on lines
+41
to
+44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To use the nightly only unstable feature, it needs to enable the feature at crate-level, and in any case, it needs build-script. So, this may not be very useful in practice, but I'll keep it for now. |
||
} | ||
``` | ||
|
||
Code like this will be generated: | ||
|
||
```rust | ||
#![cfg_attr(feature = "const_unstable", feature(const_fn))] | ||
|
||
pub struct Foo<T> { | ||
x:T, | ||
// `cfg(...)` | ||
#[const_fn(cfg(...))] | ||
pub const fn cfg() { | ||
/* ... */ | ||
} | ||
|
||
impl<T: Iterator> Foo<T> { | ||
/// Constructs a new `Foo`. | ||
#[cfg(feature = "const_unstable")] | ||
pub const fn new(x: T) -> Self { | ||
Self { x } | ||
} | ||
|
||
/// Constructs a new `Foo`. | ||
#[cfg(not(feature = "const_unstable"))] | ||
pub fn new(x: T) -> Self { | ||
Self { x } | ||
} | ||
// `cfg(feature = "...")` | ||
#[const_fn(feature = "...")] | ||
pub const fn feature() { | ||
/* ... */ | ||
} | ||
``` | ||
|
||
See [test_suite] for more examples. | ||
|
||
[test_suite]: https://github.com/taiki-e/const_fn/tree/master/test_suite | ||
|
||
## License | ||
|
||
Licensed under either of | ||
|
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,28 @@ | ||
use std::{env, fs, path::PathBuf}; | ||
|
||
fn main() { | ||
let version = match Version::new() { | ||
Some(version) => format!("{:#?}\n", version), | ||
None => panic!("unexpected output from `rustc --version`"), | ||
}; | ||
|
||
let out_dir = env::var_os("OUT_DIR").map(PathBuf::from).expect("OUT_DIR not set"); | ||
let out_file = out_dir.join("version.rs"); | ||
fs::write(out_file, version).expect("failed to write version.rs"); | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Version { | ||
minor: u16, | ||
patch: u16, | ||
nightly: bool, | ||
} | ||
|
||
impl Version { | ||
fn new() -> Option<Self> { | ||
let (version, channel, _date) = version_check::triple()?; | ||
let (_major, minor, patch) = version.to_mmp(); | ||
let nightly = channel.is_nightly() || channel.is_dev(); | ||
Some(Version { minor, patch, nightly }) | ||
} | ||
} |
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
Oops, something went wrong.
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.
It currently only accepts string literals as version requirements, like cfg_version does.
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.
Filed #19