rust-embedded-community / menu

Command-line menu system for embedded Rust platforms.
Apache License 2.0
51 stars 11 forks source link

Accessing borrowed data in the `Context` #17

Closed ryan-summers closed 7 months ago

ryan-summers commented 8 months ago

Problem

Currently, it doesn't seem possible to access non-static borrowed data in the menu context if the concrete type of the Context is also required.

Take the following example:

struct Context<'non_static> {
   my_borrowed_data: &'non_static u32,
}

#[rtic::app]
{
    struct LocalResources {
        // What should the lifetime of `Context` be here when it must be specified concretely?
        menu: menu::Runner<'static, Context<'?>>
    } 
}

The 'non_static lifetime is run-time dependent (i.e. 'a). However, if this Context type must be specified concrete (i.e. when specifying an RTIC resource), it's not possible to write the concrete type of Context without a generic lifetime. This makes it effectively impossible for Context to borrow non-static objects if the concrete type of Context must be used.

Proposal

To work around this, we would need to erase the concrete type of Context from the library types. What I believe this would be is an API as follows:

struct Runner<I: core::fmt::Write, C> {
   interface: I,
   _context_marker: PhantomData<C>,
}

impl<I: core::fmt::Write, C> Runner<I, C> {

    pub fn input_byte(&mut self, input: u8, context: &mut C) {...}
}

This would allow the Context type to be concrete, but expose an anonymous lifetime via the borrow of context in input_byte.