Skip to content

Commit 71f25b7

Browse files
committed
core: Add core::ffi::c_intptr_t and core::ffi::c_uintptr_t
Tracking issue: #88345 Signed-off-by: Yonggang Luo <luoyonggang@gmail.com>
1 parent d7f14d3 commit 71f25b7

11 files changed

Lines changed: 81 additions & 2 deletions

File tree

compiler/rustc_feature/src/unstable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ declare_features! (
422422
(unstable, avx10_target_feature, "1.88.0", Some(138843)),
423423
/// Target features on bpf.
424424
(unstable, bpf_target_feature, "1.54.0", Some(150247)),
425+
/// Allows using size_t/ssize_t/uintptr_t/intptr_t/ptrdiff_t.
426+
(unstable, c_size_t, "CURRENT_RUSTC_VERSION", Some(88345)),
425427
/// Allows using C-variadics.
426428
(unstable, c_variadic, "1.34.0", Some(44930)),
427429
/// Allows defining c-variadic functions on targets where this feature has not yet

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ symbols! {
545545
builtin_syntax,
546546
bundle,
547547
c_dash_variadic,
548+
c_size_t,
548549
c_str_literals,
549550
c_unwind,
550551
c_variadic,

library/core/src/ffi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub use self::primitives::{
3838
c_ulong, c_ulonglong, c_ushort,
3939
};
4040
#[unstable(feature = "c_size_t", issue = "88345")]
41-
pub use self::primitives::{c_ptrdiff_t, c_size_t, c_ssize_t};
41+
pub use self::primitives::{c_intptr_t, c_ptrdiff_t, c_size_t, c_ssize_t, c_uintptr_t};
4242

4343
// N.B., for LLVM to recognize the void pointer type and by extension
4444
// functions like malloc(), we need to have it represented as i8* in

library/core/src/ffi/primitives.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,26 @@ pub type c_ptrdiff_t = isize;
174174
#[unstable(feature = "c_size_t", issue = "88345")]
175175
pub type c_ssize_t = isize;
176176

177+
/// Equivalent to C's `intptr_t` type.
178+
///
179+
/// This type have the same size with a pointer. The C standard technically only
180+
/// requires that this type be a signed integer type just capable of holding a
181+
/// pointer.
182+
#[unstable(feature = "c_size_t", issue = "88345")]
183+
#[repr(transparent)]
184+
#[derive(Debug)]
185+
pub struct c_intptr_t(*const ());
186+
187+
/// Equivalent to C's `uintptr_t` type.
188+
///
189+
/// This type have the same size with a pointer. The C standard technically only
190+
/// requires that this type be an unsigned integer type just capable of holding
191+
/// a pointer.
192+
#[unstable(feature = "c_size_t", issue = "88345")]
193+
#[repr(transparent)]
194+
#[derive(Debug)]
195+
pub struct c_uintptr_t(*const ());
196+
177197
mod c_int_definition {
178198
crate::cfg_select! {
179199
any(target_arch = "avr", target_arch = "msp430") => {

library/coretests/tests/ffi.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
mod cstr;
2+
mod intptr;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![deny(fuzzy_provenance_casts)]
2+
#![deny(lossy_provenance_casts)]
3+
4+
use core::ffi::{c_intptr_t, c_uintptr_t};
5+
6+
#[test]
7+
fn test_intptr_unitptr() {
8+
// These types should have the same size as a pointer.
9+
assert_eq!(core::mem::size_of::<c_intptr_t>(), core::mem::size_of::<*const ()>());
10+
assert_eq!(core::mem::size_of::<c_uintptr_t>(), core::mem::size_of::<*const ()>());
11+
12+
let ptr = NonNull::with_exposed_provenance(16_usize);
13+
let ptr_uintptr_t = ptr as c_uintptr_t;
14+
let ptr_back = ptr_uintptr_t as *const ();
15+
assert_eq!(ptr_back.addr(), 16_usize);
16+
assert_eq!(ptr, ptr_back);
17+
}

library/coretests/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(bool_to_result)]
1212
#![feature(borrowed_buf_init)]
1313
#![feature(bstr)]
14+
#![feature(c_size_t)]
1415
#![feature(cfg_target_has_reliable_f16_f128)]
1516
#![feature(char_internals)]
1617
#![feature(clone_to_uninit)]

library/std/src/ffi/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub use core::ffi::{
179179
c_ulong, c_ulonglong, c_ushort,
180180
};
181181
#[unstable(feature = "c_size_t", issue = "88345")]
182-
pub use core::ffi::{c_ptrdiff_t, c_size_t, c_ssize_t};
182+
pub use core::ffi::{c_intptr_t, c_ptrdiff_t, c_size_t, c_ssize_t, c_uintptr_t};
183183

184184
#[doc(inline)]
185185
#[stable(feature = "cstr_from_bytes_until_nul", since = "1.69.0")]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# `c_size_t`
2+
3+
The tracking issue for this feature is: [#88345]
4+
5+
[#88345]: https://github.com/rust-lang/rust/issues/88345
6+
-----
7+
8+
The `c_size_t` feature allows to enable C FFI types `size_t` `ssize_t` `intptr_t` `uintptr_t` `ptrdiff_t`.
9+
10+
## Example
11+
12+
```rust
13+
#![feature(c_size_t)]
14+
15+
use std::ffi::{c_intptr_t, c_ptrdiff_t, c_size_t, c_ssize_t, c_uintptr_t};
16+
17+
fn main() {
18+
let _ptr_integer = 16_usize as c_uintptr_t;
19+
}
20+
```
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![crate_type = "lib"]
2+
3+
use std::ffi::{c_intptr_t};
4+
//~^ ERROR use of unstable library feature `c_size_t`

0 commit comments

Comments
 (0)