Skip to content

Commit f96dcf9

Browse files
committed
options: Add an opt-in to recover the size_t behavior removed in 5d38f2a.
1 parent 1d86a83 commit f96dcf9

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

src/codegen/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -4126,8 +4126,14 @@ mod utils {
41264126
"int64_t" => primitive_ty(ctx, "i64"),
41274127
"uint64_t" => primitive_ty(ctx, "u64"),
41284128

4129+
"size_t" if ctx.options().size_t_is_usize => {
4130+
primitive_ty(ctx, "usize")
4131+
}
41294132
"uintptr_t" => primitive_ty(ctx, "usize"),
41304133

4134+
"ssize_t" if ctx.options().size_t_is_usize => {
4135+
primitive_ty(ctx, "isize")
4136+
}
41314137
"intptr_t" | "ptrdiff_t" => primitive_ty(ctx, "isize"),
41324138
_ => return None,
41334139
})

src/lib.rs

+14
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,10 @@ impl Builder {
604604
output_vector.push("--no-record-matches".into());
605605
}
606606

607+
if self.options.size_t_is_usize {
608+
output_vector.push("--size_t-is-usize".into());
609+
}
610+
607611
if !self.options.rustfmt_bindings {
608612
output_vector.push("--no-rustfmt-bindings".into());
609613
}
@@ -1350,6 +1354,12 @@ impl Builder {
13501354
self
13511355
}
13521356

1357+
/// Set whether `size_t` should be translated to `usize` automatically.
1358+
pub fn size_t_is_usize(mut self, is: bool) -> Self {
1359+
self.options.size_t_is_usize = is;
1360+
self
1361+
}
1362+
13531363
/// Set whether rustfmt should format the generated bindings.
13541364
pub fn rustfmt_bindings(mut self, doit: bool) -> Self {
13551365
self.options.rustfmt_bindings = doit;
@@ -1776,6 +1786,9 @@ struct BindgenOptions {
17761786
/// items via the `error!` log.
17771787
record_matches: bool,
17781788

1789+
/// Whether `size_t` should be translated to `usize` automatically.
1790+
size_t_is_usize: bool,
1791+
17791792
/// Whether rustfmt should format the generated bindings.
17801793
rustfmt_bindings: bool,
17811794

@@ -1917,6 +1930,7 @@ impl Default for BindgenOptions {
19171930
time_phases: false,
19181931
record_matches: true,
19191932
rustfmt_bindings: true,
1933+
size_t_is_usize: false,
19201934
rustfmt_configuration_file: None,
19211935
no_partialeq_types: Default::default(),
19221936
no_copy_types: Default::default(),

src/options.rs

+7
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ where
383383
"Do not record matching items in the regex sets. \
384384
This disables reporting of unused items.",
385385
),
386+
Arg::with_name("size_t-is-usize")
387+
.long("size_t-is-usize")
388+
.help("Translate size_t to usize."),
386389
Arg::with_name("no-rustfmt-bindings")
387390
.long("no-rustfmt-bindings")
388391
.help("Do not format the generated bindings with rustfmt."),
@@ -763,6 +766,10 @@ where
763766
builder = builder.record_matches(false);
764767
}
765768

769+
if matches.is_present("size_t-is-usize") {
770+
builder = builder.size_t_is_usize(true);
771+
}
772+
766773
let no_rustfmt_bindings = matches.is_present("no-rustfmt-bindings");
767774
if no_rustfmt_bindings {
768775
builder = builder.rustfmt_bindings(false);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(
4+
dead_code,
5+
non_snake_case,
6+
non_camel_case_types,
7+
non_upper_case_globals
8+
)]
9+
10+
#[repr(C)]
11+
#[derive(Debug, Copy, Clone)]
12+
pub struct A {
13+
pub len: usize,
14+
pub offset: isize,
15+
pub next: *mut A,
16+
}
17+
#[test]
18+
fn bindgen_test_layout_A() {
19+
assert_eq!(
20+
::std::mem::size_of::<A>(),
21+
24usize,
22+
concat!("Size of: ", stringify!(A))
23+
);
24+
assert_eq!(
25+
::std::mem::align_of::<A>(),
26+
8usize,
27+
concat!("Alignment of ", stringify!(A))
28+
);
29+
assert_eq!(
30+
unsafe { &(*(::std::ptr::null::<A>())).len as *const _ as usize },
31+
0usize,
32+
concat!("Offset of field: ", stringify!(A), "::", stringify!(len))
33+
);
34+
assert_eq!(
35+
unsafe { &(*(::std::ptr::null::<A>())).offset as *const _ as usize },
36+
8usize,
37+
concat!("Offset of field: ", stringify!(A), "::", stringify!(offset))
38+
);
39+
assert_eq!(
40+
unsafe { &(*(::std::ptr::null::<A>())).next as *const _ as usize },
41+
16usize,
42+
concat!("Offset of field: ", stringify!(A), "::", stringify!(next))
43+
);
44+
}
45+
impl Default for A {
46+
fn default() -> Self {
47+
unsafe { ::std::mem::zeroed() }
48+
}
49+
}

tests/headers/size_t_is_usize.h

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// bindgen-flags: --size_t-is-usize
2+
3+
typedef unsigned long size_t;
4+
typedef long ssize_t;
5+
6+
struct A {
7+
size_t len;
8+
ssize_t offset;
9+
struct A* next;
10+
};

0 commit comments

Comments
 (0)