Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i128: enable support for 128-bit integers automatically
This adds a build.rs to byteorder that will set a conditional compilation flag automatically if the current Rust compiler supports 128-bit integers. This makes the i128 feature itself a no-op. We continue to allow the feature to be supplied for backwards compatibility. Addresses bincode-org/bincode#250
- Loading branch information
1 parent
688ec9d
commit 168648f
Showing
5 changed files
with
274 additions
and
186 deletions.
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,82 @@ | ||
use std::env; | ||
use std::ffi::OsString; | ||
use std::process::Command; | ||
|
||
fn main() { | ||
let version = match Version::read() { | ||
Ok(version) => version, | ||
Err(err) => { | ||
eprintln!("failed to parse `rustc --version`: {}", err); | ||
return; | ||
} | ||
}; | ||
enable_i128(version); | ||
} | ||
|
||
fn enable_i128(version: Version) { | ||
if version < (Version { major: 1, minor: 26, patch: 0 }) { | ||
return; | ||
} | ||
|
||
println!("cargo:rustc-cfg=byteorder_i128"); | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)] | ||
struct Version { | ||
major: u32, | ||
minor: u32, | ||
patch: u32, | ||
} | ||
|
||
impl Version { | ||
fn read() -> Result<Version, String> { | ||
let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc")); | ||
let output = Command::new(&rustc) | ||
.arg("--version") | ||
.output() | ||
.unwrap() | ||
.stdout; | ||
Version::parse(&String::from_utf8(output).unwrap()) | ||
} | ||
|
||
fn parse(mut s: &str) -> Result<Version, String> { | ||
if !s.starts_with("rustc ") { | ||
return Err(format!("unrecognized version string: {}", s)); | ||
} | ||
s = &s["rustc ".len()..]; | ||
|
||
let parts: Vec<&str> = s.split(".").collect(); | ||
if parts.len() < 3 { | ||
return Err(format!("not enough version parts: {:?}", parts)); | ||
} | ||
|
||
let mut num = String::new(); | ||
for c in parts[0].chars() { | ||
if !c.is_digit(10) { | ||
break; | ||
} | ||
num.push(c); | ||
} | ||
let major = num.parse::<u32>().map_err(|e| e.to_string())?; | ||
|
||
num.clear(); | ||
for c in parts[1].chars() { | ||
if !c.is_digit(10) { | ||
break; | ||
} | ||
num.push(c); | ||
} | ||
let minor = num.parse::<u32>().map_err(|e| e.to_string())?; | ||
|
||
num.clear(); | ||
for c in parts[2].chars() { | ||
if !c.is_digit(10) { | ||
break; | ||
} | ||
num.push(c); | ||
} | ||
let patch = num.parse::<u32>().map_err(|e| e.to_string())?; | ||
|
||
Ok(Version { major, minor, patch }) | ||
} | ||
} |
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.