Skip to content

Instantly share code, notes, and snippets.

@oksuz
Created February 22, 2026 21:56
Show Gist options
  • Select an option

  • Save oksuz/46e84ffe9cf551e591a816f9e559cdb3 to your computer and use it in GitHub Desktop.

Select an option

Save oksuz/46e84ffe9cf551e591a816f9e559cdb3 to your computer and use it in GitHub Desktop.
/// compile time monomorphization
/// no runtime overhead
/// no reflection
/// no dynamic dispatch
trait Handler<T> {
fn call(self);
}
impl<F> Handler<()> for F
where
F: FnOnce(),
{
fn call(self) {
self()
}
}
impl<F> Handler<(String,)> for F
where
F: FnOnce(String),
{
fn call(self) {
self("val".to_string());
}
}
fn caller<H, T>(c: H)
where
H: Handler<T>,
{
c.call();
}
fn test_1() {
println!("test_1 called");
}
fn test_2(val: String) {
println!("test_2: {}", val);
}
fn main() {
caller(test_1);
caller(test_2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment