I'm working on a Rust library that provides a trait which may optionally be implemented by a user. It can look like this:
pub trait MyHandler {
fn do_something(&mut self);
}
The main struct in the library looks somewhat like this:
pub struct MyType<H: MyHandler {
field_a: u8,
field_b: u32,
handler: Option<H>,
}
This works, but in reality a very small percentage of the users will implement a custom handler. The problem from an ergonomics point of view is that the type H will always need to be specified, even when no handler is passed in:
let t = MyType {
field_a: 1,
field_b: 3000,
handler: None,
};
error[E0282]: type annotations needed for `MyType<H>`
--> src/main.rs:12:13
|
12 | let t = MyType {
| - ^^^^^^ cannot infer type for type parameter `H` declared on the struct `MyType`
| |
| consider giving `t` the explicit type `MyType<H>`, where the type parameter `H` is specified
All the library can do to simplify this, is to provide a dummy implementation that can be specified by the user:
pub struct DefaultHandler {}
impl MyHandler for DefaultHandler {
fn do_something(&mut self) {}
}
let t = MyType::<DefaultHandler> {
field_a: 1,
field_b: 3000,
handler: None,
};
Is there a good pattern to make this more ergonomic, without having to specify a dummy trait impl?