rust-queries-core 1.0.8

Core functionality for rust-queries-builder - type-safe query builder for Rust collections
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Extended lock support for parking_lot and tokio.
//!
//! This module provides wrappers and extension traits for third-party lock types,
//! enabling them to work with the lock-aware query system.
//!
//! ## Features
//!
//! - **parking_lot Support**: High-performance RwLock and Mutex wrappers
//! - **tokio Support**: Async RwLock support for async applications
//! - **Extension Traits**: Direct `.lock_query()` and `.lock_join()` support
//!
//! ## Example (parking_lot)
//!
//! ```ignore
//! use rust_queries_core::lock_ext::{ParkingLotRwLockWrapper, ParkingLotQueryExt};
//! use std::collections::HashMap;
//! use parking_lot::RwLock;
//!
//! let mut products: HashMap<String, ParkingLotRwLockWrapper<Product>> = HashMap::new();
//! products.insert("p1".to_string(), ParkingLotRwLockWrapper::new(Product {
//!     id: 1,
//!     price: 999.99,
//! }));
//!
//! // Direct method call!
//! let expensive = products
//!     .lock_query()
//!     .where_(Product::price(), |&p| p > 500.0)
//!     .all();
//! ```
//!
//! ## Example (tokio)
//!
//! ```ignore
//! use rust_queries_core::lock_ext::{TokioRwLockWrapper, TokioLockQueryExt};
//! use std::collections::HashMap;
//!
//! async fn query_products(products: &HashMap<String, TokioRwLockWrapper<Product>>) {
//!     let expensive = products
//!         .lock_query()  // Direct method call!
//!         .where_(Product::price(), |&p| p > 500.0)
//!         .all();
//! }
//! ```

#[cfg(any(feature = "parking_lot", feature = "tokio"))]
use crate::locks::LockValue;

#[cfg(any(feature = "parking_lot", feature = "tokio"))]
use crate::lock_query::LockQuery;

#[cfg(any(feature = "parking_lot", feature = "tokio"))]
use crate::lock_lazy::LockLazyQuery;

#[cfg(any(feature = "parking_lot", feature = "tokio"))]
use crate::lock_join::LockJoinQuery;

#[cfg(any(feature = "parking_lot", feature = "tokio"))]
use std::collections::HashMap;

#[cfg(any(feature = "parking_lot", feature = "tokio"))]
use std::sync::Arc;

// ============================================================================
// parking_lot Support
// ============================================================================

/// Wrapper around Arc<parking_lot::RwLock<T>>.
///
/// This newtype is needed because of Rust's orphan rules - we can't implement
/// foreign traits (LockValue) on foreign types.
///
/// # Example
///
/// ```ignore
/// use rust_queries_core::lock_ext::ParkingLotRwLockWrapper;
/// use parking_lot::RwLock;
///
/// let wrapper = ParkingLotRwLockWrapper::new(Product {
///     id: 1,
///     name: "Laptop".to_string(),
///     price: 999.99,
/// });
/// ```
#[cfg(feature = "parking_lot")]
#[derive(Clone, Debug)]
pub struct ParkingLotRwLockWrapper<T>(Arc<parking_lot::RwLock<T>>);

#[cfg(feature = "parking_lot")]
impl<T> ParkingLotRwLockWrapper<T> {
    /// Create a new ParkingLotRwLockWrapper.
    pub fn new(value: T) -> Self {
        Self(Arc::new(parking_lot::RwLock::new(value)))
    }

    /// Get a reference to the inner Arc<RwLock<T>>.
    pub fn inner(&self) -> &Arc<parking_lot::RwLock<T>> {
        &self.0
    }
}

#[cfg(feature = "parking_lot")]
impl<T> LockValue<T> for ParkingLotRwLockWrapper<T> {
    fn with_value<F, R>(&self, f: F) -> Option<R>
    where
        F: FnOnce(&T) -> R,
    {
        // parking_lot RwLock is synchronous and doesn't panic on poisoning
        let guard = self.0.read();
        Some(f(&*guard))
    }
}

/// Wrapper around Arc<parking_lot::Mutex<T>>.
#[cfg(feature = "parking_lot")]
#[derive(Clone, Debug)]
pub struct ParkingLotMutexWrapper<T>(Arc<parking_lot::Mutex<T>>);

#[cfg(feature = "parking_lot")]
impl<T> ParkingLotMutexWrapper<T> {
    /// Create a new ParkingLotMutexWrapper.
    pub fn new(value: T) -> Self {
        Self(Arc::new(parking_lot::Mutex::new(value)))
    }

    /// Get a reference to the inner Arc<Mutex<T>>.
    pub fn inner(&self) -> &Arc<parking_lot::Mutex<T>> {
        &self.0
    }
}

#[cfg(feature = "parking_lot")]
impl<T> LockValue<T> for ParkingLotMutexWrapper<T> {
    fn with_value<F, R>(&self, f: F) -> Option<R>
    where
        F: FnOnce(&T) -> R,
    {
        // parking_lot Mutex is synchronous and doesn't panic on poisoning
        let guard = self.0.lock();
        Some(f(&*guard))
    }
}

// Extension traits for parking_lot

/// Extension trait to enable direct .lock_query() and .lock_lazy_query() calls
/// on HashMap with parking_lot RwLock.
#[cfg(feature = "parking_lot")]
pub trait ParkingLotQueryExt<V> {
    /// Create a LockQuery for SQL-like operations.
    fn lock_query(&self) -> LockQuery<'_, V, ParkingLotRwLockWrapper<V>>;
    
    /// Create a lazy lock query.
    fn lock_lazy_query(&self) -> LockLazyQuery<'_, V, ParkingLotRwLockWrapper<V>, impl Iterator<Item = &ParkingLotRwLockWrapper<V>>>;
}

#[cfg(feature = "parking_lot")]
impl<K, V: 'static> ParkingLotQueryExt<V> for HashMap<K, ParkingLotRwLockWrapper<V>>
where
    K: std::hash::Hash + Eq,
{
    fn lock_query(&self) -> LockQuery<'_, V, ParkingLotRwLockWrapper<V>> {
        let locks: Vec<_> = self.values().collect();
        LockQuery::from_locks(locks)
    }
    
    fn lock_lazy_query(&self) -> LockLazyQuery<'_, V, ParkingLotRwLockWrapper<V>, impl Iterator<Item = &ParkingLotRwLockWrapper<V>>> {
        LockLazyQuery::new(self.values())
    }
}

/// Extension trait for Mutex queries.
#[cfg(feature = "parking_lot")]
pub trait ParkingLotMutexQueryExt<V> {
    /// Create a LockQuery for SQL-like operations.
    fn lock_query(&self) -> LockQuery<'_, V, ParkingLotMutexWrapper<V>>;
    
    /// Create a lazy lock query.
    fn lock_lazy_query(&self) -> LockLazyQuery<'_, V, ParkingLotMutexWrapper<V>, impl Iterator<Item = &ParkingLotMutexWrapper<V>>>;
}

#[cfg(feature = "parking_lot")]
impl<K, V: 'static> ParkingLotMutexQueryExt<V> for HashMap<K, ParkingLotMutexWrapper<V>>
where
    K: std::hash::Hash + Eq,
{
    fn lock_query(&self) -> LockQuery<'_, V, ParkingLotMutexWrapper<V>> {
        let locks: Vec<_> = self.values().collect();
        LockQuery::from_locks(locks)
    }
    
    fn lock_lazy_query(&self) -> LockLazyQuery<'_, V, ParkingLotMutexWrapper<V>, impl Iterator<Item = &ParkingLotMutexWrapper<V>>> {
        LockLazyQuery::new(self.values())
    }
}

// Extension trait for JOIN operations

/// Extension trait for JOIN operations with parking_lot RwLock.
#[cfg(feature = "parking_lot")]
pub trait ParkingLotJoinExt<V> {
    /// Create a join query with another collection.
    fn lock_join<'a, R>(&'a self, right: &'a HashMap<impl std::hash::Hash + Eq, ParkingLotRwLockWrapper<R>>) 
        -> LockJoinQuery<'a, V, R, ParkingLotRwLockWrapper<V>, ParkingLotRwLockWrapper<R>>
    where
        R: 'static;
}

#[cfg(feature = "parking_lot")]
impl<K, V: 'static> ParkingLotJoinExt<V> for HashMap<K, ParkingLotRwLockWrapper<V>>
where
    K: std::hash::Hash + Eq,
{
    fn lock_join<'a, R>(&'a self, right: &'a HashMap<impl std::hash::Hash + Eq, ParkingLotRwLockWrapper<R>>) 
        -> LockJoinQuery<'a, V, R, ParkingLotRwLockWrapper<V>, ParkingLotRwLockWrapper<R>>
    where
        R: 'static,
    {
        let left_locks: Vec<_> = self.values().collect();
        let right_locks: Vec<_> = right.values().collect();
        LockJoinQuery::new(left_locks, right_locks)
    }
}

/// Extension trait for JOIN operations with parking_lot Mutex.
#[cfg(feature = "parking_lot")]
pub trait ParkingLotMutexJoinExt<V> {
    /// Create a join query with another collection.
    fn lock_join<'a, R>(&'a self, right: &'a HashMap<impl std::hash::Hash + Eq, ParkingLotMutexWrapper<R>>) 
        -> LockJoinQuery<'a, V, R, ParkingLotMutexWrapper<V>, ParkingLotMutexWrapper<R>>
    where
        R: 'static;
}

#[cfg(feature = "parking_lot")]
impl<K, V: 'static> ParkingLotMutexJoinExt<V> for HashMap<K, ParkingLotMutexWrapper<V>>
where
    K: std::hash::Hash + Eq,
{
    fn lock_join<'a, R>(&'a self, right: &'a HashMap<impl std::hash::Hash + Eq, ParkingLotMutexWrapper<R>>) 
        -> LockJoinQuery<'a, V, R, ParkingLotMutexWrapper<V>, ParkingLotMutexWrapper<R>>
    where
        R: 'static,
    {
        let left_locks: Vec<_> = self.values().collect();
        let right_locks: Vec<_> = right.values().collect();
        LockJoinQuery::new(left_locks, right_locks)
    }
}

// ============================================================================
// tokio Support
// ============================================================================

/// Wrapper around Arc<tokio::sync::RwLock<T>>.
///
/// This newtype enables tokio RwLock to work with the lock-aware query system.
///
/// # Example
///
/// ```ignore
/// use rust_queries_core::lock_ext::TokioRwLockWrapper;
///
/// let wrapper = TokioRwLockWrapper::new(Product {
///     id: 1,
///     name: "Laptop".to_string(),
///     price: 999.99,
/// });
/// ```
#[cfg(feature = "tokio")]
#[derive(Clone, Debug)]
pub struct TokioRwLockWrapper<T>(Arc<tokio::sync::RwLock<T>>);

#[cfg(feature = "tokio")]
impl<T> TokioRwLockWrapper<T> {
    /// Create a new TokioRwLockWrapper.
    pub fn new(value: T) -> Self {
        Self(Arc::new(tokio::sync::RwLock::new(value)))
    }

    /// Get a reference to the inner Arc<RwLock<T>>.
    pub fn inner(&self) -> &Arc<tokio::sync::RwLock<T>> {
        &self.0
    }
}

#[cfg(feature = "tokio")]
impl<T> LockValue<T> for TokioRwLockWrapper<T> {
    fn with_value<F, R>(&self, f: F) -> Option<R>
    where
        F: FnOnce(&T) -> R,
    {
        // Note: This blocks! For fully async code, consider using async queries.
        // We use blocking here because LockValue trait is synchronous.
        let guard = self.0.blocking_read();
        Some(f(&*guard))
    }
}

/// Wrapper around Arc<tokio::sync::Mutex<T>>.
#[cfg(feature = "tokio")]
#[derive(Clone, Debug)]
pub struct TokioMutexWrapper<T>(Arc<tokio::sync::Mutex<T>>);

#[cfg(feature = "tokio")]
impl<T> TokioMutexWrapper<T> {
    /// Create a new TokioMutexWrapper.
    pub fn new(value: T) -> Self {
        Self(Arc::new(tokio::sync::Mutex::new(value)))
    }

    /// Get a reference to the inner Arc<Mutex<T>>.
    pub fn inner(&self) -> &Arc<tokio::sync::Mutex<T>> {
        &self.0
    }
}

#[cfg(feature = "tokio")]
impl<T> LockValue<T> for TokioMutexWrapper<T> {
    fn with_value<F, R>(&self, f: F) -> Option<R>
    where
        F: FnOnce(&T) -> R,
    {
        // Note: This blocks! For fully async code, consider using async queries.
        let guard = self.0.blocking_lock();
        Some(f(&*guard))
    }
}

// Extension traits for tokio

/// Extension trait to enable direct .lock_query() and .lock_lazy_query() calls
/// on HashMap with tokio RwLock.
#[cfg(feature = "tokio")]
pub trait TokioLockQueryExt<V> {
    /// Create a LockQuery for SQL-like operations.
    fn lock_query(&self) -> LockQuery<'_, V, TokioRwLockWrapper<V>>;
    
    /// Create a lazy lock query.
    fn lock_lazy_query(&self) -> LockLazyQuery<'_, V, TokioRwLockWrapper<V>, impl Iterator<Item = &TokioRwLockWrapper<V>>>;
}

#[cfg(feature = "tokio")]
impl<K, V: 'static> TokioLockQueryExt<V> for HashMap<K, TokioRwLockWrapper<V>>
where
    K: std::hash::Hash + Eq,
{
    fn lock_query(&self) -> LockQuery<'_, V, TokioRwLockWrapper<V>> {
        let locks: Vec<_> = self.values().collect();
        LockQuery::from_locks(locks)
    }
    
    fn lock_lazy_query(&self) -> LockLazyQuery<'_, V, TokioRwLockWrapper<V>, impl Iterator<Item = &TokioRwLockWrapper<V>>> {
        LockLazyQuery::new(self.values())
    }
}

/// Extension trait for Mutex queries.
#[cfg(feature = "tokio")]
pub trait TokioMutexQueryExt<V> {
    /// Create a LockQuery for SQL-like operations.
    fn lock_query(&self) -> LockQuery<'_, V, TokioMutexWrapper<V>>;
    
    /// Create a lazy lock query.
    fn lock_lazy_query(&self) -> LockLazyQuery<'_, V, TokioMutexWrapper<V>, impl Iterator<Item = &TokioMutexWrapper<V>>>;
}

#[cfg(feature = "tokio")]
impl<K, V: 'static> TokioMutexQueryExt<V> for HashMap<K, TokioMutexWrapper<V>>
where
    K: std::hash::Hash + Eq,
{
    fn lock_query(&self) -> LockQuery<'_, V, TokioMutexWrapper<V>> {
        let locks: Vec<_> = self.values().collect();
        LockQuery::from_locks(locks)
    }
    
    fn lock_lazy_query(&self) -> LockLazyQuery<'_, V, TokioMutexWrapper<V>, impl Iterator<Item = &TokioMutexWrapper<V>>> {
        LockLazyQuery::new(self.values())
    }
}

// Extension trait for JOIN operations

/// Extension trait for JOIN operations with tokio RwLock.
#[cfg(feature = "tokio")]
pub trait TokioLockJoinExt<V> {
    /// Create a join query with another collection.
    fn lock_join<'a, R>(&'a self, right: &'a HashMap<impl std::hash::Hash + Eq, TokioRwLockWrapper<R>>) 
        -> LockJoinQuery<'a, V, R, TokioRwLockWrapper<V>, TokioRwLockWrapper<R>>
    where
        R: 'static;
}

#[cfg(feature = "tokio")]
impl<K, V: 'static> TokioLockJoinExt<V> for HashMap<K, TokioRwLockWrapper<V>>
where
    K: std::hash::Hash + Eq,
{
    fn lock_join<'a, R>(&'a self, right: &'a HashMap<impl std::hash::Hash + Eq, TokioRwLockWrapper<R>>) 
        -> LockJoinQuery<'a, V, R, TokioRwLockWrapper<V>, TokioRwLockWrapper<R>>
    where
        R: 'static,
    {
        let left_locks: Vec<_> = self.values().collect();
        let right_locks: Vec<_> = right.values().collect();
        LockJoinQuery::new(left_locks, right_locks)
    }
}

/// Extension trait for JOIN operations with tokio Mutex.
#[cfg(feature = "tokio")]
pub trait TokioMutexJoinExt<V> {
    /// Create a join query with another collection.
    fn lock_join<'a, R>(&'a self, right: &'a HashMap<impl std::hash::Hash + Eq, TokioMutexWrapper<R>>) 
        -> LockJoinQuery<'a, V, R, TokioMutexWrapper<V>, TokioMutexWrapper<R>>
    where
        R: 'static;
}

#[cfg(feature = "tokio")]
impl<K, V: 'static> TokioMutexJoinExt<V> for HashMap<K, TokioMutexWrapper<V>>
where
    K: std::hash::Hash + Eq,
{
    fn lock_join<'a, R>(&'a self, right: &'a HashMap<impl std::hash::Hash + Eq, TokioMutexWrapper<R>>) 
        -> LockJoinQuery<'a, V, R, TokioMutexWrapper<V>, TokioMutexWrapper<R>>
    where
        R: 'static,
    {
        let left_locks: Vec<_> = self.values().collect();
        let right_locks: Vec<_> = right.values().collect();
        LockJoinQuery::new(left_locks, right_locks)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[cfg(feature = "parking_lot")]
    #[test]
    fn test_parking_lot_wrapper() {
        let wrapper = ParkingLotRwLockWrapper::new(42);
        let result = wrapper.with_value(|v| *v * 2);
        assert_eq!(result, Some(84));
    }
    
    #[cfg(feature = "tokio")]
    #[test]
    fn test_tokio_wrapper() {
        let wrapper = TokioRwLockWrapper::new(42);
        let result = wrapper.with_value(|v| *v * 2);
        assert_eq!(result, Some(84));
    }
}