Skip to content

Copy definitions from core::ffi and centralize them #4256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/fuchsia/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ use crate::prelude::*;

// PUB_TYPE

pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_float = f32;
pub type c_double = f64;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

Expand Down Expand Up @@ -88,9 +78,6 @@ pub type fsblkcnt_t = c_ulonglong;
pub type fsfilcnt_t = c_ulonglong;
pub type rlim_t = c_ulonglong;

pub type c_long = i64;
pub type c_ulong = u64;

// FIXME(fuchsia): why are these uninhabited types? that seems... wrong?
// Presumably these should be `()` or an `extern type` (when that stabilizes).
#[cfg_attr(feature = "extra_traits", derive(Debug))]
Expand Down
13 changes: 0 additions & 13 deletions src/hermit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@

use crate::prelude::*;

pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_long = i64;
pub type c_ulong = u64;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;
pub type intptr_t = isize;
pub type uintptr_t = usize;

pub type c_float = f32;
pub type c_double = f64;

pub type size_t = usize;
pub type ssize_t = isize;
pub type ptrdiff_t = isize;
Expand Down
80 changes: 24 additions & 56 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,130 +38,98 @@ cfg_if! {

pub use core::ffi::c_void;

cfg_if! {
// This configuration comes from `rust-lang/rust` in `library/core/src/ffi/mod.rs`.
if #[cfg(all(
not(windows),
// FIXME(ctest): just use `target_vendor` = "apple"` once `ctest` supports it
not(any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos",
target_os = "visionos",
)),
any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "csky",
target_arch = "hexagon",
target_arch = "msp430",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "riscv64",
target_arch = "riscv32",
target_arch = "s390x",
target_arch = "xtensa",
)
))] {
pub type c_char = u8;
} else {
pub type c_char = i8;
}
}

cfg_if! {
if #[cfg(windows)] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod windows;
pub use crate::windows::*;

prelude!();
} else if #[cfg(target_os = "fuchsia")] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod fuchsia;
pub use crate::fuchsia::*;

prelude!();
} else if #[cfg(target_os = "switch")] {
mod fixed_width_ints;
pub use fixed_width_ints::*;
mod primitives;
pub use primitives::*;

mod switch;
pub use switch::*;

prelude!();
} else if #[cfg(target_os = "vxworks")] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod vxworks;
pub use crate::vxworks::*;

prelude!();
} else if #[cfg(target_os = "solid_asp3")] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod solid;
pub use crate::solid::*;

prelude!();
} else if #[cfg(unix)] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod unix;
pub use crate::unix::*;

prelude!();
} else if #[cfg(target_os = "hermit")] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod hermit;
pub use crate::hermit::*;

prelude!();
} else if #[cfg(target_os = "teeos")] {
mod fixed_width_ints;
pub use fixed_width_ints::*;
mod primitives;
pub use primitives::*;

mod teeos;
pub use teeos::*;

prelude!();
} else if #[cfg(target_os = "trusty")] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod trusty;
pub use crate::trusty::*;

prelude!();
} else if #[cfg(all(target_env = "sgx", target_vendor = "fortanix"))] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod sgx;
pub use crate::sgx::*;

prelude!();
} else if #[cfg(any(target_env = "wasi", target_os = "wasi"))] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod wasi;
pub use crate::wasi::*;

prelude!();
} else if #[cfg(target_os = "xous")] {
mod fixed_width_ints;
pub use crate::fixed_width_ints::*;
mod primitives;
pub use crate::primitives::*;

mod xous;
pub use crate::xous::*;
Expand Down
65 changes: 63 additions & 2 deletions src/fixed_width_ints.rs → src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,67 @@
//! This module contains type aliases for C's fixed-width integer types .
//! This module contains type aliases for C's platform-specific types
//! and fixed-width integer types.
//!
//! These aliases are deprecated: use the Rust types instead.
//! The platform-specific types definitions were taken from rust-lang/rust in
//! library/core/src/ffi/primitives.rs
//!
//! The fixed-width integer aliases are deprecated: use the Rust types instead.

pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;

pub type c_longlong = i64;
pub type c_ulonglong = u64;

pub type c_float = f32;
pub type c_double = f64;

cfg_if! {
if #[cfg(all(
not(windows),
not(target_vendor = "apple"),
any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "csky",
target_arch = "hexagon",
target_arch = "msp430",
target_arch = "powerpc",
target_arch = "powerpc64",
target_arch = "riscv32",
target_arch = "riscv64",
target_arch = "s390x",
target_arch = "xtensa",
)
))] {
pub type c_char = u8;
} else {
// On every other target, c_char is signed.
pub type c_char = i8;
}
}

cfg_if! {
if #[cfg(any(target_arch = "avr", target_arch = "msp430"))] {
pub type c_int = i16;
pub type c_uint = u16;
} else {
pub type c_int = i32;
pub type c_uint = u32;
}
}

cfg_if! {
if #[cfg(all(target_pointer_width = "64", not(windows)))] {
pub type c_long = i64;
pub type c_ulong = u64;
} else {
// The minimal size of `long` in the C standard is 32 bits
pub type c_long = i32;
pub type c_ulong = u32;
}
}

#[deprecated(since = "0.2.55", note = "Use i8 instead.")]
pub type int8_t = i8;
Expand Down
15 changes: 2 additions & 13 deletions src/sgx.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
//! SGX C types definition

pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_float = f32;
pub type c_double = f64;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
use crate::prelude::*;

pub type intmax_t = i64;
pub type uintmax_t = u64;

Expand All @@ -19,8 +11,5 @@ pub type intptr_t = isize;
pub type uintptr_t = usize;
pub type ssize_t = isize;

pub type c_long = i64;
pub type c_ulong = u64;

pub const INT_MIN: c_int = -2147483648;
pub const INT_MAX: c_int = 2147483647;
2 changes: 0 additions & 2 deletions src/solid/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub type wchar_t = u32;
pub type c_long = i64;
pub type c_ulong = u64;
2 changes: 0 additions & 2 deletions src/solid/arm.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
pub type wchar_t = u32;
pub type c_long = i32;
pub type c_ulong = u32;
10 changes: 0 additions & 10 deletions src/solid/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@

use crate::prelude::*;

pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_float = f32;
pub type c_double = f64;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

Expand Down
12 changes: 0 additions & 12 deletions src/switch.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
//! Switch C type definitions

pub type c_schar = i8;
pub type c_uchar = u8;
pub type c_short = i16;
pub type c_ushort = u16;
pub type c_int = i32;
pub type c_uint = u32;
pub type c_float = f32;
pub type c_double = f64;
pub type c_longlong = i64;
pub type c_ulonglong = u64;
pub type intmax_t = i64;
pub type uintmax_t = u64;

Expand All @@ -20,8 +10,6 @@ pub type uintptr_t = usize;
pub type ssize_t = isize;

pub type off_t = i64;
pub type c_long = i64;
pub type c_ulong = u64;
pub type wchar_t = u32;

pub const INT_MIN: c_int = -2147483648;
Expand Down
24 changes: 0 additions & 24 deletions src/teeos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,8 @@

use crate::prelude::*;

pub type c_schar = i8;

pub type c_uchar = u8;

pub type c_short = i16;

pub type c_ushort = u16;

pub type c_int = i32;

pub type c_uint = u32;

pub type c_bool = i32;

pub type c_float = f32;

pub type c_double = f64;

pub type c_longlong = i64;

pub type c_ulonglong = u64;

pub type intmax_t = i64;

pub type uintmax_t = u64;
Expand All @@ -47,10 +27,6 @@ pub type pid_t = c_int;

pub type wchar_t = u32;

pub type c_long = i64;

pub type c_ulong = u64;

// long double in C means A float point value, which has 128bit length.
// but some bit maybe not used, so the real length of long double could be 80(x86) or 128(power pc/IEEE)
// this is different from f128(not stable and not included default) in Rust, so we use u128 for FFI(Rust to C).
Expand Down
Loading