about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2022-03-01 14:46:41 -0800
committerJosh Triplett <josh@joshtriplett.org>2022-03-01 17:16:05 -0800
commit335c9609c6340a9933915e717ee7a722bd9f81bc (patch)
tree678e22a6b80cfce6d4d056f5c4a49213c401dae2 /library/std/src
parent0f505c63775e9065e8174bb445f93da9f6c9870d (diff)
downloadrust-335c9609c6340a9933915e717ee7a722bd9f81bc.tar.gz
rust-335c9609c6340a9933915e717ee7a722bd9f81bc.zip
Provide C FFI types via core::ffi, not just in std
The ability to interoperate with C code via FFI is not limited to crates
using std; this allows using these types without std.

The existing types in `std::os::raw` become type aliases for the ones in
`core::ffi`. This uses type aliases rather than re-exports, to allow the
std types to remain stable while the core types are unstable.

This also moves the currently unstable `NonZero_` variants and
`c_size_t`/`c_ssize_t`/`c_ptrdiff_t` types to `core::ffi`, while leaving
them unstable.
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/lib.rs2
-rw-r--r--library/std/src/os/raw/char.md9
-rw-r--r--library/std/src/os/raw/double.md6
-rw-r--r--library/std/src/os/raw/float.md5
-rw-r--r--library/std/src/os/raw/int.md5
-rw-r--r--library/std/src/os/raw/long.md5
-rw-r--r--library/std/src/os/raw/longlong.md5
-rw-r--r--library/std/src/os/raw/mod.rs167
-rw-r--r--library/std/src/os/raw/schar.md5
-rw-r--r--library/std/src/os/raw/short.md5
-rw-r--r--library/std/src/os/raw/uchar.md5
-rw-r--r--library/std/src/os/raw/uint.md5
-rw-r--r--library/std/src/os/raw/ulong.md5
-rw-r--r--library/std/src/os/raw/ulonglong.md5
-rw-r--r--library/std/src/os/raw/ushort.md5
-rw-r--r--library/std/src/sys/unix/process/process_unix.rs2
-rw-r--r--library/std/src/sys/unix/process/process_unsupported.rs2
-rw-r--r--library/std/src/sys/unix/process/process_vxworks.rs2
-rw-r--r--library/std/src/sys/windows/c.rs2
19 files changed, 27 insertions, 220 deletions
diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs
index 1e0d9b79b9f..4603b5aae20 100644
--- a/library/std/src/lib.rs
+++ b/library/std/src/lib.rs
@@ -261,6 +261,7 @@
 #![feature(const_socketaddr)]
 #![feature(const_trait_impl)]
 #![feature(container_error_extra)]
+#![feature(core_ffi_c)]
 #![feature(core_intrinsics)]
 #![feature(core_panic)]
 #![feature(custom_test_frameworks)]
@@ -315,6 +316,7 @@
 #![feature(prelude_import)]
 #![feature(ptr_as_uninit)]
 #![feature(ptr_internals)]
+#![feature(raw_os_nonzero)]
 #![feature(rustc_attrs)]
 #![feature(rustc_private)]
 #![feature(saturating_int_impl)]
diff --git a/library/std/src/os/raw/char.md b/library/std/src/os/raw/char.md
deleted file mode 100644
index 375d070516e..00000000000
--- a/library/std/src/os/raw/char.md
+++ /dev/null
@@ -1,9 +0,0 @@
-Equivalent to C's `char` type.
-
-[C's `char` type] is completely unlike [Rust's `char` type]; while Rust's type represents a unicode scalar value, C's `char` type is just an ordinary integer. On modern architectures this type will always be either [`i8`] or [`u8`], as they use byte-addresses memory with 8-bit bytes.
-
-C chars are most commonly used to make C strings. Unlike Rust, where the length of a string is included alongside the string, C strings mark the end of a string with the character `'\0'`. See [`CStr`] for more information.
-
-[C's `char` type]: https://en.wikipedia.org/wiki/C_data_types#Basic_types
-[Rust's `char` type]: char
-[`CStr`]: crate::ffi::CStr
diff --git a/library/std/src/os/raw/double.md b/library/std/src/os/raw/double.md
deleted file mode 100644
index 57f4534829e..00000000000
--- a/library/std/src/os/raw/double.md
+++ /dev/null
@@ -1,6 +0,0 @@
-Equivalent to C's `double` type.
-
-This type will almost always be [`f64`], which is guaranteed to be an [IEEE-754 double-precision float] in Rust. That said, the standard technically only guarantees that it be a floating-point number with at least the precision of a [`float`], and it may be `f32` or something entirely different from the IEEE-754 standard.
-
-[IEEE-754 double-precision float]: https://en.wikipedia.org/wiki/IEEE_754
-[`float`]: c_float
diff --git a/library/std/src/os/raw/float.md b/library/std/src/os/raw/float.md
deleted file mode 100644
index 61e2abc0518..00000000000
--- a/library/std/src/os/raw/float.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `float` type.
-
-This type will almost always be [`f32`], which is guaranteed to be an [IEEE-754 single-precision float] in Rust. That said, the standard technically only guarantees that it be a floating-point number, and it may have less precision than `f32` or not follow the IEEE-754 standard at all.
-
-[IEEE-754 single-precision float]: https://en.wikipedia.org/wiki/IEEE_754
diff --git a/library/std/src/os/raw/int.md b/library/std/src/os/raw/int.md
deleted file mode 100644
index 8062ff2307a..00000000000
--- a/library/std/src/os/raw/int.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `signed int` (`int`) type.
-
-This type will almost always be [`i32`], but may differ on some esoteric systems. The C standard technically only requires that this type be a signed integer that is at least the size of a [`short`]; some systems define it as an [`i16`], for example.
-
-[`short`]: c_short
diff --git a/library/std/src/os/raw/long.md b/library/std/src/os/raw/long.md
deleted file mode 100644
index cc160783f78..00000000000
--- a/library/std/src/os/raw/long.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `signed long` (`long`) type.
-
-This type will always be [`i32`] or [`i64`]. Most notably, many Linux-based systems assume an `i64`, but Windows assumes `i32`. The C standard technically only requires that this type be a signed integer that is at least 32 bits and at least the size of an [`int`], although in practice, no system would have a `long` that is neither an `i32` nor `i64`.
-
-[`int`]: c_int
diff --git a/library/std/src/os/raw/longlong.md b/library/std/src/os/raw/longlong.md
deleted file mode 100644
index 49c61bd61f4..00000000000
--- a/library/std/src/os/raw/longlong.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `signed long long` (`long long`) type.
-
-This type will almost always be [`i64`], but may differ on some systems. The C standard technically only requires that this type be a signed integer that is at least 64 bits and at least the size of a [`long`], although in practice, no system would have a `long long` that is not an `i64`, as most systems do not have a standardised [`i128`] type.
-
-[`long`]: c_int
diff --git a/library/std/src/os/raw/mod.rs b/library/std/src/os/raw/mod.rs
index b6d5199341c..19d0ffb2e39 100644
--- a/library/std/src/os/raw/mod.rs
+++ b/library/std/src/os/raw/mod.rs
@@ -1,156 +1,31 @@
-//! Platform-specific types, as defined by C.
-//!
-//! Code that interacts via FFI will almost certainly be using the
-//! base types provided by C, which aren't nearly as nicely defined
-//! as Rust's primitive types. This module provides types which will
-//! match those defined by C, so that code that interacts with C will
-//! refer to the correct types.
+//! Compatibility module for C platform-specific types. Use [`core::ffi`] instead.
 
 #![stable(feature = "raw_os", since = "1.1.0")]
 
 #[cfg(test)]
 mod tests;
 
-use core::num::*;
-
-macro_rules! type_alias_no_nz {
-    {
-      $Docfile:tt, $Alias:ident = $Real:ty;
-      $( $Cfg:tt )*
-    } => {
-        #[doc = include_str!($Docfile)]
-        $( $Cfg )*
+macro_rules! alias_core_ffi {
+    ($($t:ident)*) => {$(
         #[stable(feature = "raw_os", since = "1.1.0")]
-        pub type $Alias = $Real;
-    }
-}
-
-// To verify that the NonZero types in this file's macro invocations correspond
-//
-//  perl -n < library/std/src/os/raw/mod.rs -e 'next unless m/type_alias\!/; die "$_ ?" unless m/, (c_\w+) = (\w+), NonZero_(\w+) = NonZero(\w+)/; die "$_ ?" unless $3 eq $1 and $4 eq ucfirst $2'
-//
-// NB this does not check that the main c_* types are right.
-
-macro_rules! type_alias {
-    {
-      $Docfile:tt, $Alias:ident = $Real:ty, $NZAlias:ident = $NZReal:ty;
-      $( $Cfg:tt )*
-    } => {
-        type_alias_no_nz! { $Docfile, $Alias = $Real; $( $Cfg )* }
-
-        #[doc = concat!("Type alias for `NonZero` version of [`", stringify!($Alias), "`]")]
-        #[unstable(feature = "raw_os_nonzero", issue = "82363")]
-        $( $Cfg )*
-        pub type $NZAlias = $NZReal;
-    }
+        #[doc = include_str!(concat!("../../../../core/src/ffi/", stringify!($t), ".md"))]
+        // Make this type alias appear cfg-dependent so that Clippy does not suggest
+        // replacing expressions like `0 as c_char` with `0_i8`/`0_u8`. This #[cfg(all())] can be
+        // removed after the false positive in https://github.com/rust-lang/rust-clippy/issues/8093
+        // is fixed.
+        #[cfg(all())]
+        #[doc(cfg(all()))]
+        pub type $t = core::ffi::$t;
+    )*}
 }
 
-type_alias! { "char.md", c_char = c_char_definition::c_char, NonZero_c_char = c_char_definition::NonZero_c_char;
-// Make this type alias appear cfg-dependent so that Clippy does not suggest
-// replacing `0 as c_char` with `0_i8`/`0_u8`. This #[cfg(all())] can be removed
-// after the false positive in https://github.com/rust-lang/rust-clippy/issues/8093
-// is fixed.
-#[cfg(all())]
-#[doc(cfg(all()))] }
-type_alias! { "schar.md", c_schar = i8, NonZero_c_schar = NonZeroI8; }
-type_alias! { "uchar.md", c_uchar = u8, NonZero_c_uchar = NonZeroU8; }
-type_alias! { "short.md", c_short = i16, NonZero_c_short = NonZeroI16; }
-type_alias! { "ushort.md", c_ushort = u16, NonZero_c_ushort = NonZeroU16; }
-type_alias! { "int.md", c_int = i32, NonZero_c_int = NonZeroI32; }
-type_alias! { "uint.md", c_uint = u32, NonZero_c_uint = NonZeroU32; }
-type_alias! { "long.md", c_long = i32, NonZero_c_long = NonZeroI32;
-#[doc(cfg(all()))]
-#[cfg(any(target_pointer_width = "32", windows))] }
-type_alias! { "ulong.md", c_ulong = u32, NonZero_c_ulong = NonZeroU32;
-#[doc(cfg(all()))]
-#[cfg(any(target_pointer_width = "32", windows))] }
-type_alias! { "long.md", c_long = i64, NonZero_c_long = NonZeroI64;
-#[doc(cfg(all()))]
-#[cfg(all(target_pointer_width = "64", not(windows)))] }
-type_alias! { "ulong.md", c_ulong = u64, NonZero_c_ulong = NonZeroU64;
-#[doc(cfg(all()))]
-#[cfg(all(target_pointer_width = "64", not(windows)))] }
-type_alias! { "longlong.md", c_longlong = i64, NonZero_c_longlong = NonZeroI64; }
-type_alias! { "ulonglong.md", c_ulonglong = u64, NonZero_c_ulonglong = NonZeroU64; }
-type_alias_no_nz! { "float.md", c_float = f32; }
-type_alias_no_nz! { "double.md", c_double = f64; }
-
-#[stable(feature = "raw_os", since = "1.1.0")]
-#[doc(no_inline)]
-pub use core::ffi::c_void;
-
-/// Equivalent to C's `size_t` type, from `stddef.h` (or `cstddef` for C++).
-///
-/// This type is currently always [`usize`], however in the future there may be
-/// platforms where this is not the case.
-#[unstable(feature = "c_size_t", issue = "88345")]
-pub type c_size_t = usize;
-
-/// Equivalent to C's `ptrdiff_t` type, from `stddef.h` (or `cstddef` for C++).
-///
-/// This type is currently always [`isize`], however in the future there may be
-/// platforms where this is not the case.
-#[unstable(feature = "c_size_t", issue = "88345")]
-pub type c_ptrdiff_t = isize;
-
-/// Equivalent to C's `ssize_t` (on POSIX) or `SSIZE_T` (on Windows) type.
-///
-/// This type is currently always [`isize`], however in the future there may be
-/// platforms where this is not the case.
-#[unstable(feature = "c_size_t", issue = "88345")]
-pub type c_ssize_t = isize;
-
-mod c_char_definition {
-    cfg_if::cfg_if! {
-        // These are the targets on which c_char is unsigned.
-        if #[cfg(any(
-            all(
-                target_os = "linux",
-                any(
-                    target_arch = "aarch64",
-                    target_arch = "arm",
-                    target_arch = "hexagon",
-                    target_arch = "powerpc",
-                    target_arch = "powerpc64",
-                    target_arch = "s390x",
-                    target_arch = "riscv64",
-                    target_arch = "riscv32"
-                )
-            ),
-            all(target_os = "android", any(target_arch = "aarch64", target_arch = "arm")),
-            all(target_os = "l4re", target_arch = "x86_64"),
-            all(
-                target_os = "freebsd",
-                any(
-                    target_arch = "aarch64",
-                    target_arch = "arm",
-                    target_arch = "powerpc",
-                    target_arch = "powerpc64",
-                    target_arch = "riscv64"
-                )
-            ),
-            all(
-                target_os = "netbsd",
-                any(target_arch = "aarch64", target_arch = "arm", target_arch = "powerpc")
-            ),
-            all(target_os = "openbsd", target_arch = "aarch64"),
-            all(
-                target_os = "vxworks",
-                any(
-                    target_arch = "aarch64",
-                    target_arch = "arm",
-                    target_arch = "powerpc64",
-                    target_arch = "powerpc"
-                )
-            ),
-            all(target_os = "fuchsia", target_arch = "aarch64")
-        ))] {
-            pub type c_char = u8;
-            pub type NonZero_c_char = core::num::NonZeroU8;
-        } else {
-            // On every other target, c_char is signed.
-            pub type c_char = i8;
-            pub type NonZero_c_char = core::num::NonZeroI8;
-        }
-    }
+alias_core_ffi! {
+    c_char c_schar c_uchar
+    c_short c_ushort
+    c_int c_uint
+    c_long c_ulong
+    c_longlong c_ulonglong
+    c_float
+    c_double
+    c_void
 }
diff --git a/library/std/src/os/raw/schar.md b/library/std/src/os/raw/schar.md
deleted file mode 100644
index 69879c9f17f..00000000000
--- a/library/std/src/os/raw/schar.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `signed char` type.
-
-This type will always be [`i8`], but is included for completeness. It is defined as being a signed integer the same size as a C [`char`].
-
-[`char`]: c_char
diff --git a/library/std/src/os/raw/short.md b/library/std/src/os/raw/short.md
deleted file mode 100644
index 3d1e53d1325..00000000000
--- a/library/std/src/os/raw/short.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `signed short` (`short`) type.
-
-This type will almost always be [`i16`], but may differ on some esoteric systems. The C standard technically only requires that this type be a signed integer with at least 16 bits; some systems may define it as `i32`, for example.
-
-[`char`]: c_char
diff --git a/library/std/src/os/raw/uchar.md b/library/std/src/os/raw/uchar.md
deleted file mode 100644
index b633bb7f8da..00000000000
--- a/library/std/src/os/raw/uchar.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `unsigned char` type.
-
-This type will always be [`u8`], but is included for completeness. It is defined as being an unsigned integer the same size as a C [`char`].
-
-[`char`]: c_char
diff --git a/library/std/src/os/raw/uint.md b/library/std/src/os/raw/uint.md
deleted file mode 100644
index f3abea35937..00000000000
--- a/library/std/src/os/raw/uint.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `unsigned int` type.
-
-This type will almost always be [`u32`], but may differ on some esoteric systems. The C standard technically only requires that this type be an unsigned integer with the same size as an [`int`]; some systems define it as a [`u16`], for example.
-
-[`int`]: c_int
diff --git a/library/std/src/os/raw/ulong.md b/library/std/src/os/raw/ulong.md
deleted file mode 100644
index 4ab304e6577..00000000000
--- a/library/std/src/os/raw/ulong.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `unsigned long` type.
-
-This type will always be [`u32`] or [`u64`]. Most notably, many Linux-based systems assume an `u64`, but Windows assumes `u32`. The C standard technically only requires that this type be an unsigned integer with the size of a [`long`], although in practice, no system would have a `ulong` that is neither a `u32` nor `u64`.
-
-[`long`]: c_long
diff --git a/library/std/src/os/raw/ulonglong.md b/library/std/src/os/raw/ulonglong.md
deleted file mode 100644
index a27d70e1753..00000000000
--- a/library/std/src/os/raw/ulonglong.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `unsigned long long` type.
-
-This type will almost always be [`u64`], but may differ on some systems. The C standard technically only requires that this type be an unsigned integer with the size of a [`long long`], although in practice, no system would have a `long long` that is not a `u64`, as most systems do not have a standardised [`u128`] type.
-
-[`long long`]: c_longlong
diff --git a/library/std/src/os/raw/ushort.md b/library/std/src/os/raw/ushort.md
deleted file mode 100644
index 6928e51b352..00000000000
--- a/library/std/src/os/raw/ushort.md
+++ /dev/null
@@ -1,5 +0,0 @@
-Equivalent to C's `unsigned short` type.
-
-This type will almost always be [`u16`], but may differ on some esoteric systems. The C standard technically only requires that this type be an unsigned integer with the same size as a [`short`].
-
-[`short`]: c_short
diff --git a/library/std/src/sys/unix/process/process_unix.rs b/library/std/src/sys/unix/process/process_unix.rs
index 9fc2d9fce4d..07a0339c066 100644
--- a/library/std/src/sys/unix/process/process_unix.rs
+++ b/library/std/src/sys/unix/process/process_unix.rs
@@ -3,11 +3,11 @@ use crate::fmt;
 use crate::io::{self, Error, ErrorKind};
 use crate::mem;
 use crate::num::NonZeroI32;
-use crate::os::raw::NonZero_c_int;
 use crate::ptr;
 use crate::sys;
 use crate::sys::cvt;
 use crate::sys::process::process_common::*;
+use core::ffi::NonZero_c_int;
 
 #[cfg(target_os = "linux")]
 use crate::os::linux::process::PidFd;
diff --git a/library/std/src/sys/unix/process/process_unsupported.rs b/library/std/src/sys/unix/process/process_unsupported.rs
index 7d549d060fd..bbabdf787d9 100644
--- a/library/std/src/sys/unix/process/process_unsupported.rs
+++ b/library/std/src/sys/unix/process/process_unsupported.rs
@@ -3,12 +3,12 @@ use crate::fmt;
 use crate::io;
 use crate::io::ErrorKind;
 use crate::num::NonZeroI32;
-use crate::os::raw::NonZero_c_int;
 use crate::sys;
 use crate::sys::cvt;
 use crate::sys::pipe::AnonPipe;
 use crate::sys::process::process_common::*;
 use crate::sys::unix::unsupported::*;
+use core::ffi::NonZero_c_int;
 
 use libc::{c_int, pid_t};
 
diff --git a/library/std/src/sys/unix/process/process_vxworks.rs b/library/std/src/sys/unix/process/process_vxworks.rs
index c6714d3aae2..56ed6cfeb6a 100644
--- a/library/std/src/sys/unix/process/process_vxworks.rs
+++ b/library/std/src/sys/unix/process/process_vxworks.rs
@@ -2,11 +2,11 @@ use crate::convert::{TryFrom, TryInto};
 use crate::fmt;
 use crate::io::{self, Error, ErrorKind};
 use crate::num::NonZeroI32;
-use crate::os::raw::NonZero_c_int;
 use crate::sys;
 use crate::sys::cvt;
 use crate::sys::process::process_common::*;
 use crate::sys_common::thread;
+use core::ffi::NonZero_c_int;
 use libc::RTP_ID;
 use libc::{self, c_char, c_int};
 
diff --git a/library/std/src/sys/windows/c.rs b/library/std/src/sys/windows/c.rs
index c7b6290693e..2affd7e75b0 100644
--- a/library/std/src/sys/windows/c.rs
+++ b/library/std/src/sys/windows/c.rs
@@ -5,9 +5,9 @@
 #![unstable(issue = "none", feature = "windows_c")]
 
 use crate::mem;
-use crate::os::raw::NonZero_c_ulong;
 use crate::os::raw::{c_char, c_int, c_long, c_longlong, c_uint, c_ulong, c_ushort};
 use crate::ptr;
+use core::ffi::NonZero_c_ulong;
 
 use libc::{c_void, size_t, wchar_t};