Skip to content

Commit

Permalink
Auto merge of #1082 - IsaacWoods:core_cvoid, r=alexcrichton
Browse files Browse the repository at this point in the history
Re-export core::ffi::c_void if it exists

This is the second part of the implementation of [RFC 2521](rust-lang/rfcs#2521), replacing the definition of `c_void` in libc with a re-export of the type from `core::ffi::c_void` on builds it exists for.

This uses the re-export for rustc version `1.31.0` or greater, as `1.30.x` was the current nightly when [the PR for the changes to libcore and libstd](rust-lang/rust#53910) was merged, so I'm assuming the first nightly they will appear in will be `1.31.0`; is this acceptable?

cc rust-lang/rust#53856
  • Loading branch information
bors committed Sep 19, 2018
2 parents 878e6ae + 79c80c4 commit d13f346
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = """
A library for types and bindings to native C functions often found in libc or
other common platform libraries.
"""
build = "build.rs"

[badges]
travis-ci = { repository = "rust-lang/libc" }
Expand Down
35 changes: 35 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::env;
use std::process::Command;
use std::str;

fn main() {
/*
* If `core::ffi::c_void` exists, libc can just re-export it. Otherwise, it
* must define an incompatible type to retain backwards-compatibility.
*/
if rustc_minor_version().expect("Failed to get rustc version") >= 31 {
println!("cargo:rustc-cfg=core_cvoid");
}
}

fn rustc_minor_version() -> Option<u32> {
macro_rules! otry {
($e:expr) => {
match $e {
Some(e) => e,
None => return None,
}
};
}

let rustc = otry!(env::var_os("RUSTC"));
let output = otry!(Command::new(rustc).arg("--version").output().ok());
let version = otry!(str::from_utf8(&output.stdout).ok());
let mut pieces = version.split('.');

if pieces.next() != Some("rustc 1") {
return None;
}

otry!(pieces.next()).parse().ok()
}
27 changes: 16 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,22 @@ cfg_if! {
// On the Switch, we only define some useful universal types for
// convenience. Those can be found in the switch.rs file.
} else {

// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
// more optimization opportunities around it recognizing things like
// malloc/free.
#[repr(u8)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
cfg_if! {
if #[cfg(core_cvoid)] {
pub use core::ffi::c_void;
} else {
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
// more optimization opportunities around it recognizing things like
// malloc/free.
#[repr(u8)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
}
}
}

pub type int8_t = i8;
Expand Down
30 changes: 18 additions & 12 deletions src/switch.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
//! Switch C type definitions

// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
// more optimization opportunities around it recognizing things like
// malloc/free.
#[repr(u8)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
}

pub type int8_t = i8;
pub type int16_t = i16;
pub type int32_t = i32;
Expand Down Expand Up @@ -46,3 +34,21 @@ pub type c_char = u8;
pub type c_long = i64;
pub type c_ulong = u64;
pub type wchar_t = u32;

cfg_if! {
if #[cfg(core_cvoid)] {
pub use core::ffi::c_void;
} else {
// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help
// enable more optimization opportunities around it recognizing things
// like malloc/free.
#[repr(u8)]
pub enum c_void {
// Two dummy variants so the #[repr] attribute can be used.
#[doc(hidden)]
__variant1,
#[doc(hidden)]
__variant2,
}
}
}

0 comments on commit d13f346

Please sign in to comment.