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