Skip to content

Commit

Permalink
Merge pull request #43 from devnev/prefix-lifetime
Browse files Browse the repository at this point in the history
Fix lifetimes of prefix-query methods
  • Loading branch information
sile authored Jan 13, 2025
2 parents 4220fb9 + 184718d commit 70908d7
Showing 1 changed file with 47 additions and 11 deletions.
58 changes: 47 additions & 11 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ impl<K: Bytes, V> GenericPatriciaMap<K, V> {
key: &'b Q,
) -> CommonPrefixesIter<'a, 'b, K::Borrowed, V>
where
'a: 'b,
Q: ?Sized + AsRef<K::Borrowed>,
{
CommonPrefixesIter {
Expand All @@ -333,10 +332,13 @@ impl<K: Bytes, V> GenericPatriciaMap<K, V> {
/// .flatten()
/// .eq(vec![&"a", &"b", &"c", &"d"].into_iter()));
/// ```
pub fn common_prefix_values<'a, 'b, Q>(&'a self, key: &'b Q) -> impl 'a + Iterator<Item = &'a V>
pub fn common_prefix_values<'a, 'b, Q>(
&'a self,
key: &'b Q,
) -> impl Iterator<Item = &'a V> + use<'a, 'b, Q, K, V>
where
'b: 'a,
Q: ?Sized + AsRef<K::Borrowed>,
<K as Bytes>::Borrowed: 'b,
{
self.tree
.common_prefixes(key.as_ref())
Expand Down Expand Up @@ -479,10 +481,7 @@ impl<K: Bytes, V> GenericPatriciaMap<K, V> {
pub fn iter_prefix<'a, 'b>(
&'a self,
prefix: &'b K::Borrowed,
) -> impl 'a + Iterator<Item = (K, &'a V)>
where
'b: 'a,
{
) -> impl Iterator<Item = (K, &'a V)> + use<'a, 'b, K, V> {
self.tree
.iter_prefix(prefix)
.into_iter()
Expand All @@ -506,10 +505,7 @@ impl<K: Bytes, V> GenericPatriciaMap<K, V> {
pub fn iter_prefix_mut<'a, 'b>(
&'a mut self,
prefix: &'b K::Borrowed,
) -> impl 'a + Iterator<Item = (K, &'a mut V)>
where
'b: 'a,
{
) -> impl Iterator<Item = (K, &'a mut V)> + use<'a, 'b, K, V> {
self.tree
.iter_prefix_mut(prefix)
.into_iter()
Expand Down Expand Up @@ -987,4 +983,44 @@ mod tests {
assert_eq!(map.get("インターポール"), Some(&1));
assert_eq!(map.get("インターポル"), Some(&2));
}

#[test]
fn issue42_iter_prefix() {
let mut map = StringPatriciaMap::new();
map.insert("a0/b0", 0);
map.insert("a1/b1", 0);
let items: Vec<_> = {
let prefix = "a0".to_owned();
map.iter_prefix(&prefix).collect()
};

assert_eq!(items, vec![("a0/b0".to_owned(), &0)])
}

#[test]
#[ignore = "bug causing assertion failure"]
fn issue42_iter_prefix_mut() {
let mut map = StringPatriciaMap::new();
map.insert("a0/b0", 0);
map.insert("a1/b1", 0);
let items: Vec<_> = {
let prefix = "a0".to_owned();
map.iter_prefix_mut(&prefix).collect()
};

assert_eq!(items, vec![("a0/b0".to_owned(), &mut 0)])
}

#[test]
fn issue42_common_prefix_values() {
let mut map = StringPatriciaMap::new();
map.insert("a0/b0", 0);
map.insert("a1/b1", 0);
let items: Vec<_> = {
let prefix = "a0/b0/c0".to_owned();
map.common_prefix_values(&prefix).collect()
};

assert_eq!(items, vec![&0])
}
}

0 comments on commit 70908d7

Please sign in to comment.