Skip to content

Commit

Permalink
Use a bound method instead of a free function for param search
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaBluejay committed Sep 1, 2023
1 parent a7e0243 commit c592579
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
8 changes: 3 additions & 5 deletions impl/src/as/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ use syn::{
Token,
};

use crate::utils::{
contains_any_of, forward, skip, Either, HashSet, ParamSearch, Spanning,
};
use crate::utils::{forward, skip, Either, HashSet, ParamSearch, Spanning};

/// Expands an [`AsRef`]/[`AsMut`] derive macro.
pub fn expand(
Expand Down Expand Up @@ -212,7 +210,7 @@ impl<'a> ToTokens for Expansion<'a> {
param_consts,
};

let field_contains_param = contains_any_of(field_ty, &param_search);
let field_contains_param = param_search.any_in(field_ty);

let is_blanket = matches!(&self.args, Some(AsArgs::Forward(_)));

Expand Down Expand Up @@ -248,7 +246,7 @@ impl<'a> ToTokens for Expansion<'a> {
break 'ver Direct;
}

if field_contains_param || contains_any_of(&return_ty, &param_search) {
if field_contains_param || param_search.any_in(&return_ty) {
break 'ver Forwarded;
}

Expand Down
23 changes: 12 additions & 11 deletions impl/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) use self::fields_ext::FieldsExt;
pub(crate) use self::spanning::Spanning;

#[cfg(feature = "as_ref")]
pub(crate) use self::param_search::{contains_any_of, ParamSearch};
pub(crate) use self::param_search::ParamSearch;

#[derive(Clone, Copy, Default)]
pub struct DeterministicState;
Expand Down Expand Up @@ -1697,6 +1697,17 @@ mod param_search {
pub param_consts: HashSet<&'a syn::Ident>,
}

impl<'a> ParamSearch<'a> {
pub(crate) fn any_in(&self, ty: &syn::Type) -> bool {
let mut visitor = TypeSearchVisitor {
search: self,
found: false,
};
visitor.visit_type(ty);
visitor.found
}
}

struct TypeSearchVisitor<'a> {
search: &'a ParamSearch<'a>,
found: bool,
Expand Down Expand Up @@ -1739,14 +1750,4 @@ mod param_search {
syn::visit::visit_expr_path(self, ep)
}
}

pub(crate) fn contains_any_of(ty: &syn::Type, search: &ParamSearch<'_>) -> bool {
let mut visitor = TypeSearchVisitor {
search,
found: false,
};

visitor.visit_type(ty);
visitor.found
}
}

0 comments on commit c592579

Please sign in to comment.