Skip to content

Commit

Permalink
Replace trait/struct with a closure
Browse files Browse the repository at this point in the history
  • Loading branch information
ekr-cfa committed Nov 27, 2024
1 parent 9ef1e7c commit 5fb9d76
Showing 1 changed file with 15 additions and 36 deletions.
51 changes: 15 additions & 36 deletions src/people.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,9 +959,11 @@ impl ContextPeopleExt for Context {
}

T::setup(self);
let mut accumulator = PeopleAccumulatorVec::default();
self.query_people_internal(&mut accumulator, q.get_query());
accumulator.people
let mut result = Vec::new();
self.query_people_internal(| person | {
result.push(person);
}, q.get_query());
result
}

fn query_people_count<T: Query>(&self, q: T) -> usize {
Expand All @@ -971,9 +973,11 @@ impl ContextPeopleExt for Context {
}

T::setup(self);
let mut accumulator = PeopleAccumulatorCount::default();
self.query_people_internal(&mut accumulator, q.get_query());
accumulator.count
let mut count : usize = 0;
self.query_people_internal(| _person | {
count += 1;
}, q.get_query());
count
}

fn match_person<T: Query>(&self, person_id: PersonId, q: T) -> bool {
Expand Down Expand Up @@ -1014,31 +1018,6 @@ impl ContextPeopleExt for Context {
}
}

trait PeopleAccumulator {
fn add_person(&mut self, person_id: PersonId);
}

#[derive(Default)]
struct PeopleAccumulatorCount {
count: usize,
}

impl PeopleAccumulator for PeopleAccumulatorCount {
fn add_person(&mut self, _person_id: PersonId) {
self.count += 1;
}
}

#[derive(Default)]
struct PeopleAccumulatorVec {
people: Vec<PersonId>,
}

impl PeopleAccumulator for PeopleAccumulatorVec {
fn add_person(&mut self, person_id: PersonId) {
self.people.push(person_id);
}
}

trait ContextPeopleExtInternal {
fn register_indexer<T: PersonProperty + 'static>(&self);
Expand All @@ -1048,9 +1027,9 @@ trait ContextPeopleExtInternal {
person_id: PersonId,
property: T,
);
fn query_people_internal<T: PeopleAccumulator>(
fn query_people_internal(
&self,
accumulator: &mut T,
accumulator: impl FnMut(PersonId),
property_hashes: Vec<(TypeId, IndexValue)>,
);
}
Expand Down Expand Up @@ -1105,9 +1084,9 @@ impl ContextPeopleExtInternal for Context {
}
}

fn query_people_internal<T: PeopleAccumulator>(
fn query_people_internal(
&self,
accumulator: &mut T,
mut accumulator: impl FnMut(PersonId),
property_hashes: Vec<(TypeId, IndexValue)>,
) {
let mut indexes = Vec::<Ref<HashSet<PersonId>>>::new();
Expand Down Expand Up @@ -1173,7 +1152,7 @@ impl ContextPeopleExtInternal for Context {
}

// This matches.
accumulator.add_person(person);
accumulator(person);
}
}
}
Expand Down

0 comments on commit 5fb9d76

Please sign in to comment.