matchit icon indicating copy to clipboard operation
matchit copied to clipboard

API for getting immutable / mutable references to all values inside a node

Open 90-008 opened this issue 4 years ago • 9 comments

My use-case is that I'm writing a tower::Service and I have a Node<Handler> where Handler implements tower::Service. What I want to do is implement poll_ready for this tower::Service, but I can't do it due to not being able to get mutable references to all values inside the node.

90-008 avatar Oct 26 '21 04:10 90-008

Would:

fn routes(&self) -> impl Iterator<Item = (&str, &T)>;
fn routes_mut(&mut self) -> impl Iterator<Item = (&str, &mut T)>;

Work for your use case?

ibraheemdev avatar Oct 26 '21 14:10 ibraheemdev

Yes, that would be very nice.

90-008 avatar Oct 26 '21 15:10 90-008

Actually, I would be fine with only getting an iterator over the values. I looked into this a bit and wrote something that would do the job, which was just values and values_mut methods.

EDIT: I have a branch here if you want to check it out.

90-008 avatar Oct 29 '21 14:10 90-008

I would like to provide the routes as well. I think the best way to do this would be to store a list of full paths and indices at the root of the tree. Node prefixes could then just be an index and a range, and providing an iterator would just be self.full.iter()

ibraheemdev avatar Oct 29 '21 15:10 ibraheemdev

I would like to provide the routes as well. I think the best way to do this would be to store a list of full paths and indices at the root of the tree. Node prefixes could then just be an index and a range, and providing an iterator would just be self.full.iter()

How would this be implemented (maybe a general overview?) I'm not very familiar with radix trees. Would like to PR this!

90-008 avatar Nov 02 '21 06:11 90-008

I'm thinking something like:

pub struct Root<T> {
   node: Node<T>,
   // a list of prefixes, and nested indices
   paths: Vec<(Vec<u8>, Vec<u8>)>
}

impl Root<T> {
    // this would really be a concrete iterator type
    fn routes(&self) -> impl Iterator<Item = (&str, &T)> {
        self.paths.iter().map(|(path, indices)| {
            unsafe {
                &*indices.fold(&self.node, |node, i| &node.children[i]).value.get().unwrap()
            }
        })
    }
    
    // rest of node methods here...
}

When you insert a route, you add the route path, and the path (indices) to the value in the tree. It would need to be updated when the tree is shifted around. As an optimization, Node { prefix: Vec<u8>, .. } could be changed to a (usize, Range<usize>), pointing to a element and prefix in paths, but that isn't necessary at first.

This way iteration is very cheap; we don't have to allocate a stack for traversal.

ibraheemdev avatar Nov 02 '21 23:11 ibraheemdev

I have implemented this on: https://github.com/yusdacra/matchit/tree/feat/values

My implementation is pretty different as it does not store indices, instead it takes advantage from the fact that the values don't get moved / removed. I have added tests to verify that it works properly. It however does add new unsafe, so it might not be wanted.

90-008 avatar Nov 04 '21 10:11 90-008

Your code aliases a Box, which is UB. You could do this with NonNull, but I would prefer the index-based solution.

ibraheemdev avatar Nov 04 '21 21:11 ibraheemdev

Your code aliases a Box, which is UB. You could do this with NonNull, but I would prefer the index-based solution.

Hmm, yeah I checked around and it seems it's UB... I will try the index-based solution again then. I first tried the index-based solution but couldn't figure out how to properly get the indices...

90-008 avatar Nov 05 '21 03:11 90-008