Skip to content

can we nest states using only references? #26

@wtholliday

Description

@wtholliday

I was looking into using references instead of passing around a context, and it seems tricky. Just wanted to record somewhere how far I got.

This is close, but doesn't quite work:

    struct State<F, V> {
        f: F,
        state: String,
        phantom: std::marker::PhantomData<V>,
    }

    impl<'a, V: View + 'a, F: Fn(&'a String) -> V + 'a> View for State<F, V> {
        fn draw(&self) {
            (self.f)(&self.state).draw();
        }
    }

The problem is in F: Fn(&'a String) -> V + 'a. This says the entire closure cannot outlive the String passed in. That's not quite what we want. It's ok if the closure lives longer. What we want is for the return type (V) to live as long as the String passed in.

What we want is something like F: for<'a> Fn(&'a String) -> V<'a> where V is a higher-kinded-type, but rust doesn't (yet) have this feature.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions