Skip to content

Iterator without referencing stack-allocated variable - iter_owned? #68

@SuperFluffy

Description

@SuperFluffy

How do you feel about providing a "owned" iterator that takes ownership of the mutation guard rather than a reference? Here's a very quick draft I threw together: SuperFluffy#1 (using OwnedGuard, which is likely not necessary and could be relaxed to G: Guard); it requires a lot of near-duplicate code however.

For context, I am wrapping a papaya::HashMap and want to provide an iterator over it, but papaya is leaking through because of its pin mechanism; take this example:

struct BlobStorage {
    inner: papaya::HashMap<Uuid, Arc<Blob>>,
}

impl BlobStorage {
    fn iter(&self) -> impl Iterator<Item = (&Uuid, &Arc<Blob>)> {
        let pinned = self.pin();
        pinned.iter()
    }
}

/* error
error[E0515]: cannot return value referencing local variable `pinned`
  --> the_example/src/example.rs
   |
77 |         pinned.iter()
   |         ------^^^^^^^
   |         |
   |         returns a value referencing data owned by the current function
   |         `pinned` is borrowed here
   |
   = help: use `.collect()` to allocate the iterator
*./

Example API from the link above:

impl<K, V, S> HashMap<K, V, S>
where
    K: Hash + Eq,
    S: BuildHasher,
{
    #[inline]
    pub fn iter_owned<'g>(&'g self, guard: OwnedGuard<'g>) -> IterOwned<'g, K, V> {
        IterOwned {
            raw: self.raw.iter_owned(self.raw.verify_owned(guard)),
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions