Created
February 22, 2026 21:56
-
-
Save oksuz/46e84ffe9cf551e591a816f9e559cdb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// 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