Skip to content
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
2 changes: 1 addition & 1 deletion library/core/src/ffi/c_longlong.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ 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
[`long`]: c_long
3 changes: 3 additions & 0 deletions library/core/src/ffi/c_ptrdiff_t.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.
2 changes: 0 additions & 2 deletions library/core/src/ffi/c_short.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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
3 changes: 3 additions & 0 deletions library/core/src/ffi/c_size_t.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.
3 changes: 3 additions & 0 deletions library/core/src/ffi/c_ssize_t.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.
43 changes: 36 additions & 7 deletions library/core/src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,42 @@ pub use self::va_list::{VaArgSafe, VaList, VaListImpl};
pub mod va_list;

mod primitives;
#[stable(feature = "core_ffi_c", since = "1.64.0")]
pub use self::primitives::{
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,
};
#[unstable(feature = "c_size_t", issue = "88345")]
pub use self::primitives::{c_ptrdiff_t, c_size_t, c_ssize_t};

macro_rules! type_alias {
{
$Docfile:tt, $Alias:ident = $Real:ty;
$( $Cfg:tt )*
} => {
#[doc = include_str!($Docfile)]
#[doc(cfg(all()))]
$( $Cfg )*
pub type $Alias = $Real;
Copy link
Member

Choose a reason for hiding this comment

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

IMO this macro isn't adding enough value to be a macro. It's ~only saving on the repetition of the doc(cfg(all())), but that's not hard to copy/paste across all of these (and why is it needed?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe we can drop the macro now? don't know why it introduced at the first place, maybe it's not needed anymore?

Copy link
Member

Choose a reason for hiding this comment

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

If you'd like to revise this PR to not move things around and keep the old structure, I'd be happy to merge it with just the new docs. (I think that means just keeping the first commit). The separate files are intentional per #136334, though we haven't (yet) made use of that, so I'd prefer to avoid significant refactoring in the shape for now.

}
}

type_alias! { "c_char.md", c_char = self::primitives::c_char; #[stable(feature = "core_ffi_c", since = "1.64.0")]}

type_alias! { "c_schar.md", c_schar = self::primitives::c_schar; #[stable(feature = "core_ffi_c", since = "1.64.0")]}
type_alias! { "c_uchar.md", c_uchar = self::primitives::c_uchar; #[stable(feature = "core_ffi_c", since = "1.64.0")]}

type_alias! { "c_short.md", c_short = self::primitives::c_short; #[stable(feature = "core_ffi_c", since = "1.64.0")]}
type_alias! { "c_ushort.md", c_ushort = self::primitives::c_ushort; #[stable(feature = "core_ffi_c", since = "1.64.0")]}

type_alias! { "c_int.md", c_int = self::primitives::c_int; #[stable(feature = "core_ffi_c", since = "1.64.0")]}
type_alias! { "c_uint.md", c_uint = self::primitives::c_uint; #[stable(feature = "core_ffi_c", since = "1.64.0")]}

type_alias! { "c_long.md", c_long = self::primitives::c_long; #[stable(feature = "core_ffi_c", since = "1.64.0")]}
type_alias! { "c_ulong.md", c_ulong = self::primitives::c_ulong; #[stable(feature = "core_ffi_c", since = "1.64.0")]}

type_alias! { "c_longlong.md", c_longlong = self::primitives::c_longlong; #[stable(feature = "core_ffi_c", since = "1.64.0")]}
type_alias! { "c_ulonglong.md", c_ulonglong = self::primitives::c_ulonglong; #[stable(feature = "core_ffi_c", since = "1.64.0")]}

type_alias! { "c_float.md", c_float = self::primitives::c_float; #[stable(feature = "core_ffi_c", since = "1.64.0")]}
type_alias! { "c_double.md", c_double = self::primitives::c_double; #[stable(feature = "core_ffi_c", since = "1.64.0")]}

type_alias! { "c_size_t.md", c_size_t = self::primitives::c_size_t; #[unstable(feature = "c_size_t", issue = "88345")]}
type_alias! { "c_ptrdiff_t.md", c_ptrdiff_t = self::primitives::c_ptrdiff_t; #[unstable(feature = "c_size_t", issue = "88345")]}
type_alias! { "c_ssize_t.md", c_ssize_t = self::primitives::c_ssize_t; #[unstable(feature = "c_size_t", issue = "88345")]}

// N.B., for LLVM to recognize the void pointer type and by extension
// functions like malloc(), we need to have it represented as i8* in
Expand Down
60 changes: 16 additions & 44 deletions library/core/src/ffi/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,25 @@
//! This module is intentionally standalone to facilitate parsing when retrieving
//! core C types.

macro_rules! type_alias {
{
$Docfile:tt, $Alias:ident = $Real:ty;
$( $Cfg:tt )*
} => {
#[doc = include_str!($Docfile)]
$( $Cfg )*
#[stable(feature = "core_ffi_c", since = "1.64.0")]
pub type $Alias = $Real;
}
}
pub(super) type c_char = c_char_definition::c_char;

type_alias! { "c_char.md", c_char = c_char_definition::c_char; #[doc(cfg(all()))] }
pub(super) type c_schar = i8;
pub(super) type c_uchar = u8;

type_alias! { "c_schar.md", c_schar = i8; }
type_alias! { "c_uchar.md", c_uchar = u8; }
type_alias! { "c_short.md", c_short = i16; }
type_alias! { "c_ushort.md", c_ushort = u16; }
pub(super) type c_short = i16;
pub(super) type c_ushort = u16;

type_alias! { "c_int.md", c_int = c_int_definition::c_int; #[doc(cfg(all()))] }
type_alias! { "c_uint.md", c_uint = c_int_definition::c_uint; #[doc(cfg(all()))] }
pub(super) type c_int = c_int_definition::c_int;
pub(super) type c_uint = c_int_definition::c_uint;

type_alias! { "c_long.md", c_long = c_long_definition::c_long; #[doc(cfg(all()))] }
type_alias! { "c_ulong.md", c_ulong = c_long_definition::c_ulong; #[doc(cfg(all()))] }
pub(super) type c_long = c_long_definition::c_long;
pub(super) type c_ulong = c_long_definition::c_ulong;

type_alias! { "c_longlong.md", c_longlong = i64; }
type_alias! { "c_ulonglong.md", c_ulonglong = u64; }
pub(super) type c_longlong = i64;
pub(super) type c_ulonglong = u64;

type_alias! { "c_float.md", c_float = f32; }
type_alias! { "c_double.md", c_double = f64; }
pub(super) type c_float = f32;
pub(super) type c_double = f64;

mod c_char_definition {
crate::cfg_select! {
Expand Down Expand Up @@ -150,26 +139,9 @@ mod c_long_definition {
}
}

/// 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;
pub(super) type c_size_t = usize;
pub(super) type c_ptrdiff_t = isize;
pub(super) type c_ssize_t = isize;

mod c_int_definition {
crate::cfg_select! {
Expand Down
Loading