Skip to content

Commit 5aac93c

Browse files
committed
Auto merge of #53910 - IsaacWoods:unify_cvoid, r=SimonSapin
Move std::os::raw::c_void into libcore and re-export in libstd Implements the first part of [RFC 2521](rust-lang/rfcs#2521). cc #53856
2 parents cb6d2df + 23e345b commit 5aac93c

File tree

4 files changed

+48
-35
lines changed

4 files changed

+48
-35
lines changed

src/libcore/ffi.rs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#![stable(feature = "", since = "1.30.0")]
2+
3+
#![allow(non_camel_case_types)]
4+
5+
//! Utilities related to FFI bindings.
6+
7+
use ::fmt;
8+
9+
/// Equivalent to C's `void` type when used as a [pointer].
10+
///
11+
/// In essence, `*const c_void` is equivalent to C's `const void*`
12+
/// and `*mut c_void` is equivalent to C's `void*`. That said, this is
13+
/// *not* the same as C's `void` return type, which is Rust's `()` type.
14+
///
15+
/// Ideally, this type would be equivalent to [`!`], but currently it may
16+
/// be more ideal to use `c_void` for FFI purposes.
17+
///
18+
/// [`!`]: ../../std/primitive.never.html
19+
/// [pointer]: ../../std/primitive.pointer.html
20+
// NB: For LLVM to recognize the void pointer type and by extension
21+
// functions like malloc(), we need to have it represented as i8* in
22+
// LLVM bitcode. The enum used here ensures this and prevents misuse
23+
// of the "raw" type by only having private variants.. We need two
24+
// variants, because the compiler complains about the repr attribute
25+
// otherwise.
26+
#[repr(u8)]
27+
#[stable(feature = "raw_os", since = "1.1.0")]
28+
pub enum c_void {
29+
#[unstable(feature = "c_void_variant", reason = "should not have to exist",
30+
issue = "0")]
31+
#[doc(hidden)] __variant1,
32+
#[unstable(feature = "c_void_variant", reason = "should not have to exist",
33+
issue = "0")]
34+
#[doc(hidden)] __variant2,
35+
}
36+
37+
#[stable(feature = "std_debug", since = "1.16.0")]
38+
impl fmt::Debug for c_void {
39+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40+
f.pad("c_void")
41+
}
42+
}

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ pub mod iter;
202202
pub mod option;
203203
pub mod raw;
204204
pub mod result;
205+
pub mod ffi;
205206

206207
pub mod slice;
207208
pub mod str;

src/libstd/ffi/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -171,5 +171,8 @@ pub use self::c_str::{FromBytesWithNulError};
171171
#[stable(feature = "rust1", since = "1.0.0")]
172172
pub use self::os_str::{OsString, OsStr};
173173

174+
#[stable(feature = "raw_os", since = "1.1.0")]
175+
pub use core::ffi::c_void;
176+
174177
mod c_str;
175178
mod os_str;

src/libstd/os/raw/mod.rs

+2-35
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
1919
#![stable(feature = "raw_os", since = "1.1.0")]
2020

21-
use fmt;
22-
2321
#[doc(include = "os/raw/char.md")]
2422
#[cfg(any(all(target_os = "linux", any(target_arch = "aarch64",
2523
target_arch = "arm",
@@ -83,40 +81,9 @@ use fmt;
8381
#[doc(include = "os/raw/double.md")]
8482
#[stable(feature = "raw_os", since = "1.1.0")] pub type c_double = f64;
8583

86-
/// Equivalent to C's `void` type when used as a [pointer].
87-
///
88-
/// In essence, `*const c_void` is equivalent to C's `const void*`
89-
/// and `*mut c_void` is equivalent to C's `void*`. That said, this is
90-
/// *not* the same as C's `void` return type, which is Rust's `()` type.
91-
///
92-
/// Ideally, this type would be equivalent to [`!`], but currently it may
93-
/// be more ideal to use `c_void` for FFI purposes.
94-
///
95-
/// [`!`]: ../../primitive.never.html
96-
/// [pointer]: ../../primitive.pointer.html
97-
// NB: For LLVM to recognize the void pointer type and by extension
98-
// functions like malloc(), we need to have it represented as i8* in
99-
// LLVM bitcode. The enum used here ensures this and prevents misuse
100-
// of the "raw" type by only having private variants.. We need two
101-
// variants, because the compiler complains about the repr attribute
102-
// otherwise.
103-
#[repr(u8)]
10484
#[stable(feature = "raw_os", since = "1.1.0")]
105-
pub enum c_void {
106-
#[unstable(feature = "c_void_variant", reason = "should not have to exist",
107-
issue = "0")]
108-
#[doc(hidden)] __variant1,
109-
#[unstable(feature = "c_void_variant", reason = "should not have to exist",
110-
issue = "0")]
111-
#[doc(hidden)] __variant2,
112-
}
113-
114-
#[stable(feature = "std_debug", since = "1.16.0")]
115-
impl fmt::Debug for c_void {
116-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117-
f.pad("c_void")
118-
}
119-
}
85+
#[doc(no_inline)]
86+
pub use core::ffi::c_void;
12087

12188
#[cfg(test)]
12289
#[allow(unused_imports)]

0 commit comments

Comments
 (0)