menu icon indicating copy to clipboard operation
menu copied to clipboard

Accessing borrowed data in the `Context`

Open ryan-summers opened this issue 1 year ago • 0 comments

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.

ryan-summers avatar Apr 04 '24 08:04 ryan-summers