core/ffi/
mod.rs

1//! Platform-specific types, as defined by C.
2//!
3//! Code that interacts via FFI will almost certainly be using the
4//! base types provided by C, which aren't nearly as nicely defined
5//! as Rust's primitive types. This module provides types which will
6//! match those defined by C, so that code that interacts with C will
7//! refer to the correct types.
8
9#![stable(feature = "core_ffi", since = "1.30.0")]
10#![allow(non_camel_case_types)]
11
12#[doc(inline)]
13#[stable(feature = "core_c_str", since = "1.64.0")]
14#[cfg(not(feature = "ferrocene_certified"))]
15pub use self::c_str::CStr;
16#[doc(inline)]
17#[stable(feature = "cstr_from_bytes_until_nul", since = "1.69.0")]
18#[cfg(not(feature = "ferrocene_certified"))]
19pub use self::c_str::FromBytesUntilNulError;
20#[doc(inline)]
21#[stable(feature = "core_c_str", since = "1.64.0")]
22#[cfg(not(feature = "ferrocene_certified"))]
23pub use self::c_str::FromBytesWithNulError;
24#[cfg(not(feature = "ferrocene_certified"))]
25use crate::fmt;
26
27#[stable(feature = "c_str_module", since = "1.88.0")]
28#[cfg(not(feature = "ferrocene_certified"))]
29pub mod c_str;
30
31#[unstable(
32    feature = "c_variadic",
33    issue = "44930",
34    reason = "the `c_variadic` feature has not been properly tested on all supported platforms"
35)]
36#[cfg(not(feature = "ferrocene_certified"))]
37pub use self::va_list::{VaArgSafe, VaList, VaListImpl};
38
39#[unstable(
40    feature = "c_variadic",
41    issue = "44930",
42    reason = "the `c_variadic` feature has not been properly tested on all supported platforms"
43)]
44#[cfg(not(feature = "ferrocene_certified"))]
45pub mod va_list;
46
47mod primitives;
48#[stable(feature = "core_ffi_c", since = "1.64.0")]
49pub use self::primitives::{
50    c_char, c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint,
51    c_ulong, c_ulonglong, c_ushort,
52};
53#[unstable(feature = "c_size_t", issue = "88345")]
54pub use self::primitives::{c_ptrdiff_t, c_size_t, c_ssize_t};
55
56// N.B., for LLVM to recognize the void pointer type and by extension
57//     functions like malloc(), we need to have it represented as i8* in
58//     LLVM bitcode. The enum used here ensures this and prevents misuse
59//     of the "raw" type by only having private variants. We need two
60//     variants, because the compiler complains about the repr attribute
61//     otherwise and we need at least one variant as otherwise the enum
62//     would be uninhabited and at least dereferencing such pointers would
63//     be UB.
64#[doc = include_str!("c_void.md")]
65#[lang = "c_void"]
66#[repr(u8)]
67#[stable(feature = "core_c_void", since = "1.30.0")]
68pub enum c_void {
69    #[unstable(
70        feature = "c_void_variant",
71        reason = "temporary implementation detail",
72        issue = "none"
73    )]
74    #[doc(hidden)]
75    __variant1,
76    #[unstable(
77        feature = "c_void_variant",
78        reason = "temporary implementation detail",
79        issue = "none"
80    )]
81    #[doc(hidden)]
82    __variant2,
83}
84
85#[stable(feature = "std_debug", since = "1.16.0")]
86#[cfg(not(feature = "ferrocene_certified"))]
87impl fmt::Debug for c_void {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        f.debug_struct("c_void").finish()
90    }
91}
92
93// Link the MSVC default lib
94#[cfg(all(windows, target_env = "msvc"))]
95#[link(
96    name = "/defaultlib:msvcrt",
97    modifiers = "+verbatim",
98    cfg(not(target_feature = "crt-static"))
99)]
100#[link(name = "/defaultlib:libcmt", modifiers = "+verbatim", cfg(target_feature = "crt-static"))]
101#[cfg(not(feature = "ferrocene_certified"))]
102unsafe extern "C" {}