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_r(), |&p| p < 100.0)
    .where_(Product::stock_r(), |&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_r(), |&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_r())
    .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_r());
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_r());
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_r());
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_r());
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_r(), 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_r(), 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_r(), start, end)
    .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.