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
26#[unstable(
27    feature = "c_variadic",
28    issue = "44930",
29    reason = "the `c_variadic` feature has not been properly tested on all supported platforms"
30)]
31pub use self::va_list::{VaArgSafe, VaList};
32
33#[unstable(
34    feature = "c_variadic",
35    issue = "44930",
36    reason = "the `c_variadic` feature has not been properly tested on all supported platforms"
37)]
38pub mod va_list;
39
40mod primitives;
41#[stable(feature = "core_ffi_c", since = "1.64.0")]
42pub use self::primitives::{
43    c_char, c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint,
44    c_ulong, c_ulonglong, c_ushort,
45};
46#[unstable(feature = "c_size_t", issue = "88345")]
47pub use self::primitives::{c_ptrdiff_t, c_size_t, c_ssize_t};
48
49// N.B., for LLVM to recognize the void pointer type and by extension
50//     functions like malloc(), we need to have it represented as i8* in
51//     LLVM bitcode. The enum used here ensures this and prevents misuse
52//     of the "raw" type by only having private variants. We need two
53//     variants, because the compiler complains about the repr attribute
54//     otherwise and we need at least one variant as otherwise the enum
55//     would be uninhabited and at least dereferencing such pointers would
56//     be UB.
57#[doc = "Equivalent to C\'s `void` type when used as a [pointer].\n\nIn essence, `*const c_void` is equivalent to C\'s `const void*`\nand `*mut c_void` is equivalent to C\'s `void*`. That said, this is\n*not* the same as C\'s `void` return type, which is Rust\'s `()` type.\n\nTo model pointers to opaque types in FFI, until `extern type` is\nstabilized, it is recommended to use a newtype wrapper around an empty\nbyte array. See the [Nomicon] for details.\n\nOne could use `std::os::raw::c_void` if they want to support old Rust\ncompiler down to 1.1.0. After Rust 1.30.0, it was re-exported by\nthis definition. For more information, please read [RFC 2521].\n\n[Nomicon]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs\n[RFC 2521]: https://github.com/rust-lang/rfcs/blob/master/text/2521-c_void-reunification.md\n"include_str!("c_void.md")]
58#[lang = "c_void"]
59#[repr(u8)]
60#[stable(feature = "core_c_void", since = "1.30.0")]
61pub enum c_void {
62    #[unstable(
63        feature = "c_void_variant",
64        reason = "temporary implementation detail",
65        issue = "none"
66    )]
67    #[doc(hidden)]
68    __variant1,
69    #[unstable(
70        feature = "c_void_variant",
71        reason = "temporary implementation detail",
72        issue = "none"
73    )]
74    #[doc(hidden)]
75    __variant2,
76}
77
78#[stable(feature = "std_debug", since = "1.16.0")]
79impl fmt::Debug for c_void {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        f.debug_struct("c_void").finish()
82    }
83}
84
85// Link the MSVC default lib
86#[cfg(all(windows, target_env = "msvc"))]
87#[link(
88    name = "/defaultlib:msvcrt",
89    modifiers = "+verbatim",
90    cfg(not(target_feature = "crt-static"))
91)]
92#[link(name = "/defaultlib:libcmt", modifiers = "+verbatim", cfg(target_feature = "crt-static"))]
93unsafe extern "C" {}