-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #115 from bluss/merge-0.4
Merge 0.4 branch to master
- Loading branch information
Showing
9 changed files
with
234 additions
and
31 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
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,79 @@ | ||
|
||
use std::env; | ||
use std::io::Write; | ||
use std::process::{Command, Stdio}; | ||
|
||
fn main() { | ||
// we need to output *some* file to opt out of the default | ||
println!("cargo:rerun-if-changed=build.rs"); | ||
|
||
detect_maybe_uninit(); | ||
} | ||
|
||
fn detect_maybe_uninit() { | ||
let has_unstable_union_with_md = probe(&maybe_uninit_code(true)); | ||
if has_unstable_union_with_md { | ||
println!("cargo:rustc-cfg=has_manually_drop_in_union"); | ||
println!("cargo:rustc-cfg=has_union_feature"); | ||
return; | ||
} | ||
|
||
let has_stable_union_with_md = probe(&maybe_uninit_code(false)); | ||
if has_stable_union_with_md { | ||
println!("cargo:rustc-cfg=has_manually_drop_in_union"); | ||
} | ||
} | ||
|
||
// To guard against changes in this currently unstable feature, use | ||
// a detection tests instead of a Rustc version and/or date test. | ||
fn maybe_uninit_code(use_feature: bool) -> String { | ||
let feature = if use_feature { "#![feature(untagged_unions)]" } else { "" }; | ||
|
||
let code = " | ||
#![allow(warnings)] | ||
use std::mem::ManuallyDrop; | ||
#[derive(Copy)] | ||
pub union MaybeUninit<T> { | ||
empty: (), | ||
value: ManuallyDrop<T>, | ||
} | ||
impl<T> Clone for MaybeUninit<T> where T: Copy | ||
{ | ||
fn clone(&self) -> Self { *self } | ||
} | ||
fn main() { | ||
let value1 = MaybeUninit::<[i32; 3]> { empty: () }; | ||
let value2 = MaybeUninit { value: ManuallyDrop::new([1, 2, 3]) }; | ||
} | ||
"; | ||
|
||
|
||
[feature, code].concat() | ||
} | ||
|
||
/// Test if a code snippet can be compiled | ||
fn probe(code: &str) -> bool { | ||
let rustc = env::var_os("RUSTC").unwrap_or_else(|| "rustc".into()); | ||
let out_dir = env::var_os("OUT_DIR").expect("environment variable OUT_DIR"); | ||
|
||
let mut child = Command::new(rustc) | ||
.arg("--out-dir") | ||
.arg(out_dir) | ||
.arg("--emit=obj") | ||
.arg("-") | ||
.stdin(Stdio::piped()) | ||
.spawn() | ||
.expect("rustc probe"); | ||
|
||
child | ||
.stdin | ||
.as_mut() | ||
.expect("rustc stdin") | ||
.write_all(code.as_bytes()) | ||
.expect("write rustc stdin"); | ||
|
||
child.wait().expect("rustc probe").success() | ||
} |
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,51 @@ | ||
|
||
|
||
use array::Array; | ||
use std::mem::ManuallyDrop; | ||
|
||
/// A combination of ManuallyDrop and “maybe uninitialized”; | ||
/// this wraps a value that can be wholly or partially uninitialized; | ||
/// it also has no drop regardless of the type of T. | ||
#[derive(Copy)] | ||
pub union MaybeUninit<T> { | ||
empty: (), | ||
value: ManuallyDrop<T>, | ||
} | ||
// Why we don't use std's MaybeUninit on nightly? See the ptr method | ||
|
||
impl<T> Clone for MaybeUninit<T> where T: Copy | ||
{ | ||
fn clone(&self) -> Self { *self } | ||
} | ||
|
||
impl<T> MaybeUninit<T> { | ||
/// Create a new MaybeUninit with uninitialized interior | ||
pub unsafe fn uninitialized() -> Self { | ||
MaybeUninit { empty: () } | ||
} | ||
|
||
/// Create a new MaybeUninit from the value `v`. | ||
pub fn from(v: T) -> Self { | ||
MaybeUninit { value: ManuallyDrop::new(v) } | ||
} | ||
|
||
// Raw pointer casts written so that we don't reference or access the | ||
// uninitialized interior value | ||
|
||
/// Return a raw pointer to the start of the interior array | ||
pub fn ptr(&self) -> *const T::Item | ||
where T: Array | ||
{ | ||
// std MaybeUninit creates a &self.value reference here which is | ||
// not guaranteed to be sound in our case - we will partially | ||
// initialize the value, not always wholly. | ||
self as *const _ as *const T::Item | ||
} | ||
|
||
/// Return a mut raw pointer to the start of the interior array | ||
pub fn ptr_mut(&mut self) -> *mut T::Item | ||
where T: Array | ||
{ | ||
self as *mut _ as *mut T::Item | ||
} | ||
} |
Oops, something went wrong.