Skip to content

Remove core ffi primitive redefinitions #4339

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions libc-test/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4064,8 +4064,7 @@ fn test_linux(target: &str) {
"epoll_params" => true,

// FIXME(linux): Requires >= 6.12 kernel headers.
"dmabuf_cmsg" |
"dmabuf_token" => true,
"dmabuf_cmsg" | "dmabuf_token" => true,

_ => false,
}
Expand Down
135 changes: 71 additions & 64 deletions src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,70 @@
//!
//! 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),
// 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",
)),
not(target_os = "vita"),
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;
}
}
// 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),
// // 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",
// )),
// not(target_os = "vita"),
// 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 Expand Up @@ -186,3 +186,10 @@ cfg_if! {
pub type __uint128_t = u128;
}
}

// Re-export rust core::ffi c perimitive types
#[allow(unused_imports)]
pub use core::ffi::{
c_char, c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint,
c_ulong, c_ulonglong, c_ushort,
};
Comment on lines +192 to +195
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could actually go a step further here; we won't want to publicly reexport these types at all anymore since users have access to core::ffi directly, so this block can be removed. Instead,

libc/src/macros.rs

Lines 83 to 86 in 5d549b2

pub(crate) use crate::{
c_char, c_double, c_float, c_int, c_long, c_longlong, c_short, c_uchar, c_uint,
c_ulong, c_ulonglong, c_ushort, c_void, intptr_t, size_t, ssize_t, uintptr_t,
};
can be adjusted to reexport core::ffi for our internal use.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes this is a good point! I will add this right now.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out doing this will break the libc tests as those rely of the re-exports through libc rather than directly from core::ffi. Should I see if I can get the tests to use core::ffi instead of libc for these types?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems reasonable to me 👍. It could even be a separate PR to merge sooner since the rest of this is blocked

Loading