Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetime of chunks from vector::Focus::chunk_at #204

Open
dfoxfranke opened this issue Dec 28, 2022 · 0 comments
Open

Lifetime of chunks from vector::Focus::chunk_at #204

dfoxfranke opened this issue Dec 28, 2022 · 0 comments

Comments

@dfoxfranke
Copy link

vector::Focus::chunk_at has a signature of

impl <'a, A> Focus<'a, A> {
    fn chunk_at(&mut self, index: usize) -> &[A];
}

meaning that the lifetime of the returned slice is tied to the lifetime of the focus. I think the return type should actually be &'a [A],
i.e. the slice lives as long as the vector we're focusing on. This should be safe because:

  1. The Focus doesn't own any chunk storage; it's all managed by an RrbPool.
  2. The RrbPool won't drop a chunk so long as there exists a Vector which references it.
  3. 'a is the lifetime of the Vector which constructed the Focus.

I see that the implementation of Chunks has an unsafe hack which extends the lifetime of a slice returned from chunk_at by casting it through a raw pointer and back. It seems to me that this shouldn't be necessary and what's returned from chunk_at should already have the appropriate lifetime.

For the meantime I've put this into my code as a workaround:

trait FocusExt<'a, A> {
    fn chunk_at_ext(&mut self, index: usize) -> (Range<usize>, &'a [A]);
}

impl<'a, A> FocusExt<'a, A> for vector::Focus<'a, A>
where
    A: Clone,
{
    fn chunk_at_ext(&mut self, index: usize) -> (Range<usize>, &'a [A]) {
        let (range, chunk) = self.chunk_at(index);
        unsafe { (range, &*(chunk as *const [A])) }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant