Skip to content

Commit 3842b11

Browse files
pvdrzemilio
authored andcommitted
replace the TraversalPredicate trait with an alias type
1 parent 2aa244f commit 3842b11

File tree

2 files changed

+15
-51
lines changed

2 files changed

+15
-51
lines changed

src/ir/context.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -467,13 +467,7 @@ pub struct BindgenContext {
467467
/// A traversal of allowlisted items.
468468
struct AllowlistedItemsTraversal<'ctx> {
469469
ctx: &'ctx BindgenContext,
470-
#[allow(clippy::type_complexity)]
471-
traversal: ItemTraversal<
472-
'ctx,
473-
ItemSet,
474-
Vec<ItemId>,
475-
for<'a> fn(&'a BindgenContext, Edge) -> bool,
476-
>,
470+
traversal: ItemTraversal<'ctx, ItemSet, Vec<ItemId>>,
477471
}
478472

479473
impl<'ctx> Iterator for AllowlistedItemsTraversal<'ctx> {

src/ir/traversal.rs

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -179,17 +179,10 @@ pub enum EdgeKind {
179179

180180
/// A predicate to allow visiting only sub-sets of the whole IR graph by
181181
/// excluding certain edges from being followed by the traversal.
182-
pub trait TraversalPredicate {
183-
/// Should the traversal follow this edge, and visit everything that is
184-
/// reachable through it?
185-
fn should_follow(&self, ctx: &BindgenContext, edge: Edge) -> bool;
186-
}
187-
188-
impl TraversalPredicate for for<'a> fn(&'a BindgenContext, Edge) -> bool {
189-
fn should_follow(&self, ctx: &BindgenContext, edge: Edge) -> bool {
190-
(*self)(ctx, edge)
191-
}
192-
}
182+
///
183+
/// The predicate must return true if the traversal should follow this edge
184+
/// and visit everything that is reachable through it.
185+
pub type TraversalPredicate = for<'a> fn(&'a BindgenContext, Edge) -> bool;
193186

194187
/// A `TraversalPredicate` implementation that follows all edges, and therefore
195188
/// traversals using this predicate will see the whole IR graph reachable from
@@ -378,11 +371,10 @@ pub trait Trace {
378371
/// An graph traversal of the transitive closure of references between items.
379372
///
380373
/// See `BindgenContext::allowlisted_items` for more information.
381-
pub struct ItemTraversal<'ctx, Storage, Queue, Predicate>
374+
pub struct ItemTraversal<'ctx, Storage, Queue>
382375
where
383376
Storage: TraversalStorage<'ctx>,
384377
Queue: TraversalQueue,
385-
Predicate: TraversalPredicate,
386378
{
387379
ctx: &'ctx BindgenContext,
388380

@@ -393,25 +385,23 @@ where
393385
queue: Queue,
394386

395387
/// The predicate that determines which edges this traversal will follow.
396-
predicate: Predicate,
388+
predicate: TraversalPredicate,
397389

398390
/// The item we are currently traversing.
399391
currently_traversing: Option<ItemId>,
400392
}
401393

402-
impl<'ctx, Storage, Queue, Predicate>
403-
ItemTraversal<'ctx, Storage, Queue, Predicate>
394+
impl<'ctx, Storage, Queue> ItemTraversal<'ctx, Storage, Queue>
404395
where
405396
Storage: TraversalStorage<'ctx>,
406397
Queue: TraversalQueue,
407-
Predicate: TraversalPredicate,
408398
{
409399
/// Begin a new traversal, starting from the given roots.
410400
pub fn new<R>(
411401
ctx: &'ctx BindgenContext,
412402
roots: R,
413-
predicate: Predicate,
414-
) -> ItemTraversal<'ctx, Storage, Queue, Predicate>
403+
predicate: TraversalPredicate,
404+
) -> ItemTraversal<'ctx, Storage, Queue>
415405
where
416406
R: IntoIterator<Item = ItemId>,
417407
{
@@ -433,16 +423,14 @@ where
433423
}
434424
}
435425

436-
impl<'ctx, Storage, Queue, Predicate> Tracer
437-
for ItemTraversal<'ctx, Storage, Queue, Predicate>
426+
impl<'ctx, Storage, Queue> Tracer for ItemTraversal<'ctx, Storage, Queue>
438427
where
439428
Storage: TraversalStorage<'ctx>,
440429
Queue: TraversalQueue,
441-
Predicate: TraversalPredicate,
442430
{
443431
fn visit_kind(&mut self, item: ItemId, kind: EdgeKind) {
444432
let edge = Edge::new(item, kind);
445-
if !self.predicate.should_follow(self.ctx, edge) {
433+
if !(self.predicate)(self.ctx, edge) {
446434
return;
447435
}
448436

@@ -454,12 +442,10 @@ where
454442
}
455443
}
456444

457-
impl<'ctx, Storage, Queue, Predicate> Iterator
458-
for ItemTraversal<'ctx, Storage, Queue, Predicate>
445+
impl<'ctx, Storage, Queue> Iterator for ItemTraversal<'ctx, Storage, Queue>
459446
where
460447
Storage: TraversalStorage<'ctx>,
461448
Queue: TraversalQueue,
462-
Predicate: TraversalPredicate,
463449
{
464450
type Item = ItemId;
465451

@@ -488,21 +474,5 @@ where
488474
///
489475
/// See `BindgenContext::assert_no_dangling_item_traversal` for more
490476
/// information.
491-
pub type AssertNoDanglingItemsTraversal<'ctx> = ItemTraversal<
492-
'ctx,
493-
Paths<'ctx>,
494-
VecDeque<ItemId>,
495-
for<'a> fn(&'a BindgenContext, Edge) -> bool,
496-
>;
497-
498-
#[cfg(test)]
499-
mod tests {
500-
use super::*;
501-
502-
#[test]
503-
#[allow(dead_code)]
504-
fn traversal_predicate_is_object_safe() {
505-
// This should compile only if TraversalPredicate is object safe.
506-
fn takes_by_trait_object(_: &dyn TraversalPredicate) {}
507-
}
508-
}
477+
pub type AssertNoDanglingItemsTraversal<'ctx> =
478+
ItemTraversal<'ctx, Paths<'ctx>, VecDeque<ItemId>>;

0 commit comments

Comments
 (0)