#Lifetime help for function inputs.

5 messages · Page 1 of 1 (latest)

alpine hemlock
#

I'm trying to do something similar to this rustnomicon chapter except with a Bevy ReadOnlySystem.

/// trait for executing a system with some Input and Output
pub trait ReadHandler<Input=(), Output=()> {
    fn run(&self, world: &BevyEcs) -> Output;
}

/// Wrapper for storing Handler function pointer and arguments
pub struct ReadHandlerWrapper<System, Args>(pub System, pub Arguments);

// implementation using an HRTB to execute the system.
impl<System, Args, Input, Output> ReadHandler<Input, Output> for ReadHandlerWrapper<System, Args> 
where
    Args: 'static,
    Output: 'static,
    System: for<'i> ReadOnlySystem<In=InRef<'i, Args>, Out=Output>
    // ^^^^^^^ Error occurs here
{
    fn run(&self, world: &World) -> Output {
        todo!()
    }
}

The Error is:

error[E0582]: binding for associated type `In` references lifetime `'i`, which does not appear in the trait input types
   --> src/handler/mod.rs:106:31
    |
106 |     System: for<'i> ReadOnlySystem<In=InRef<'i, Arg>, Out=Output>
    |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^

I don't want to have to clone Args into the system every single time its' ran, because it may be a rather large struct. Any help is greatly appreciated.

eternal micaBOT
#

Error code: b77348bf29a54a0fbbf7d4623b0ef448

Internal error occurred

For support, please send the error code above in #bug-reports-and-errors on the support server (click to join) with a description of what you were doing at the time.

undone forge
#

the rustonomicon mentions that an hrtb is "an infinite list of trait bounds that F must satisfy". this works for the Fn trait, which looks roughly like this:rust // stripping a way a lot of noise pub trait Fn<Args> { type Output; fn call(&self, args: Args) -> Self::Output; } for<'i> Fn(&'i I) desugars to for<'i> Fn<(&'i I,)>. this works because the hrtb is applied to a type parameter: Fn<(&'lt1 I,)>, Fn<(&'lt2 I,)>, and Fn<(&'lt3 I,)> are treated as three separate trait impls. (three of the infinitely many)

for<'i> Func<Input=&'i I> doesn't work because the hrtb is applied to an associated type. F can only implement Func once, with one associated Input, because Func isn't generic. the infinite list of trait bounds here are saying that F's Input type must be &'lt1 I, but it also has to be &'lt2 I, and also &'lt3 I, and so on infinitely many times. that can't be true, because those are all different types and there can only be one Input.

#

(annoyingly, E0502 is the generic error code for borrowcheck errors and rustc --explain E0502 doesen't have anything useful related to this case :|)