简体   繁体   中英

How do I specify type parameters as being function arguments in Rust?

I'm trying to make a list that will hold Box<dyn Fn(&E)> where E is specified as part of the type. This works until E contains a reference, at which point it starts asking for lifetimes that aren't relevant.

A simpler example:

pub struct CallbackTest<E> {
    pub cb: Box<dyn Fn(&E)>,
}

impl<E> CallbackTest<E> {
    pub fn new<F>(cb: F) -> Self
    where
        F: Fn(&E)
    {
        Self { cb: Box::new(cb) }
    }
}

pub struct GameData { /* ... */ }

pub type TestRef = CallbackTest<(u32, &GameData)>;

This gives me a missing lifetime specifier error. I could put a lifetime parameter on TestRef to make it work, but that's not the correct lifetime. I don't want the &GameData to have to live for the entire lifetime of the CallbackTest , just during the function call.

EDIT: The &GameData is intentional. It's not a mistake. I hope my changes have made the goal behind this more obvious.

Any advice?

Here we are rust.playground

use std::marker::PhantomData;

pub struct CallbackTest<'a, E, Fa: 'a + Fn(&E)> {
    pub cb: Box<Fa>,
    _e: &'a PhantomData<E>,
}

impl<'a, E, Fa: 'a + Fn(&E)> CallbackTest<'a, E, Fa> {
    pub fn new(cb: Fa) -> Self
    {
        Self { 
            cb: Box::new(cb), 
            _e: &PhantomData
        }
    }
}

pub struct GameData { 
    pub field: i32,
}

pub type TestRef<'a, 'b, Fb> = CallbackTest<'b, (u32, &'a GameData, ), Fb>;

fn main() {
    let game_data = GameData{ field: 42};
    let test_ref: TestRef<_> = CallbackTest::new(|(val, gd): &(u32, &GameData)| { println!("val:{}, field:{}", val, (*gd).field)});
    (test_ref.cb)(&(24, &game_data));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM