Can I add function params by generic parameter?

⚓ Rust    📅 2026-02-27    👤 surdeus    👁️ 1      

surdeus

I have a type like this one below. It’s a version of the event/observer design pattern. You can register listener callbacks and fire the event/signal to deliver info to all listeners.

Now I want some signals that take an extra “context” parameter.

type InternalsPtr<D, P> = Arc<Mutex<SignalInternals<D, P>>>;

#[derive(Clone)]
pub struct Signal<D, TTM>
    where TTM: ThreadTransferMechanism, D: 'static
{
    inner: InternalsPtr<D, TTM>,
}
struct SignalInternals<D, TTM> {
    listeners: Vec<Box<dyn Fn(&D) + Send + Sync + 'static>>,
    delivery_scheduled: bool,
    firing: Vec<D>,
    thread_transfer_mechanism: TTM
}

So, I want a variation where the listeners take another argument:

listeners: Vec<Box<dyn Fn(&D, &mut Context) + Send + Sync + 'static>>,

Am I correct that Rust has no way to do that all under the same type? I guess that would be “variadic generics”?

4 posts - 4 participants

Read full topic

🏷️ Rust_feed