-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
107 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::error::Error; | ||
use std::ffi::OsString; | ||
use std::process::{exit, Command}; | ||
|
||
fn main() { | ||
let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc")); | ||
let command = format!("`{} --version`", rustc.to_string_lossy()); | ||
|
||
let output = Command::new(&rustc) | ||
.arg("--version") | ||
.output() | ||
.unwrap_or_else(|e| { | ||
eprintln!("Error: failed to run {}: {}", command, e); | ||
exit(1) | ||
}); | ||
|
||
let supports_maybe_uninit = parse(&output.stdout).unwrap_or_else(|e| { | ||
eprintln!("Error: failed to parse output of {}: {}", command, e); | ||
exit(1) | ||
}); | ||
|
||
if supports_maybe_uninit { | ||
println!("cargo:rustc-cfg=supports_maybe_uninit"); | ||
} | ||
} | ||
|
||
fn parse(output: &[u8]) -> Result<bool, Box<dyn Error>> { | ||
let s = std::str::from_utf8(output)?; | ||
let last_line = s.lines().last().unwrap_or(s); | ||
let mut words = last_line.trim().split(' '); | ||
if words.next() != Some("rustc") { | ||
return Err("version does not start with 'rustc'".into()); | ||
} | ||
let mut triplet = words | ||
.next() | ||
.ok_or("output does not contain version triplet")? | ||
.split('.'); | ||
if triplet.next() != Some("1") { | ||
return Err("rustc major version is not 1".into()); | ||
} | ||
let minor: u32 = triplet | ||
.next() | ||
.ok_or("rustc version does not contain minor version")? | ||
.parse() | ||
.map_err(|e| format!("failed to parse minor version: {}", e))?; | ||
|
||
Ok(minor >= 36) | ||
} |
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,39 @@ | ||
#[cfg(supports_maybe_uninit)] | ||
pub(crate) use std::mem::MaybeUninit; | ||
|
||
#[cfg(not(supports_maybe_uninit))] | ||
mod polyfill { | ||
use std::mem::ManuallyDrop; | ||
use std::ptr; | ||
|
||
use unreachable::UncheckedOptionExt; | ||
|
||
/// A simple `Option`-based implementation of `MaybeUninit` for compiler versions < 1.36.0. | ||
pub struct MaybeUninit<T>(Option<ManuallyDrop<T>>); | ||
|
||
impl<T> MaybeUninit<T> { | ||
pub fn new(val: T) -> Self { | ||
MaybeUninit(Some(ManuallyDrop::new(val))) | ||
} | ||
pub fn uninit() -> Self { | ||
MaybeUninit(None) | ||
} | ||
pub fn as_ptr(&self) -> *const T { | ||
self.0 | ||
.as_ref() | ||
.map(|v| &**v as *const _) | ||
.unwrap_or_else(ptr::null) | ||
} | ||
pub fn as_mut_ptr(&mut self) -> *mut T { | ||
self.0 | ||
.as_mut() | ||
.map(|v| &mut **v as *mut _) | ||
.unwrap_or_else(ptr::null_mut) | ||
} | ||
pub unsafe fn assume_init(self) -> T { | ||
ManuallyDrop::into_inner(self.0.unchecked_unwrap()) | ||
} | ||
} | ||
} | ||
#[cfg(not(supports_maybe_uninit))] | ||
pub(crate) use self::polyfill::MaybeUninit; |