Skip to content

Commit 5bd5ccf

Browse files
Jorge AparicioYamakaky
authored andcommitted
Map stdint.h + size_t types to Rust native ones.
uint8_t -> u8 size_t -> usize This is consistent with how libc 0.2 is defining these types. See the second paragraph of [1] and the discussion in [2] for rationale. This is a [breaking-change] because re-generating bindings after this change will change the type signatures of some functions and structs. [1] https://github.com/rust-lang/rfcs/blob/master/text/1291-promote-libc.md#changes-from-today [2] rust-lang/rust#28096
1 parent 282d24c commit 5bd5ccf

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
77
### Breaking
88
- Change the way to link to a library in the cli interface (see the `-l` option
99
in the output of `--help`)
10+
- Map stdint.h + size_t types to Rust native ones (#256)
1011

1112
### Added
1213
- `-no-rust-enums` generate integer constants instead of enums

src/gen.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,24 @@ fn ctypedef_to_rs(
416416
ty: &Type)
417417
-> Vec<P<ast::Item>> {
418418
fn mk_item(ctx: &mut GenCtx, name: &str, ty: &Type) -> P<ast::Item> {
419+
let rust_ty = match &name[..] {
420+
// Alias these types to `usize`, e.g. `type size_t = usize`, instead of aliasing them to
421+
// an arch-dependent type, i.e. `type size_t = u64` on 64-bit archs and
422+
// `type size_t = u32` on 32-bit archs
423+
"size_t" | "uintptr_t" => mk_ty(ctx, false, vec!["usize".to_string()]),
424+
// Same as above
425+
"ptrdiff_t" | "intptr_t" | "ssize_t" => mk_ty(ctx, false, vec!["isize".to_string()]),
426+
"uint8_t" => mk_ty(ctx, false, vec!["u8".to_string()]),
427+
"int8_t" => mk_ty(ctx, false, vec!["i8".to_string()]),
428+
"uint16_t" => mk_ty(ctx, false, vec!["u16".to_string()]),
429+
"int16_t" => mk_ty(ctx, false, vec!["i16".to_string()]),
430+
"uint32_t" => mk_ty(ctx, false, vec!["u32".to_string()]),
431+
"int32_t" => mk_ty(ctx, false, vec!["i32".to_string()]),
432+
"uint64_t" => mk_ty(ctx, false, vec!["u64".to_string()]),
433+
"int64_t" => mk_ty(ctx, false, vec!["i64".to_string()]),
434+
_ => cty_to_rs(ctx, ty),
435+
};
419436
let rust_name = rust_type_id(ctx, name);
420-
let rust_ty = cty_to_rs(ctx, ty);
421437
let base = ast::ItemKind::Ty(
422438
P(ast::Ty {
423439
id: ast::DUMMY_NODE_ID,

tests/headers/signed.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include <stddef.h>
2+
#include <stdint.h>
3+
#include <sys/types.h>

tests/headers/unsigned.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#include <stddef.h>
2+
#include <stdint.h>

tests/test_ints.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use bindgen;
2+
3+
#[test]
4+
fn unsigned() {
5+
let bindings = bindgen::builder()
6+
.header("tests/headers/unsigned.h")
7+
.generate()
8+
.unwrap()
9+
.to_string();
10+
11+
assert!(bindings.contains("pub type size_t = usize;"));
12+
assert!(bindings.contains("pub type uintptr_t = usize;"));
13+
assert!(bindings.contains("pub type uint8_t = u8;"));
14+
assert!(bindings.contains("pub type uint16_t = u16;"));
15+
assert!(bindings.contains("pub type uint32_t = u32;"));
16+
assert!(bindings.contains("pub type uint64_t = u64;"));
17+
}
18+
19+
#[test]
20+
fn signed() {
21+
let bindings = bindgen::builder()
22+
.header("tests/headers/signed.h")
23+
.generate()
24+
.unwrap()
25+
.to_string();
26+
27+
assert!(bindings.contains("pub type ptrdiff_t = isize;"));
28+
assert!(bindings.contains("pub type intptr_t = isize;"));
29+
assert!(bindings.contains("pub type ssize_t = isize;"));
30+
assert!(bindings.contains("pub type int8_t = i8;"));
31+
assert!(bindings.contains("pub type int16_t = i16;"));
32+
assert!(bindings.contains("pub type int32_t = i32;"));
33+
assert!(bindings.contains("pub type int64_t = i64;"));
34+
}

tests/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ mod test_func;
1414
mod test_struct;
1515
mod test_union;
1616
mod test_builtins;
17+
mod test_ints;

0 commit comments

Comments
 (0)