LazyQuery

Struct LazyQuery 

Source
pub struct LazyQuery<'a, T: 'static, I>
where I: Iterator<Item = &'a T>,
{ /* private fields */ }
Expand description

A lazy query builder that uses iterators for deferred execution.

Unlike the standard Query, LazyQuery doesn’t execute until you call a terminal operation like .collect(), .count(), or .first().

§Benefits

  • Deferred execution: No work until results needed
  • Iterator fusion: Rust optimizes chained operations
  • Early termination: .take() stops as soon as enough items found
  • Composable: Build complex queries by composition

§Example

// Nothing executes yet
let query = LazyQuery::new(&products)
    .where_(Product::price(), |&p| p < 100.0)
    .where_(Product::stock(), |&s| s > 0);

// Execution happens here
let results: Vec<_> = query.collect();

Implementations§

Source§

impl<'a, T: 'static> LazyQuery<'a, T, Iter<'a, T>>

Source

pub fn new(data: &'a [T]) -> Self

Creates a new lazy query from a slice.

§Example
let query = LazyQuery::new(&products);
Source§

impl<'a, T: 'static, I> LazyQuery<'a, T, I>
where I: Iterator<Item = &'a T>,

Source

pub fn from_iter(iter: I) -> Self

Creates a new lazy query from an iterator.

This is useful for creating LazyQuery instances from custom iterators or for implementing extension traits.

§Example
let iter = vec![1, 2, 3].iter();
let query = LazyQuery::from_iter(iter);
Source§

impl<'a, T: 'static, I> LazyQuery<'a, T, I>
where I: Iterator<Item = &'a T> + 'a,

Source

pub fn where_<F, P>( self, path: KeyPaths<T, F>, predicate: P, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where F: 'static, P: Fn(&F) -> bool + 'a,

Adds a filter predicate (lazy - not executed yet).

§Example
let query = LazyQuery::new(&products)
    .where_(Product::price(), |&p| p < 100.0);
Source

pub fn map_items<F, O>(self, f: F) -> impl Iterator<Item = O> + 'a
where F: Fn(&'a T) -> O + 'a, I: 'a,

Maps each item through a transformation (lazy).

§Example
let prices = LazyQuery::new(&products)
    .map_items(|p| p.price)
    .collect::<Vec<_>>();
Source

pub fn select_lazy<F>( self, path: KeyPaths<T, F>, ) -> impl Iterator<Item = F> + 'a
where F: Clone + 'static, I: 'a,

Selects/projects a field value (lazy).

Returns iterator over cloned field values.

§Example
let names: Vec<String> = LazyQuery::new(&products)
    .select_lazy(Product::name())
    .collect();
Source

pub fn take_lazy( self, n: usize, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where I: 'a,

Takes at most n items (lazy).

§Example
let first_10: Vec<_> = LazyQuery::new(&products)
    .take_lazy(10)
    .collect();
Source

pub fn skip_lazy( self, n: usize, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where I: 'a,

Skips n items (lazy).

§Example
let page_2: Vec<_> = LazyQuery::new(&products)
    .skip_lazy(10)
    .take_lazy(10)
    .collect();
Source

pub fn collect(self) -> Vec<&'a T>

Collects all items into a vector (terminal operation - executes query).

§Example
let results: Vec<&Product> = query.collect();
Source

pub fn first(self) -> Option<&'a T>

Gets the first item (terminal operation - executes until first match).

§Example
let first = query.first();
Source

pub fn count(self) -> usize

Counts items (terminal operation - executes query).

§Example
let count = query.count();
Source

pub fn any(self) -> bool

Checks if any items match (terminal operation - short-circuits).

§Example
let exists = query.any();
Source

pub fn for_each<F>(self, f: F)
where F: FnMut(&'a T),

Executes a function for each item (terminal operation).

§Example
query.for_each(|item| println!("{:?}", item));
Source

pub fn fold<B, F>(self, init: B, f: F) -> B
where F: FnMut(B, &'a T) -> B,

Folds the iterator (terminal operation).

§Example
let sum = query.fold(0.0, |acc, item| acc + item.price);
Source

pub fn find<P>(self, predicate: P) -> Option<&'a T>
where P: FnMut(&&'a T) -> bool,

Finds an item matching a predicate (terminal - short-circuits).

§Example
let found = query.find(|item| item.id == 42);
Source

pub fn all_match<P>(self, predicate: P) -> bool
where P: FnMut(&'a T) -> bool,

Checks if all items match a predicate (terminal - short-circuits).

§Example
let all_positive = query.all_match(|item| item.value > 0);
Source

pub fn all(self) -> Vec<&'a T>

Collects all items into a vector (terminal operation - executes query).

§Example
let results: Vec<&Product> = query.all();
Source

pub fn into_iter(self) -> I

Converts to a standard iterator for further chaining.

§Example
let custom: Vec<_> = query
    .into_iter()
    .map(|item| item.custom_transform())
    .filter(|x| x.is_valid())
    .collect();
Source§

impl<'a, T: 'static, I> LazyQuery<'a, T, I>
where I: Iterator<Item = &'a T> + 'a,

Source

pub fn sum_by<F>(self, path: KeyPaths<T, F>) -> F
where F: Clone + Add<Output = F> + Default + 'static, I: 'a,

Computes sum of a field (terminal operation).

§Example
let total: f64 = LazyQuery::new(&products)
    .sum_by(Product::price());
Source

pub fn avg_by(self, path: KeyPaths<T, f64>) -> Option<f64>
where I: 'a,

Computes average of a float field (terminal operation).

§Example
let avg = LazyQuery::new(&products)
    .avg_by(Product::price());
Source

pub fn min_by<F>(self, path: KeyPaths<T, F>) -> Option<F>
where F: Ord + Clone + 'static, I: 'a,

Finds minimum value of a field (terminal operation).

§Example
let min = LazyQuery::new(&products)
    .min_by(Product::price());
Source

pub fn max_by<F>(self, path: KeyPaths<T, F>) -> Option<F>
where F: Ord + Clone + 'static, I: 'a,

Finds maximum value of a field (terminal operation).

§Example
let max = LazyQuery::new(&products)
    .max_by(Product::price());
Source

pub fn min_by_float(self, path: KeyPaths<T, f64>) -> Option<f64>
where I: 'a,

Finds minimum float value (terminal operation).

Source

pub fn max_by_float(self, path: KeyPaths<T, f64>) -> Option<f64>
where I: 'a,

Finds maximum float value (terminal operation).

Source

pub fn where_after_systemtime( self, path: KeyPaths<T, SystemTime>, reference: SystemTime, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by SystemTime being after a reference time (lazy).

§Arguments
  • path - The key-path to the SystemTime field
  • reference - The reference time to compare against
§Example
let recent = LazyQuery::new(&events)
    .where_after_systemtime(Event::timestamp(), cutoff_time)
    .collect::<Vec<_>>();
Source

pub fn where_before_systemtime( self, path: KeyPaths<T, SystemTime>, reference: SystemTime, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by SystemTime being before a reference time (lazy).

§Arguments
  • path - The key-path to the SystemTime field
  • reference - The reference time to compare against
§Example
let old = LazyQuery::new(&events)
    .where_before_systemtime(Event::timestamp(), cutoff_time)
    .collect::<Vec<_>>();
Source

pub fn where_between_systemtime( self, path: KeyPaths<T, SystemTime>, start: SystemTime, end: SystemTime, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by SystemTime being between two times (inclusive, lazy).

§Arguments
  • path - The key-path to the SystemTime field
  • start - The start time
  • end - The end time
§Example
let range = LazyQuery::new(&events)
    .where_between_systemtime(Event::timestamp(), start, end)
    .collect::<Vec<_>>();
Source§

impl<'a, T: 'static, I> LazyQuery<'a, T, I>
where I: Iterator<Item = &'a T> + 'a,

Source

pub fn where_after<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, reference: DateTime<Tz>, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime being after a reference time (lazy).

§Arguments
  • path - The key-path to the DateTime field
  • reference - The reference time to compare against
§Example
let recent = LazyQuery::new(&events)
    .where_after(Event::timestamp(), cutoff_time)
    .collect::<Vec<_>>();
Source

pub fn where_before<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, reference: DateTime<Tz>, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime being before a reference time (lazy).

§Arguments
  • path - The key-path to the DateTime field
  • reference - The reference time to compare against
§Example
let old = LazyQuery::new(&events)
    .where_before(Event::timestamp(), cutoff_time)
    .collect::<Vec<_>>();
Source

pub fn where_between<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, start: DateTime<Tz>, end: DateTime<Tz>, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime being between two times (inclusive, lazy).

§Arguments
  • path - The key-path to the DateTime field
  • start - The start time
  • end - The end time
§Example
let range = LazyQuery::new(&events)
    .where_between(Event::timestamp(), start, end)
    .collect::<Vec<_>>();
Source

pub fn where_today<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, now: DateTime<Tz>, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime being today (lazy).

§Arguments
  • path - The key-path to the DateTime field
  • now - The current DateTime to compare against
§Example
let today = LazyQuery::new(&events)
    .where_today(Event::timestamp(), Utc::now())
    .collect::<Vec<_>>();
Source

pub fn where_year<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, year: i32, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime year (lazy).

§Arguments
  • path - The key-path to the DateTime field
  • year - The year to filter by
§Example
let this_year = LazyQuery::new(&events)
    .where_year(Event::timestamp(), 2024)
    .collect::<Vec<_>>();
Source

pub fn where_month<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, month: u32, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime month (lazy).

§Arguments
  • path - The key-path to the DateTime field
  • month - The month to filter by (1-12)
§Example
let december = LazyQuery::new(&events)
    .where_month(Event::timestamp(), 12)
    .collect::<Vec<_>>();
Source

pub fn where_day<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, day: u32, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by DateTime day (lazy).

§Arguments
  • path - The key-path to the DateTime field
  • day - The day to filter by (1-31)
§Example
let first = LazyQuery::new(&events)
    .where_day(Event::timestamp(), 1)
    .collect::<Vec<_>>();
Source

pub fn where_weekend<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by weekend dates (Saturday and Sunday, lazy).

§Arguments
  • path - The key-path to the DateTime field
§Example
let weekend_events = LazyQuery::new(&events)
    .where_weekend(Event::timestamp())
    .collect::<Vec<_>>();
Source

pub fn where_weekday<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by weekday dates (Monday through Friday, lazy).

§Arguments
  • path - The key-path to the DateTime field
§Example
let weekday_events = LazyQuery::new(&events)
    .where_weekday(Event::timestamp())
    .collect::<Vec<_>>();
Source

pub fn where_business_hours<Tz>( self, path: KeyPaths<T, DateTime<Tz>>, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>
where Tz: TimeZone + 'static, Tz::Offset: Display,

Filter by business hours (9 AM - 5 PM, lazy).

§Arguments
  • path - The key-path to the DateTime field
§Example
let business_hours = LazyQuery::new(&events)
    .where_business_hours(Event::timestamp())
    .collect::<Vec<_>>();
Source§

impl<'a, T: 'static, I> LazyQuery<'a, T, I>
where I: Iterator<Item = &'a T> + 'a,

Source

pub fn min_timestamp(self, path: KeyPaths<T, i64>) -> Option<i64>
where I: 'a,

Finds minimum i64 timestamp value (terminal operation).

§Example
let earliest = LazyQuery::new(&events)
    .min_timestamp(Event::created_at_r());
Source

pub fn max_timestamp(self, path: KeyPaths<T, i64>) -> Option<i64>
where I: 'a,

Finds maximum i64 timestamp value (terminal operation).

§Example
let latest = LazyQuery::new(&events)
    .max_timestamp(Event::created_at_r());
Source

pub fn avg_timestamp(self, path: KeyPaths<T, i64>) -> Option<i64>
where I: 'a,

Computes average of i64 timestamp values (terminal operation).

§Example
let avg = LazyQuery::new(&events)
    .avg_timestamp(Event::created_at_r());
Source

pub fn sum_timestamp(self, path: KeyPaths<T, i64>) -> i64
where I: 'a,

Computes sum of i64 timestamp values (terminal operation).

§Example
let total = LazyQuery::new(&events)
    .sum_timestamp(Event::created_at_r());
Source

pub fn count_timestamp(self, path: KeyPaths<T, i64>) -> usize
where I: 'a,

Counts i64 timestamp values (terminal operation).

§Example
let count = LazyQuery::new(&events)
    .count_timestamp(Event::created_at_r());
Source

pub fn where_after_timestamp( self, path: KeyPaths<T, i64>, reference: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being after a reference time (lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • reference - The reference timestamp to compare against
§Example
let recent = LazyQuery::new(&events)
    .where_after_timestamp(Event::created_at_r(), cutoff_time)
    .collect::<Vec<_>>();
Source

pub fn where_before_timestamp( self, path: KeyPaths<T, i64>, reference: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being before a reference time (lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • reference - The reference timestamp to compare against
§Example
let old = LazyQuery::new(&events)
    .where_before_timestamp(Event::created_at_r(), cutoff_time)
    .collect::<Vec<_>>();
Source

pub fn where_between_timestamp( self, path: KeyPaths<T, i64>, start: i64, end: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being between two times (inclusive, lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • start - The start timestamp
  • end - The end timestamp
§Example
let range = LazyQuery::new(&events)
    .where_between_timestamp(Event::created_at_r(), start, end)
    .collect::<Vec<_>>();
Source

pub fn where_last_days_timestamp( self, path: KeyPaths<T, i64>, days: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being within the last N days (lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • days - Number of days to look back
§Example
let recent = LazyQuery::new(&events)
    .where_last_days_timestamp(Event::created_at_r(), 30)
    .collect::<Vec<_>>();
Source

pub fn where_next_days_timestamp( self, path: KeyPaths<T, i64>, days: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being within the next N days (lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • days - Number of days to look forward
§Example
let upcoming = LazyQuery::new(&events)
    .where_next_days_timestamp(Event::scheduled_at_r(), 7)
    .collect::<Vec<_>>();
Source

pub fn where_last_hours_timestamp( self, path: KeyPaths<T, i64>, hours: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being within the last N hours (lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • hours - Number of hours to look back
§Example
let recent = LazyQuery::new(&events)
    .where_last_hours_timestamp(Event::created_at_r(), 24)
    .collect::<Vec<_>>();
Source

pub fn where_next_hours_timestamp( self, path: KeyPaths<T, i64>, hours: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being within the next N hours (lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • hours - Number of hours to look forward
§Example
let upcoming = LazyQuery::new(&events)
    .where_next_hours_timestamp(Event::scheduled_at_r(), 2)
    .collect::<Vec<_>>();
Source

pub fn where_last_minutes_timestamp( self, path: KeyPaths<T, i64>, minutes: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being within the last N minutes (lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • minutes - Number of minutes to look back
§Example
let recent = LazyQuery::new(&events)
    .where_last_minutes_timestamp(Event::created_at_r(), 60)
    .collect::<Vec<_>>();
Source

pub fn where_next_minutes_timestamp( self, path: KeyPaths<T, i64>, minutes: i64, ) -> LazyQuery<'a, T, impl Iterator<Item = &'a T> + 'a>

Filter by i64 timestamp being within the next N minutes (lazy).

§Arguments
  • path - The key-path to the i64 timestamp field
  • minutes - Number of minutes to look forward
§Example
let upcoming = LazyQuery::new(&events)
    .where_next_minutes_timestamp(Event::scheduled_at_r(), 30)
    .collect::<Vec<_>>();

Trait Implementations§

Source§

impl<'a, T: 'static, I> IntoIterator for LazyQuery<'a, T, I>
where I: Iterator<Item = &'a T>,

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = I

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl<'a, T, I> Freeze for LazyQuery<'a, T, I>
where I: Freeze,

§

impl<'a, T, I> RefUnwindSafe for LazyQuery<'a, T, I>

§

impl<'a, T, I> Send for LazyQuery<'a, T, I>
where I: Send, T: Sync,

§

impl<'a, T, I> Sync for LazyQuery<'a, T, I>
where I: Sync, T: Sync,

§

impl<'a, T, I> Unpin for LazyQuery<'a, T, I>
where I: Unpin,

§

impl<'a, T, I> UnwindSafe for LazyQuery<'a, T, I>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.