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.
89#![stable(feature = "core_ffi", since = "1.30.0")]
10#![allow(non_camel_case_types)]
1112#[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;
2223#[stable(feature = "c_str_module", since = "1.88.0")]
24pub mod c_str;
2526mod va_list;
27#[stable(feature = "c_variadic", since = "1.99.0")]
28pub use self::va_list::{VaArgSafe, VaList};
2930mod primitives;
31#[stable(feature = "core_ffi_c", since = "1.64.0")]
32pub use self::primitives::{
33c_char, c_double, c_float, c_int, c_long, c_longlong, c_schar, c_short, c_uchar, c_uint,
34c_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};
3839// 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 = "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\ncompilers 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")]
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}
6768#[stable(feature = "std_debug", since = "1.16.0")]
69impl fmt::Debugfor c_void {
70fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71f.debug_struct("c_void").finish()
72 }
73}
7475// 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" {}
8485// 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 {
89use crate::ffi::{c_char, c_int, c_void};
9091unsafe extern "C" {
92#[rustc_canonical_symbol]
93fn memcpy(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void;
9495#[rustc_canonical_symbol]
96fn memmove(dest: *mut c_void, src: *const c_void, n: usize) -> *mut c_void;
9798#[rustc_canonical_symbol]
99fn memset(s: *mut c_void, c: c_int, n: usize) -> *mut c_void;
100101#[rustc_canonical_symbol]
102fn memcmp(s1: *const c_void, s2: *const c_void, n: usize) -> c_int;
103104#[rustc_canonical_symbol]
105fn bcmp(s1: *const c_void, s2: *const c_void, n: usize) -> c_int;
106107#[rustc_canonical_symbol]
108fn strlen(s: *const c_char) -> usize;
109 }
110}