LockLazyQuery

Struct LockLazyQuery 

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

Lazy query for locked data with early termination.

Implementations§

Source§

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

Source

pub fn new(iter: I) -> Self

Create a new lazy query from an iterator of locks.

Source

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

Filter using a key-path predicate (lazy).

Source

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

Map to a field value (lazy).

This allows you to select only specific fields from locked data without cloning the entire object. Perfect for projecting data efficiently.

§Example
// Select only product names (not full objects)
let names: Vec<String> = products
    .lock_lazy_query()
    .where_(Product::price_r(), |&p| p > 100.0)
    .select_lazy(Product::name_r())
    .collect();
 
// Select only IDs  
let ids: Vec<u32> = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .select_lazy(Product::id_r())
    .take(10)
    .collect();
 
// Select prices and compute sum
let total: f64 = products
    .lock_lazy_query()
    .where_(Product::category_r(), |c| c == "Electronics")
    .select_lazy(Product::price_r())
    .sum();

Performance Note: This is much more efficient than collecting full objects and then extracting fields, as it only clones the specific field value.

Source

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

Take first N items (lazy).

Source

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

Skip first N items (lazy).

Source

pub fn count(self) -> usize

Count matching items (terminal).

Source

pub fn first(self) -> Option<T>
where T: Clone,

Get first matching item (terminal).

Source

pub fn any(self) -> bool

Check if any items match (terminal).

Source

pub fn collect(self) -> Vec<T>
where T: Clone,

Collect into Vec (terminal).

Source

pub fn all(self) -> Vec<T>
where T: Clone,

Get all matching items (alias for collect, similar to LockQuery::all).

This provides a familiar API for users coming from LockQuery.

§Example
let all_items: Vec<Product> = products
    .lock_lazy_query()
    .where_(Product::price_r(), |&p| p > 100.0)
    .all();
Source

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

Sum a numeric field (terminal).

Efficiently computes the sum without collecting all items into a Vec first.

§Example
// Sum all prices
let total_value: f64 = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .sum(Product::price_r());
 
// Sum stock quantities
let total_stock: u32 = products
    .lock_lazy_query()
    .where_(Product::category_r(), |c| c == "Electronics")
    .sum(Product::stock_r());
Source

pub fn avg(self, path: KeyPaths<T, f64>) -> Option<f64>

Calculate average of f64 field (terminal).

Returns None if no items match.

§Example
let avg_price = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .avg(Product::price_r());
 
match avg_price {
    Some(avg) => println!("Average price: ${:.2}", avg),
    None => println!("No items found"),
}
Source

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

Find minimum value (terminal).

Returns None if no items match.

§Example
let min_stock = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .min(Product::stock_r());
 
println!("Minimum stock level: {:?}", min_stock);
Source

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

Find maximum value (terminal).

Returns None if no items match.

§Example
let max_price = products
    .lock_lazy_query()
    .where_(Product::category_r(), |c| c == "Electronics")
    .max(Product::price_r());
 
println!("Most expensive: ${:.2}", max_price.unwrap_or(0.0));
Source

pub fn min_float(self, path: KeyPaths<T, f64>) -> Option<f64>

Find minimum float value (terminal).

Handles f64 values correctly with partial ordering.

§Example
let cheapest = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .min_float(Product::price_r());
Source

pub fn max_float(self, path: KeyPaths<T, f64>) -> Option<f64>

Find maximum float value (terminal).

Handles f64 values correctly with partial ordering.

§Example
let most_expensive = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .max_float(Product::price_r());
Source

pub fn exists(self) -> bool

Check if any items exist matching the criteria (terminal).

Alias for any(), provides SQL-like EXISTS semantics. Stops at the first match - very efficient!

§Example
let has_expensive = products
    .lock_lazy_query()
    .where_(Product::price_r(), |&p| p > 1000.0)
    .exists();
 
if has_expensive {
    println!("We have luxury items!");
}
 
// SQL equivalent: SELECT EXISTS(SELECT 1 FROM products WHERE price > 1000)
Source

pub fn limit(self, n: usize) -> impl Iterator<Item = T> + 'a
where T: Clone,

Limit results to first N items (lazy).

Alias for creating a limited iterator. Use with .collect() or .all().

§Example
let top_5: Vec<Product> = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 10)
    .limit(5)
    .collect();
 
// SQL equivalent: SELECT * FROM products WHERE stock > 10 LIMIT 5
Source

pub fn skip( self, n: usize, ) -> LockLazyQuery<'a, T, L, impl Iterator<Item = &'a L> + 'a>

Skip first N items (lazy).

Alias for skip_lazy() with better SQL-like naming.

§Example
// Get second page (skip 10, take 10)
let page_2 = products
    .lock_lazy_query()
    .skip(10)
    .limit(10)
    .collect();
 
// SQL equivalent: SELECT * FROM products LIMIT 10 OFFSET 10
Source

pub fn distinct<F>(self, path: KeyPaths<T, F>) -> Vec<F>
where F: Eq + Hash + Clone + 'static,

Get distinct values for a field (terminal).

Returns a Vec of unique field values. Uses HashSet internally.

§Example
let categories: Vec<String> = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .distinct(Product::category_r());
 
println!("Available categories: {:?}", categories);
 
// SQL equivalent: SELECT DISTINCT category FROM products WHERE stock > 0
Source

pub fn last(self) -> Option<T>
where T: Clone,

Get last matching item (terminal).

Note: This consumes the entire iterator to find the last item. Less efficient than first() for lazy evaluation.

§Example
let last_product = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .last();
Source

pub fn nth(self, n: usize) -> Option<T>
where T: Clone,

Find item at specific index (terminal).

Returns None if index is out of bounds.

§Example
let third_item = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .nth(2);  // 0-indexed, so this is the 3rd item
Source

pub fn all_match<F, P>(self, path: KeyPaths<T, F>, predicate: P) -> bool
where F: 'static, P: Fn(&F) -> bool + 'a,

Check if all items match a predicate (terminal).

Returns true if all items match, false otherwise. Short-circuits on first non-match.

§Example
let all_in_stock = products
    .lock_lazy_query()
    .where_(Product::category_r(), |c| c == "Electronics")
    .all_match(Product::stock_r(), |&s| s > 0);
 
if all_in_stock {
    println!("All electronics are in stock!");
}
Source

pub fn find<F, P>(self, path: KeyPaths<T, F>, predicate: P) -> Option<T>
where F: 'static, P: Fn(&F) -> bool + 'a, T: Clone,

Find first item matching an additional predicate (terminal).

Like first() but with an extra condition.

§Example
let expensive_laptop = products
    .lock_lazy_query()
    .where_(Product::category_r(), |c| c == "Electronics")
    .find(Product::price_r(), |&p| p > 500.0);
Source

pub fn count_where<F, P>(self, path: KeyPaths<T, F>, predicate: P) -> usize
where F: 'static, P: Fn(&F) -> bool + 'a,

Count items matching an additional condition (terminal).

Like count() but with a field-specific predicate.

§Example
let expensive_count = products
    .lock_lazy_query()
    .where_(Product::category_r(), |c| c == "Electronics")
    .count_where(Product::price_r(), |&p| p > 500.0);
Source

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

Filter by SystemTime being after a reference time.

§Arguments
  • path - The key-path to the SystemTime field
  • reference - The reference time to compare against
§Example
let recent = events
    .lock_lazy_query()
    .where_after_systemtime(Event::timestamp_r(), cutoff_time);
Source

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

Filter by SystemTime being before a reference time.

§Arguments
  • path - The key-path to the SystemTime field
  • reference - The reference time to compare against
§Example
let old = events
    .lock_lazy_query()
    .where_before_systemtime(Event::timestamp_r(), cutoff_time);
Source

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

Filter by SystemTime being between two times (inclusive).

§Arguments
  • path - The key-path to the SystemTime field
  • start - The start time
  • end - The end time
§Example
let range = events
    .lock_lazy_query()
    .where_between_systemtime(Event::timestamp_r(), start, end);
Source

pub fn order_by<F>(self, path: KeyPaths<T, F>) -> Vec<T>
where F: Ord + Clone + 'static, T: Clone,

Orders results by a field in ascending order (terminal).

Note: This method requires T: Clone as it creates owned sorted copies. This is a terminal operation that collects and sorts all matching items.

§Arguments
  • path - The key-path to the field to order by
§Example
let sorted = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .order_by(Product::name_r());
Source

pub fn order_by_desc<F>(self, path: KeyPaths<T, F>) -> Vec<T>
where F: Ord + Clone + 'static, T: Clone,

Orders results by a field in descending order (terminal).

Note: This method requires T: Clone as it creates owned sorted copies. This is a terminal operation that collects and sorts all matching items.

§Arguments
  • path - The key-path to the field to order by
§Example
let sorted = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .order_by_desc(Product::stock_r());
Source

pub fn order_by_float(self, path: KeyPaths<T, f64>) -> Vec<T>
where T: Clone,

Orders results by a float field in ascending order (terminal).

Note: This method requires T: Clone as it creates owned sorted copies. This is a terminal operation that collects and sorts all matching items.

§Arguments
  • path - The key-path to the f64 field to order by
§Example
let sorted = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .order_by_float(Product::price_r());
Source

pub fn order_by_float_desc(self, path: KeyPaths<T, f64>) -> Vec<T>
where T: Clone,

Orders results by a float field in descending order (terminal).

Note: This method requires T: Clone as it creates owned sorted copies. This is a terminal operation that collects and sorts all matching items.

§Arguments
  • path - The key-path to the f64 field to order by
§Example
let sorted = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .order_by_float_desc(Product::rating_r());
Source

pub fn group_by<F>(self, path: KeyPaths<T, F>) -> HashMap<F, Vec<T>>
where F: Eq + Hash + Clone + 'static, T: Clone,

Groups results by a field value (terminal).

Note: This method requires T: Clone as it creates owned copies in groups. This is a terminal operation that collects all matching items into groups.

§Arguments
  • path - The key-path to the field to group by
§Example
let by_category = products
    .lock_lazy_query()
    .where_(Product::stock_r(), |&s| s > 0)
    .group_by(Product::category_r());
 
for (category, products) in by_category {
    println!("{}: {} products", category, products.len());
}
 
// SQL equivalent: SELECT * FROM products WHERE stock > 0 GROUP BY category

Auto Trait Implementations§

§

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

§

impl<'a, T, L, I> RefUnwindSafe for LockLazyQuery<'a, T, L, I>

§

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

§

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

§

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

§

impl<'a, T, L, I> UnwindSafe for LockLazyQuery<'a, T, L, 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.