Skip to content

Commit 4d45ce0

Browse files
committed
Add Isize and Usize IntKinds for use in ParseCallbacks::int_macro
C macros are often used to define array/buffer size constants. In Rust, representing these as `usize` is more natural than the fixed-bit-size integer types, because it allows indexing and comparisons without casting. I also added `isize` while I was at it. Using these types is already possible via `IntKind::Custom`, but given they are built-in integer types like `u64` and `i64`, I figured having a separate enum variant would be appropriate.
1 parent b7b501f commit 4d45ce0

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

bindgen/codegen/helpers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ pub(crate) mod ast_ty {
219219
IntKind::U32 => syn::parse_quote! { u32 },
220220
IntKind::I64 => syn::parse_quote! { i64 },
221221
IntKind::U64 => syn::parse_quote! { u64 },
222+
IntKind::Isize => syn::parse_quote! { isize },
223+
IntKind::Usize => syn::parse_quote! { usize },
222224
IntKind::Custom { name, .. } => {
223225
syn::parse_str(name).expect("Invalid integer type.")
224226
}

bindgen/ir/int.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ pub enum IntKind {
7878
/// A `uint128_t`.
7979
U128,
8080

81+
/// A pointer-sized signed integer.
82+
Isize,
83+
84+
/// A pointer-sized unsigned integer.
85+
Usize,
86+
8187
/// A custom integer type, used to allow custom macro types depending on
8288
/// range.
8389
Custom {
@@ -97,10 +103,10 @@ impl IntKind {
97103
// to know whether it is or not right now (unlike char, there's no
98104
// WChar_S / WChar_U).
99105
Bool | UChar | UShort | UInt | ULong | ULongLong | U8 | U16 |
100-
Char16 | WChar | U32 | U64 | U128 => false,
106+
Char16 | WChar | U32 | U64 | U128 | Usize => false,
101107

102108
SChar | Short | Int | Long | LongLong | I8 | I16 | I32 | I64 |
103-
I128 => true,
109+
I128 | Isize => true,
104110

105111
Char { is_signed } | Custom { is_signed, .. } => is_signed,
106112
}

0 commit comments

Comments
 (0)