@@ -331,6 +331,18 @@ where
331
331
fn keys_with_entries_or_locked ( & self ) -> Vec < K > {
332
332
self . map_impl . keys_with_entries_or_locked ( )
333
333
}
334
+
335
+ #[ inline]
336
+ async fn lock_all_entries ( & self ) -> impl Stream < Item = <Self as Lockable < K , V > >:: Guard < ' _ > > {
337
+ LockableMapImpl :: lock_all_entries ( & self . map_impl ) . await
338
+ }
339
+
340
+ #[ inline]
341
+ async fn lock_all_entries_owned (
342
+ self : & Arc < Self > ,
343
+ ) -> impl Stream < Item = <Self as Lockable < K , V > >:: OwnedGuard > {
344
+ LockableMapImpl :: lock_all_entries ( Arc :: clone ( self ) ) . await
345
+ }
334
346
}
335
347
336
348
impl < K , V > LockableHashMap < K , V >
@@ -355,106 +367,6 @@ where
355
367
map_impl : LockableMapImpl :: new ( ) ,
356
368
}
357
369
}
358
-
359
- /// Lock all entries of the cache once. The result of this is a [Stream] that will
360
- /// produce the corresponding lock guards. If items are locked, the [Stream] will
361
- /// produce them as they become unlocked and can be locked by the stream.
362
- ///
363
- /// The returned stream is `async` and therefore may return items much later than
364
- /// when this function was called, but it only returns an entry if it existed
365
- /// or was locked at the time this function was called, and still exists when
366
- /// the stream is returning the entry.
367
- /// For any entry currently locked by another thread or task while this function
368
- /// is called, the following rules apply:
369
- /// - If that thread/task creates the entry => the stream will return it
370
- /// - If that thread/task removes the entry => the stream will not return it
371
- /// - If the entry was not pre-existing and that thread/task does not create it => the stream will not return it.
372
- ///
373
- /// Examples
374
- /// -----
375
- /// ```
376
- /// use futures::stream::StreamExt;
377
- /// use lockable::{AsyncLimit, Lockable, LockableHashMap};
378
- ///
379
- /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
380
- /// let lockable_map = LockableHashMap::<i64, String>::new();
381
- ///
382
- /// // Insert two entries
383
- /// lockable_map
384
- /// .async_lock(4, AsyncLimit::no_limit())
385
- /// .await?
386
- /// .insert(String::from("Value 4"));
387
- /// lockable_map
388
- /// .async_lock(5, AsyncLimit::no_limit())
389
- /// .await?
390
- /// .insert(String::from("Value 5"));
391
- ///
392
- /// // Lock all entries and add them to an `entries` vector
393
- /// let mut entries: Vec<(i64, String)> = Vec::new();
394
- /// let mut stream = lockable_map.lock_all_entries().await;
395
- /// while let Some(guard) = stream.next().await {
396
- /// entries.push((*guard.key(), guard.value().unwrap().clone()));
397
- /// }
398
- ///
399
- /// // `entries` now contains both entries, but in an arbitrary order
400
- /// assert_eq!(2, entries.len());
401
- /// assert!(entries.contains(&(4, String::from("Value 4"))));
402
- /// assert!(entries.contains(&(5, String::from("Value 5"))));
403
- /// # Ok::<(), lockable::Never>(())}).unwrap();
404
- /// ```
405
- pub async fn lock_all_entries (
406
- & self ,
407
- ) -> impl Stream < Item = <Self as Lockable < K , V > >:: Guard < ' _ > > {
408
- LockableMapImpl :: lock_all_entries ( & self . map_impl ) . await
409
- }
410
-
411
- /// Lock all entries of the cache once. The result of this is a [Stream] that will
412
- /// produce the corresponding lock guards. If items are locked, the [Stream] will
413
- /// produce them as they become unlocked and can be locked by the stream.
414
- ///
415
- /// This is identical to [LockableHashMap::lock_all_entries], but but it works on
416
- /// an `Arc<LockableHashMap>` instead of a [LockableHashMap] and returns a
417
- /// [Lockable::OwnedGuard] that binds its lifetime to the [LockableHashMap] in that
418
- /// [Arc]. Such a [Lockable::OwnedGuard] can be more easily moved around or cloned.
419
- ///
420
- /// Examples
421
- /// -----
422
- /// ```
423
- /// use futures::stream::StreamExt;
424
- /// use lockable::{AsyncLimit, Lockable, LockableHashMap};
425
- /// use std::sync::Arc;
426
- ///
427
- /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
428
- /// let lockable_map = Arc::new(LockableHashMap::<i64, String>::new());
429
- ///
430
- /// // Insert two entries
431
- /// lockable_map
432
- /// .async_lock(4, AsyncLimit::no_limit())
433
- /// .await?
434
- /// .insert(String::from("Value 4"));
435
- /// lockable_map
436
- /// .async_lock(5, AsyncLimit::no_limit())
437
- /// .await?
438
- /// .insert(String::from("Value 5"));
439
- ///
440
- /// // Lock all entries and add them to an `entries` vector
441
- /// let mut entries: Vec<(i64, String)> = Vec::new();
442
- /// let mut stream = lockable_map.lock_all_entries_owned().await;
443
- /// while let Some(guard) = stream.next().await {
444
- /// entries.push((*guard.key(), guard.value().unwrap().clone()));
445
- /// }
446
- ///
447
- /// // `entries` now contains both entries, but in an arbitrary order
448
- /// assert_eq!(2, entries.len());
449
- /// assert!(entries.contains(&(4, String::from("Value 4"))));
450
- /// assert!(entries.contains(&(5, String::from("Value 5"))));
451
- /// # Ok::<(), lockable::Never>(())}).unwrap();
452
- /// ```
453
- pub async fn lock_all_entries_owned (
454
- self : & Arc < Self > ,
455
- ) -> impl Stream < Item = <Self as Lockable < K , V > >:: OwnedGuard > {
456
- LockableMapImpl :: lock_all_entries ( Arc :: clone ( self ) ) . await
457
- }
458
370
}
459
371
460
372
impl < K , V > Default for LockableHashMap < K , V >
0 commit comments