|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Platform-specific types, as defined by C. |
| 12 | +//! |
| 13 | +//! Code that interacts via FFI will almost certainly be using the |
| 14 | +//! base types provided by C, which aren't nearly as nicely defined |
| 15 | +//! as Rust's primitive types. This module provides types which will |
| 16 | +//! match those defined by C, so that code that interacts with C will |
| 17 | +//! refer to the correct types. |
| 18 | +
|
| 19 | +#![stable(feature = "raw_os", since = "1.1.0")] |
| 20 | + |
| 21 | +use fmt; |
| 22 | + |
| 23 | +#[doc(include = "os/raw/char.md")] |
| 24 | +#[cfg(any(all(target_os = "linux", any(target_arch = "aarch64", |
| 25 | + target_arch = "arm", |
| 26 | + target_arch = "powerpc", |
| 27 | + target_arch = "powerpc64", |
| 28 | + target_arch = "s390x")), |
| 29 | + all(target_os = "android", any(target_arch = "aarch64", |
| 30 | + target_arch = "arm")), |
| 31 | + all(target_os = "l4re", target_arch = "x86_64"), |
| 32 | + all(target_os = "openbsd", target_arch = "aarch64"), |
| 33 | + all(target_os = "fuchsia", target_arch = "aarch64")))] |
| 34 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = u8; |
| 35 | +#[doc(include = "os/raw/char.md")] |
| 36 | +#[cfg(not(any(all(target_os = "linux", any(target_arch = "aarch64", |
| 37 | + target_arch = "arm", |
| 38 | + target_arch = "powerpc", |
| 39 | + target_arch = "powerpc64", |
| 40 | + target_arch = "s390x")), |
| 41 | + all(target_os = "android", any(target_arch = "aarch64", |
| 42 | + target_arch = "arm")), |
| 43 | + all(target_os = "l4re", target_arch = "x86_64"), |
| 44 | + all(target_os = "openbsd", target_arch = "aarch64"), |
| 45 | + all(target_os = "fuchsia", target_arch = "aarch64"))))] |
| 46 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_char = i8; |
| 47 | +#[doc(include = "os/raw/schar.md")] |
| 48 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_schar = i8; |
| 49 | +#[doc(include = "os/raw/uchar.md")] |
| 50 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_uchar = u8; |
| 51 | +#[doc(include = "os/raw/short.md")] |
| 52 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_short = i16; |
| 53 | +#[doc(include = "os/raw/ushort.md")] |
| 54 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_ushort = u16; |
| 55 | +#[doc(include = "os/raw/int.md")] |
| 56 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_int = i32; |
| 57 | +#[doc(include = "os/raw/uint.md")] |
| 58 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_uint = u32; |
| 59 | +#[doc(include = "os/raw/long.md")] |
| 60 | +#[cfg(any(target_pointer_width = "32", windows))] |
| 61 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_long = i32; |
| 62 | +#[doc(include = "os/raw/ulong.md")] |
| 63 | +#[cfg(any(target_pointer_width = "32", windows))] |
| 64 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulong = u32; |
| 65 | +#[doc(include = "os/raw/long.md")] |
| 66 | +#[cfg(all(target_pointer_width = "64", not(windows)))] |
| 67 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_long = i64; |
| 68 | +#[doc(include = "os/raw/ulong.md")] |
| 69 | +#[cfg(all(target_pointer_width = "64", not(windows)))] |
| 70 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulong = u64; |
| 71 | +#[doc(include = "os/raw/longlong.md")] |
| 72 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_longlong = i64; |
| 73 | +#[doc(include = "os/raw/ulonglong.md")] |
| 74 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_ulonglong = u64; |
| 75 | +#[doc(include = "os/raw/float.md")] |
| 76 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_float = f32; |
| 77 | +#[doc(include = "os/raw/double.md")] |
| 78 | +#[stable(feature = "raw_os", since = "1.1.0")] pub type c_double = f64; |
| 79 | + |
| 80 | +/// Equivalent to C's `void` type when used as a [pointer]. |
| 81 | +/// |
| 82 | +/// In essence, `*const c_void` is equivalent to C's `const void*` |
| 83 | +/// and `*mut c_void` is equivalent to C's `void*`. That said, this is |
| 84 | +/// *not* the same as C's `void` return type, which is Rust's `()` type. |
| 85 | +/// |
| 86 | +/// Ideally, this type would be equivalent to [`!`], but currently it may |
| 87 | +/// be more ideal to use `c_void` for FFI purposes. |
| 88 | +/// |
| 89 | +/// [`!`]: ../../../std/primitive.never.html |
| 90 | +/// [pointer]: ../../../std/primitive.pointer.html |
| 91 | +// NB: For LLVM to recognize the void pointer type and by extension |
| 92 | +// functions like malloc(), we need to have it represented as i8* in |
| 93 | +// LLVM bitcode. The enum used here ensures this and prevents misuse |
| 94 | +// of the "raw" type by only having private variants.. We need two |
| 95 | +// variants, because the compiler complains about the repr attribute |
| 96 | +// otherwise. |
| 97 | +#[repr(u8)] |
| 98 | +#[stable(feature = "raw_os", since = "1.1.0")] |
| 99 | +pub enum c_void { |
| 100 | + #[unstable(feature = "c_void_variant", reason = "should not have to exist", |
| 101 | + issue = "0")] |
| 102 | + #[doc(hidden)] __variant1, |
| 103 | + #[unstable(feature = "c_void_variant", reason = "should not have to exist", |
| 104 | + issue = "0")] |
| 105 | + #[doc(hidden)] __variant2, |
| 106 | +} |
| 107 | + |
| 108 | +#[stable(feature = "std_debug", since = "1.16.0")] |
| 109 | +impl fmt::Debug for c_void { |
| 110 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 111 | + f.pad("c_void") |
| 112 | + } |
| 113 | +} |
0 commit comments