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