Skip to content

Commit 10923f1

Browse files
committed
Also add lock_all_entries to trait
[wip] this part currently makes the compiler ICE
1 parent 503eecf commit 10923f1

File tree

4 files changed

+205
-200
lines changed

4 files changed

+205
-200
lines changed

src/lockable_hash_map.rs

Lines changed: 12 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,18 @@ where
331331
fn keys_with_entries_or_locked(&self) -> Vec<K> {
332332
self.map_impl.keys_with_entries_or_locked()
333333
}
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+
}
334346
}
335347

336348
impl<K, V> LockableHashMap<K, V>
@@ -355,106 +367,6 @@ where
355367
map_impl: LockableMapImpl::new(),
356368
}
357369
}
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-
}
458370
}
459371

460372
impl<K, V> Default for LockableHashMap<K, V>

src/lockable_lru_cache.rs

Lines changed: 12 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,18 @@ where
402402
fn keys_with_entries_or_locked(&self) -> Vec<K> {
403403
self.map_impl.keys_with_entries_or_locked()
404404
}
405+
406+
#[inline]
407+
async fn lock_all_entries(&self) -> impl Stream<Item = <Self as Lockable<K, V>>::Guard<'_>> {
408+
LockableMapImpl::lock_all_entries(&self.map_impl).await
409+
}
410+
411+
#[inline]
412+
async fn lock_all_entries_owned(
413+
self: &Arc<Self>,
414+
) -> impl Stream<Item = <Self as Lockable<K, V>>::OwnedGuard> {
415+
LockableMapImpl::lock_all_entries(Arc::clone(self)).await
416+
}
405417
}
406418

407419
impl<K, V, Time> LockableLruCache<K, V, Time>
@@ -429,106 +441,6 @@ where
429441
}
430442
}
431443

432-
/// Lock all entries of the cache once. The result of this is a [Stream] that will
433-
/// produce the corresponding lock guards. If items are locked, the [Stream] will
434-
/// produce them as they become unlocked and can be locked by the stream.
435-
///
436-
/// The returned stream is `async` and therefore may return items much later than
437-
/// when this function was called, but it only returns an entry if it existed
438-
/// or was locked at the time this function was called, and still exists when
439-
/// the stream is returning the entry.
440-
/// For any entry currently locked by another thread or task while this function
441-
/// is called, the following rules apply:
442-
/// - If that thread/task creates the entry => the stream will return it
443-
/// - If that thread/task removes the entry => the stream will not return it
444-
/// - If the entry was not pre-existing and that thread/task does not create it => the stream will not return it.
445-
///
446-
/// Examples
447-
/// -----
448-
/// ```
449-
/// use futures::stream::StreamExt;
450-
/// use lockable::{AsyncLimit, Lockable, LockableLruCache};
451-
///
452-
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
453-
/// let lockable_map = LockableLruCache::<i64, String>::new();
454-
///
455-
/// // Insert two entries
456-
/// lockable_map
457-
/// .async_lock(4, AsyncLimit::no_limit())
458-
/// .await?
459-
/// .insert(String::from("Value 4"));
460-
/// lockable_map
461-
/// .async_lock(5, AsyncLimit::no_limit())
462-
/// .await?
463-
/// .insert(String::from("Value 5"));
464-
///
465-
/// // Lock all entries and add them to an `entries` vector
466-
/// let mut entries: Vec<(i64, String)> = Vec::new();
467-
/// let mut stream = lockable_map.lock_all_entries().await;
468-
/// while let Some(guard) = stream.next().await {
469-
/// entries.push((*guard.key(), guard.value().unwrap().clone()));
470-
/// }
471-
///
472-
/// // `entries` now contains both entries, but in an arbitrary order
473-
/// assert_eq!(2, entries.len());
474-
/// assert!(entries.contains(&(4, String::from("Value 4"))));
475-
/// assert!(entries.contains(&(5, String::from("Value 5"))));
476-
/// # Ok::<(), lockable::Never>(())}).unwrap();
477-
/// ```
478-
pub async fn lock_all_entries(
479-
&self,
480-
) -> impl Stream<Item = <Self as Lockable<K, V>>::Guard<'_>> {
481-
LockableMapImpl::lock_all_entries(&self.map_impl).await
482-
}
483-
484-
/// Lock all entries of the cache once. The result of this is a [Stream] that will
485-
/// produce the corresponding lock guards. If items are locked, the [Stream] will
486-
/// produce them as they become unlocked and can be locked by the stream.
487-
///
488-
/// This is identical to [LockableLruCache::lock_all_entries], but it works on
489-
/// an `Arc<LockableLruCache>` instead of a [LockableLruCache] and returns a
490-
/// [Lockable::OwnedGuard] that binds its lifetime to the [LockableLruCache] in that
491-
/// [Arc]. Such a [Lockable::OwnedGuard] can be more easily moved around or cloned.
492-
///
493-
/// Examples
494-
/// -----
495-
/// ```
496-
/// use futures::stream::StreamExt;
497-
/// use lockable::{AsyncLimit, Lockable, LockableLruCache};
498-
/// use std::sync::Arc;
499-
///
500-
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
501-
/// let lockable_map = Arc::new(LockableLruCache::<i64, String>::new());
502-
///
503-
/// // Insert two entries
504-
/// lockable_map
505-
/// .async_lock(4, AsyncLimit::no_limit())
506-
/// .await?
507-
/// .insert(String::from("Value 4"));
508-
/// lockable_map
509-
/// .async_lock(5, AsyncLimit::no_limit())
510-
/// .await?
511-
/// .insert(String::from("Value 5"));
512-
///
513-
/// // Lock all entries and add them to an `entries` vector
514-
/// let mut entries: Vec<(i64, String)> = Vec::new();
515-
/// let mut stream = lockable_map.lock_all_entries_owned().await;
516-
/// while let Some(guard) = stream.next().await {
517-
/// entries.push((*guard.key(), guard.value().unwrap().clone()));
518-
/// }
519-
///
520-
/// // `entries` now contains both entries, but in an arbitrary order
521-
/// assert_eq!(2, entries.len());
522-
/// assert!(entries.contains(&(4, String::from("Value 4"))));
523-
/// assert!(entries.contains(&(5, String::from("Value 5"))));
524-
/// # Ok::<(), lockable::Never>(())}).unwrap();
525-
/// ```
526-
pub async fn lock_all_entries_owned(
527-
self: &Arc<Self>,
528-
) -> impl Stream<Item = <Self as Lockable<K, V>>::OwnedGuard> {
529-
LockableMapImpl::lock_all_entries(Arc::clone(self)).await
530-
}
531-
532444
/// Lock all entries that are currently unlocked and that were unlocked for at least
533445
/// the given `duration`. This follows the LRU nature of the cache.
534446
///

0 commit comments

Comments
 (0)