Skip to content

options: Add an opt-in to recover the size_t behavior removed in 5d38f2ac. #1720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
- [Removed](#removed)
- [Fixed](#fixed)
- [Security](#security)
- [0.53.0](#0530)
- [0.53.1](#0531)
- [Added](#added-1)
- [0.53.0](#0530)
- [Added](#added-2)
- [Changed](#changed-1)
- [Fixed](#fixed-1)
- [0.52.0](#0520)
- [Added](#added-2)
- [Added](#added-3)
- [Changed](#changed-2)
- [Fixed](#fixed-2)
- [0.51.1](#0511)
Expand All @@ -23,18 +25,18 @@
- [0.51.0](#0510)
- [Fixed](#fixed-4)
- [Changed](#changed-4)
- [Added](#added-3)
- [0.50.0](#0500)
- [Added](#added-4)
- [0.49.3](#0493)
- [0.50.0](#0500)
- [Added](#added-5)
- [0.49.3](#0493)
- [Added](#added-6)
- [0.49.2](#0492)
- [Changed](#changed-5)
- [0.49.1](#0491)
- [Fixed](#fixed-5)
- [Changed](#changed-6)
- [0.49.0](#0490)
- [Added](#added-6)
- [Added](#added-7)
- [Fixed](#fixed-6)
- [Changed](#changed-7)
- [0.48.1](#0481)
Expand All @@ -53,14 +55,14 @@
- [Changed](#changed-11)
- [Fixed](#fixed-11)
- [0.33.1 .. 0.46.0](#0331--0460)
- [Added](#added-7)
- [Added](#added-8)
- [Removed](#removed-1)
- [Changed](#changed-12)
- [Fixed](#fixed-12)
- [0.33.1](#0331)
- [Fixed](#fixed-13)
- [0.33.0](#0330)
- [Added](#added-8)
- [Added](#added-9)
- [Changed](#changed-13)
- [Deprecated](#deprecated-1)
- [Removed](#removed-2)
Expand All @@ -71,22 +73,22 @@
- [0.32.1](#0321)
- [Fixed](#fixed-16)
- [0.32.0](#0320)
- [Added](#added-9)
- [Added](#added-10)
- [Changed](#changed-14)
- [Fixed](#fixed-17)
- [0.31.0](#0310)
- [Added](#added-10)
- [Added](#added-11)
- [Changed](#changed-15)
- [Deprecated](#deprecated-2)
- [Removed](#removed-3)
- [Fixed](#fixed-18)
- [0.30.0](#0300)
- [Added](#added-11)
- [Added](#added-12)
- [Changed](#changed-16)
- [Deprecated](#deprecated-3)
- [Fixed](#fixed-19)
- [0.29.0](#0290)
- [Added](#added-12)
- [Added](#added-13)
- [Changed](#changed-17)
- [Fixed](#fixed-20)

Expand Down Expand Up @@ -124,6 +126,16 @@ Released YYYY/MM/DD

--------------------------------------------------------------------------------

# 0.53.1

Released 2020/02/03.

## Added

* Opt-in to convert size_t to usize again (#1720).

--------------------------------------------------------------------------------

# 0.53.0

Released 2020/02/02.
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ readme = "README.md"
repository = "https://github.com/rust-lang/rust-bindgen"
documentation = "https://docs.rs/bindgen"
homepage = "https://rust-lang.github.io/rust-bindgen/"
version = "0.53.0"
version = "0.53.1"
build = "build.rs"

include = [
Expand Down
6 changes: 6 additions & 0 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4126,8 +4126,14 @@ mod utils {
"int64_t" => primitive_ty(ctx, "i64"),
"uint64_t" => primitive_ty(ctx, "u64"),

"size_t" if ctx.options().size_t_is_usize => {
primitive_ty(ctx, "usize")
}
"uintptr_t" => primitive_ty(ctx, "usize"),

"ssize_t" if ctx.options().size_t_is_usize => {
primitive_ty(ctx, "isize")
}
"intptr_t" | "ptrdiff_t" => primitive_ty(ctx, "isize"),
_ => return None,
})
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ impl Builder {
output_vector.push("--no-record-matches".into());
}

if self.options.size_t_is_usize {
output_vector.push("--size_t-is-usize".into());
}

if !self.options.rustfmt_bindings {
output_vector.push("--no-rustfmt-bindings".into());
}
Expand Down Expand Up @@ -1350,6 +1354,12 @@ impl Builder {
self
}

/// Set whether `size_t` should be translated to `usize` automatically.
pub fn size_t_is_usize(mut self, is: bool) -> Self {
self.options.size_t_is_usize = is;
self
}

/// Set whether rustfmt should format the generated bindings.
pub fn rustfmt_bindings(mut self, doit: bool) -> Self {
self.options.rustfmt_bindings = doit;
Expand Down Expand Up @@ -1776,6 +1786,9 @@ struct BindgenOptions {
/// items via the `error!` log.
record_matches: bool,

/// Whether `size_t` should be translated to `usize` automatically.
size_t_is_usize: bool,

/// Whether rustfmt should format the generated bindings.
rustfmt_bindings: bool,

Expand Down Expand Up @@ -1917,6 +1930,7 @@ impl Default for BindgenOptions {
time_phases: false,
record_matches: true,
rustfmt_bindings: true,
size_t_is_usize: false,
rustfmt_configuration_file: None,
no_partialeq_types: Default::default(),
no_copy_types: Default::default(),
Expand Down
7 changes: 7 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ where
"Do not record matching items in the regex sets. \
This disables reporting of unused items.",
),
Arg::with_name("size_t-is-usize")
.long("size_t-is-usize")
.help("Translate size_t to usize."),
Arg::with_name("no-rustfmt-bindings")
.long("no-rustfmt-bindings")
.help("Do not format the generated bindings with rustfmt."),
Expand Down Expand Up @@ -763,6 +766,10 @@ where
builder = builder.record_matches(false);
}

if matches.is_present("size_t-is-usize") {
builder = builder.size_t_is_usize(true);
}

let no_rustfmt_bindings = matches.is_present("no-rustfmt-bindings");
if no_rustfmt_bindings {
builder = builder.rustfmt_bindings(false);
Expand Down
49 changes: 49 additions & 0 deletions tests/expectations/tests/size_t_is_usize.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* automatically generated by rust-bindgen */

#![allow(
dead_code,
non_snake_case,
non_camel_case_types,
non_upper_case_globals
)]

#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct A {
pub len: usize,
pub offset: isize,
pub next: *mut A,
}
#[test]
fn bindgen_test_layout_A() {
assert_eq!(
::std::mem::size_of::<A>(),
24usize,
concat!("Size of: ", stringify!(A))
);
assert_eq!(
::std::mem::align_of::<A>(),
8usize,
concat!("Alignment of ", stringify!(A))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<A>())).len as *const _ as usize },
0usize,
concat!("Offset of field: ", stringify!(A), "::", stringify!(len))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<A>())).offset as *const _ as usize },
8usize,
concat!("Offset of field: ", stringify!(A), "::", stringify!(offset))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<A>())).next as *const _ as usize },
16usize,
concat!("Offset of field: ", stringify!(A), "::", stringify!(next))
);
}
impl Default for A {
fn default() -> Self {
unsafe { ::std::mem::zeroed() }
}
}
10 changes: 10 additions & 0 deletions tests/headers/size_t_is_usize.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// bindgen-flags: --size_t-is-usize

typedef unsigned long size_t;
typedef long ssize_t;

struct A {
size_t len;
ssize_t offset;
struct A* next;
};