I have a struct with a field 'payload' of Option<T>
where T can be one of several primitive integer types. Like so:
pub struct Request<T: MyIntTrait> {
pub cmd: Cmd,
pub payload: Option<T>,
a bunch of other fields...
}
When instantiating this struct with None as its payload obviously type annotations are needed:
let req: Request<u8> = Request { cmd: Cmd::Get, payload: None, ...}
or like this:
let req = Request { cmd: Cmd::Get, payload: None::<u8>, ...}
This makes for a very unergonomic api that is counterintuitive to use. Has anybody got an idea how to get around this or will I just have to live with it?
Unless the T
can be inferred somewhere else, you have to specify it somehow. You could specify a default for the type argument, which may be sufficient in your case:
pub struct Request<T: MyIntTrait = u8>