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" )] |
14 | pub use self::c_str::CStr; |
15 | #[doc (inline)] |
16 | #[stable (feature = "cstr_from_bytes_until_nul" , since = "1.69.0" )] |
17 | pub use self::c_str::FromBytesUntilNulError; |
18 | #[doc (inline)] |
19 | #[stable (feature = "core_c_str" , since = "1.64.0" )] |
20 | pub use self::c_str::FromBytesWithNulError; |
21 | use crate::fmt; |
22 | |
23 | #[unstable (feature = "c_str_module" , issue = "112134" )] |
24 | pub 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 | )] |
31 | pub use self::va_list::{VaList, VaListImpl}; |
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 | )] |
38 | pub mod va_list; |
39 | |
40 | mod primitives; |
41 | #[stable (feature = "core_ffi_c" , since = "1.64.0" )] |
42 | pub 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" )] |
47 | pub 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 = include_str!("c_void.md" )] |
58 | #[lang = "c_void" ] |
59 | #[cfg_attr (not(doc), repr(u8))] // An implementation detail we don't want to show up in rustdoc |
60 | #[stable (feature = "core_c_void" , since = "1.30.0" )] |
61 | pub 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" )] |
79 | impl fmt::Debug for c_void { |
80 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
81 | f.debug_struct(name:"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" ))] |
93 | unsafe extern "C" {} |
94 | |