Skip to content

Commit

Permalink
replace the TraversalPredicate trait with an alias type
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdrz authored and emilio committed Sep 5, 2022
1 parent 2aa244f commit 3842b11
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 51 deletions.
8 changes: 1 addition & 7 deletions src/ir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,13 +467,7 @@ pub struct BindgenContext {
/// A traversal of allowlisted items.
struct AllowlistedItemsTraversal<'ctx> {
ctx: &'ctx BindgenContext,
#[allow(clippy::type_complexity)]
traversal: ItemTraversal<
'ctx,
ItemSet,
Vec<ItemId>,
for<'a> fn(&'a BindgenContext, Edge) -> bool,
>,
traversal: ItemTraversal<'ctx, ItemSet, Vec<ItemId>>,
}

impl<'ctx> Iterator for AllowlistedItemsTraversal<'ctx> {
Expand Down
58 changes: 14 additions & 44 deletions src/ir/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,17 +179,10 @@ pub enum EdgeKind {

/// A predicate to allow visiting only sub-sets of the whole IR graph by
/// excluding certain edges from being followed by the traversal.
pub trait TraversalPredicate {
/// Should the traversal follow this edge, and visit everything that is
/// reachable through it?
fn should_follow(&self, ctx: &BindgenContext, edge: Edge) -> bool;
}

impl TraversalPredicate for for<'a> fn(&'a BindgenContext, Edge) -> bool {
fn should_follow(&self, ctx: &BindgenContext, edge: Edge) -> bool {
(*self)(ctx, edge)
}
}
///
/// The predicate must return true if the traversal should follow this edge
/// and visit everything that is reachable through it.
pub type TraversalPredicate = for<'a> fn(&'a BindgenContext, Edge) -> bool;

/// A `TraversalPredicate` implementation that follows all edges, and therefore
/// traversals using this predicate will see the whole IR graph reachable from
Expand Down Expand Up @@ -378,11 +371,10 @@ pub trait Trace {
/// An graph traversal of the transitive closure of references between items.
///
/// See `BindgenContext::allowlisted_items` for more information.
pub struct ItemTraversal<'ctx, Storage, Queue, Predicate>
pub struct ItemTraversal<'ctx, Storage, Queue>
where
Storage: TraversalStorage<'ctx>,
Queue: TraversalQueue,
Predicate: TraversalPredicate,
{
ctx: &'ctx BindgenContext,

Expand All @@ -393,25 +385,23 @@ where
queue: Queue,

/// The predicate that determines which edges this traversal will follow.
predicate: Predicate,
predicate: TraversalPredicate,

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

impl<'ctx, Storage, Queue, Predicate>
ItemTraversal<'ctx, Storage, Queue, Predicate>
impl<'ctx, Storage, Queue> ItemTraversal<'ctx, Storage, Queue>
where
Storage: TraversalStorage<'ctx>,
Queue: TraversalQueue,
Predicate: TraversalPredicate,
{
/// Begin a new traversal, starting from the given roots.
pub fn new<R>(
ctx: &'ctx BindgenContext,
roots: R,
predicate: Predicate,
) -> ItemTraversal<'ctx, Storage, Queue, Predicate>
predicate: TraversalPredicate,
) -> ItemTraversal<'ctx, Storage, Queue>
where
R: IntoIterator<Item = ItemId>,
{
Expand All @@ -433,16 +423,14 @@ where
}
}

impl<'ctx, Storage, Queue, Predicate> Tracer
for ItemTraversal<'ctx, Storage, Queue, Predicate>
impl<'ctx, Storage, Queue> Tracer for ItemTraversal<'ctx, Storage, Queue>
where
Storage: TraversalStorage<'ctx>,
Queue: TraversalQueue,
Predicate: TraversalPredicate,
{
fn visit_kind(&mut self, item: ItemId, kind: EdgeKind) {
let edge = Edge::new(item, kind);
if !self.predicate.should_follow(self.ctx, edge) {
if !(self.predicate)(self.ctx, edge) {
return;
}

Expand All @@ -454,12 +442,10 @@ where
}
}

impl<'ctx, Storage, Queue, Predicate> Iterator
for ItemTraversal<'ctx, Storage, Queue, Predicate>
impl<'ctx, Storage, Queue> Iterator for ItemTraversal<'ctx, Storage, Queue>
where
Storage: TraversalStorage<'ctx>,
Queue: TraversalQueue,
Predicate: TraversalPredicate,
{
type Item = ItemId;

Expand Down Expand Up @@ -488,21 +474,5 @@ where
///
/// See `BindgenContext::assert_no_dangling_item_traversal` for more
/// information.
pub type AssertNoDanglingItemsTraversal<'ctx> = ItemTraversal<
'ctx,
Paths<'ctx>,
VecDeque<ItemId>,
for<'a> fn(&'a BindgenContext, Edge) -> bool,
>;

#[cfg(test)]
mod tests {
use super::*;

#[test]
#[allow(dead_code)]
fn traversal_predicate_is_object_safe() {
// This should compile only if TraversalPredicate is object safe.
fn takes_by_trait_object(_: &dyn TraversalPredicate) {}
}
}
pub type AssertNoDanglingItemsTraversal<'ctx> =
ItemTraversal<'ctx, Paths<'ctx>, VecDeque<ItemId>>;

0 comments on commit 3842b11

Please sign in to comment.