Skip to main content

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