Skip to content

Commit a0e612b

Browse files
committed
fixup: use methods to compose callbacks
1 parent ecd5604 commit a0e612b

File tree

7 files changed

+40
-26
lines changed

7 files changed

+40
-26
lines changed

bindgen/codegen/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,9 @@ impl CodeGenerator for CompInfo {
21122112

21132113
// The custom derives callback may return a list of derive attributes;
21142114
// add them to the end of the list.
2115-
let custom_derives = ctx.options().parse_callbacks.iter().flat_map(|cb| cb.add_derives(&canonical_name)).collect::<Vec<String>>();
2115+
let custom_derives = ctx
2116+
.options()
2117+
.all_callbacks(|cb| cb.add_derives(&canonical_name));
21162118
// In most cases this will be a no-op, since custom_derives will be empty.
21172119
derives.extend(custom_derives.iter().map(|s| s.as_str()));
21182120

@@ -3149,7 +3151,8 @@ impl CodeGenerator for Enum {
31493151

31503152
// The custom derives callback may return a list of derive attributes;
31513153
// add them to the end of the list.
3152-
let custom_derives = ctx.options().parse_callbacks.iter().flat_map(|cb| cb.add_derives(&name)).collect::<Vec<String>>();
3154+
let custom_derives =
3155+
ctx.options().all_callbacks(|cb| cb.add_derives(&name));
31533156
// In most cases this will be a no-op, since custom_derives will be empty.
31543157
derives.extend(custom_derives.iter().map(|s| s.as_str()));
31553158

bindgen/ir/context.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use super::module::{Module, ModuleKind};
1919
use super::template::{TemplateInstantiation, TemplateParameters};
2020
use super::traversal::{self, Edge, ItemTraversal};
2121
use super::ty::{FloatKind, Type, TypeKind};
22-
use crate::callbacks::ParseCallbacks;
2322
use crate::clang::{self, Cursor};
2423
use crate::parse::ClangItemParser;
2524
use crate::BindgenOptions;
@@ -619,14 +618,9 @@ If you encounter an error missing from this list, please file an issue or a PR!"
619618
)
620619
}
621620

622-
/// Get the user-provided callbacks by reference, if any.
623-
pub fn parse_callbacks(&self) -> impl Iterator<Item = &dyn ParseCallbacks> {
624-
self.options().parse_callbacks.iter().map(|cb| cb.as_ref())
625-
}
626-
627621
/// Add another path to the set of included files.
628622
pub fn include_file(&mut self, filename: String) {
629-
for cb in self.parse_callbacks() {
623+
for cb in &self.options().parse_callbacks {
630624
cb.include_file(&filename);
631625
}
632626
self.deps.insert(filename);
@@ -2251,7 +2245,7 @@ If you encounter an error missing from this list, please file an issue or a PR!"
22512245
Some(CanDerive::No)
22522246
}
22532247
} else {
2254-
self.options.parse_callbacks.iter().find_map(|cb| {
2248+
self.options.last_callback(|cb| {
22552249
cb.blocklisted_type_implements_trait(
22562250
name,
22572251
derive_trait,

bindgen/ir/enum_ty.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ impl Enum {
102102
let name = cursor.spelling();
103103
let annotations = Annotations::new(&cursor);
104104
let custom_behavior = ctx
105-
.parse_callbacks()
106-
.find_map(|callbacks| {
105+
.options()
106+
.last_callback(|callbacks| {
107107
callbacks
108108
.enum_variant_behavior(type_name, &name, val)
109109
})
@@ -119,8 +119,8 @@ impl Enum {
119119
});
120120

121121
let new_name = ctx
122-
.parse_callbacks()
123-
.find_map(|callbacks| {
122+
.options()
123+
.last_callback(|callbacks| {
124124
callbacks.enum_variant_name(type_name, &name, val)
125125
})
126126
.or_else(|| {

bindgen/ir/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -665,8 +665,8 @@ impl ClangSubItemParser for Function {
665665
name.push_str("_destructor");
666666
}
667667
if let Some(nm) = context
668-
.parse_callbacks()
669-
.find_map(|callbacks| callbacks.generated_name_override(&name))
668+
.options()
669+
.last_callback(|callbacks| callbacks.generated_name_override(&name))
670670
{
671671
name = nm;
672672
}

bindgen/ir/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,8 +932,8 @@ impl Item {
932932
let name = names.join("_");
933933

934934
let name = if opt.user_mangled == UserMangled::Yes {
935-
ctx.parse_callbacks()
936-
.find_map(|callbacks| callbacks.item_name(&name))
935+
ctx.options()
936+
.last_callback(|callbacks| callbacks.item_name(&name))
937937
.unwrap_or(name)
938938
} else {
939939
name

bindgen/ir/var.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ impl ClangSubItemParser for Var {
182182
use clang_sys::*;
183183
match cursor.kind() {
184184
CXCursor_MacroDefinition => {
185-
for callbacks in ctx.parse_callbacks() {
185+
for callbacks in &ctx.options().parse_callbacks {
186186
match callbacks.will_parse_macro(&cursor.spelling()) {
187187
MacroParsingBehavior::Ignore => {
188188
return Err(ParseError::Continue);
@@ -191,7 +191,7 @@ impl ClangSubItemParser for Var {
191191
}
192192

193193
if cursor.is_macro_function_like() {
194-
handle_function_macro(&cursor, callbacks);
194+
handle_function_macro(&cursor, callbacks.as_ref());
195195
// We handled the macro, skip macro processing below.
196196
return Err(ParseError::Continue);
197197
}
@@ -249,15 +249,15 @@ impl ClangSubItemParser for Var {
249249
true,
250250
ctx,
251251
);
252-
for callbacks in ctx.parse_callbacks() {
252+
for callbacks in &ctx.options().parse_callbacks {
253253
callbacks.str_macro(&name, &val);
254254
}
255255
(TypeKind::Pointer(char_ty), VarType::String(val))
256256
}
257257
EvalResult::Int(Wrapping(value)) => {
258258
let kind = ctx
259-
.parse_callbacks()
260-
.find_map(|c| c.int_macro(&name, value))
259+
.options()
260+
.last_callback(|c| c.int_macro(&name, value))
261261
.unwrap_or_else(|| {
262262
default_macro_constant_type(ctx, value)
263263
});

bindgen/lib.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,9 +2146,6 @@ impl BindgenOptions {
21462146
for regex_set in &mut regex_sets {
21472147
regex_set.build(record_matches);
21482148
}
2149-
2150-
// Reverse parse callbacks to have a last-to-first priority.
2151-
self.parse_callbacks.reverse();
21522149
}
21532150

21542151
/// Update rust target version
@@ -2163,6 +2160,26 @@ impl BindgenOptions {
21632160
pub fn rust_features(&self) -> RustFeatures {
21642161
self.rust_features
21652162
}
2163+
2164+
fn last_callback<T>(
2165+
&self,
2166+
f: impl Fn(&dyn crate::callbacks::ParseCallbacks) -> Option<T>,
2167+
) -> Option<T> {
2168+
self.parse_callbacks
2169+
.iter()
2170+
.filter_map(|cb| f(cb.as_ref()))
2171+
.last()
2172+
}
2173+
2174+
fn all_callbacks<T>(
2175+
&self,
2176+
f: impl Fn(&dyn crate::callbacks::ParseCallbacks) -> Vec<T>,
2177+
) -> Vec<T> {
2178+
self.parse_callbacks
2179+
.iter()
2180+
.flat_map(|cb| f(cb.as_ref()))
2181+
.collect()
2182+
}
21662183
}
21672184

21682185
impl Default for BindgenOptions {

0 commit comments

Comments
 (0)