How to return an async function in a generic struct

⚓ Rust    📅 2025-09-09    👤 surdeus    👁️ 1      

surdeus

I have the following struct:

struct ExecReturnType<T, F, C>
where
    T: 'static,
    F: Future<Output = T> + 'static,
    C: FnMut() -> F,
{
    state: Signal<CommandState>,
    fun:   C,
}

And this is how I initialize and return it:

pub fn use_exec_with_duration<T, F>(
    mut future: impl FnMut() -> F + 'static,
) -> ExecReturnType<T, impl Future<Output = T>, impl FnMut() -> impl Future<Output = T>>
where
    T: 'static,
    F: Future<Output = T> + 'static,
{
    let mut state = use_signal(CommandState::default);
    // ...
    ExecReturnType {
        state,
        fun: move || {
            let res = future();
            *state.write() = CommandState::Submitting;
            timeout.action(());
            async move {
                let res = res.await;
                *state.write() = CommandState::None;
                res
            }
        },
    }
}

But the compiler tells me that different impl Trait results in different obaque types. I tried different approaches, but none seems to work, how should I solve this?

3 posts - 2 participants

Read full topic

🏷️ Rust_feed