Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Jan 1, 2025
1 parent c84d09a commit c15e36a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 68 deletions.
113 changes: 46 additions & 67 deletions src/tools/rust-analyzer/crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5219,49 +5219,25 @@ impl Type {
traits_in_scope: &FxHashSet<TraitId>,
with_local_impls: Option<Module>,
name: Option<&Name>,
callback: impl FnMut(Function) -> Option<T>,
mut callback: impl FnMut(Function) -> Option<T>,
) -> Option<T> {
struct Callback<T, F> {
f: F,
slot: Option<T>,
}
impl<T, F> MethodCandidateCallback for &'_ mut Callback<T, F>
where
F: FnMut(Function) -> Option<T>,
{
fn on_inherent_method(&mut self, f: Function) -> ControlFlow<()> {
match (self.f)(f) {
it @ Some(_) => {
self.slot = it;
ControlFlow::Break(())
}
None => ControlFlow::Continue(()),
}
}

fn on_trait_method(&mut self, f: Function) -> ControlFlow<()> {
match (self.f)(f) {
it @ Some(_) => {
self.slot = it;
ControlFlow::Break(())
}
None => ControlFlow::Continue(()),
}
}
}

let _p = tracing::info_span!("iterate_method_candidates_with_traits").entered();
let mut callback = Callback { slot: None, f: callback };

let mut slot = None;
self.iterate_method_candidates_split_inherent(
db,
scope,
traits_in_scope,
with_local_impls,
name,
&mut callback,
|f| match callback(f) {
it @ Some(_) => {
slot = it;
ControlFlow::Break(())
}
None => ControlFlow::Continue(()),
},
);
callback.slot
slot
}

pub fn iterate_method_candidates<T>(
Expand Down Expand Up @@ -5361,49 +5337,26 @@ impl Type {
traits_in_scope: &FxHashSet<TraitId>,
with_local_impls: Option<Module>,
name: Option<&Name>,
callback: impl FnMut(AssocItem) -> Option<T>,
mut callback: impl FnMut(AssocItem) -> Option<T>,
) -> Option<T> {
struct Callback<T, F> {
f: F,
slot: Option<T>,
}
impl<T, F> PathCandidateCallback for &'_ mut Callback<T, F>
where
F: FnMut(AssocItem) -> Option<T>,
{
fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()> {
match (self.f)(item) {
it @ Some(_) => {
self.slot = it;
ControlFlow::Break(())
}
None => ControlFlow::Continue(()),
}
}

fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()> {
match (self.f)(item) {
it @ Some(_) => {
self.slot = it;
ControlFlow::Break(())
}
None => ControlFlow::Continue(()),
}
}
}

let _p = tracing::info_span!("iterate_path_candidates").entered();
let mut callback = Callback { slot: None, f: callback };
let mut slot = None;

self.iterate_path_candidates_split_inherent(
db,
scope,
traits_in_scope,
with_local_impls,
name,
&mut callback,
|item| match callback(item) {
it @ Some(_) => {
slot = it;
ControlFlow::Break(())
}
None => ControlFlow::Continue(()),
},
);
callback.slot
slot
}

/// Iterates over inherent methods.
Expand Down Expand Up @@ -6167,8 +6120,34 @@ pub trait MethodCandidateCallback {
fn on_trait_method(&mut self, f: Function) -> ControlFlow<()>;
}

impl<F> MethodCandidateCallback for F
where
F: FnMut(Function) -> ControlFlow<()>,
{
fn on_inherent_method(&mut self, f: Function) -> ControlFlow<()> {
self(f)
}

fn on_trait_method(&mut self, f: Function) -> ControlFlow<()> {
self(f)
}
}

pub trait PathCandidateCallback {
fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()>;

fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()>;
}

impl<F> PathCandidateCallback for F
where
F: FnMut(AssocItem) -> ControlFlow<()>,
{
fn on_inherent_item(&mut self, item: AssocItem) -> ControlFlow<()> {
self(item)
}

fn on_trait_item(&mut self, item: AssocItem) -> ControlFlow<()> {
self(item)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ where
ControlFlow::Continue(())
}

#[allow(unstable_name_collisions)] // FIXME: Remove this when `is_none_or()` reaches stable.
fn on_trait_item(&mut self, item: hir::AssocItem) -> ControlFlow<()> {
// The excluded check needs to come before the `seen` test, so that if we see the same method twice,
// once as inherent and once not, we will include it.
Expand Down

0 comments on commit c15e36a

Please sign in to comment.