I'm currently writing a TFTP implementation to learn Rust.
Every operation involves an opcode which I defined as:
#[repr(u16)]
enum TftpOpcode {
ReadRequest = 1,
WriteRequest = 2,
Data = 3,
Acknowledgement = 4,
Error = 5
}
At some point I get raw data over a UdpSocket
, which I convert to u16 via the byteorder
crate and I'd like to be able to create a TftpOpcode
from that. For this reason I thought implementing TryFrom might be a good idea and copied the declaration from the docs:
impl TryFrom<u16> for TftpOpcode {
type Error = &'static str;
fn try_from(value: u16) -> Result<Self, Self::Error> {
match value {
1 => Ok(TftpOpcode::ReadRequest),
2 => Ok(TftpOpcode::WriteRequest),
3 => Ok(TftpOpcode::Data),
4 => Ok(TftpOpcode::Acknowledgement),
5 => Ok(TftpOpcode::Error),
_ => Err("something went wrong")
}
}
}
The compiler doesn't like this and gives me an error message:
error: ambiguous associated item
--> src\main.rs:65:45
|
65 | fn try_from(value: u16) -> Result<Self, Self::Error> {
| ^^^^^^^^^^^ help: use fully-qualified syntax: `<TftpOpcode as TryFrom>::Error`
|
I read the hint and tried to apply the fix the compiler told me, which produced this error and confused me even more:
error[E0107]: wrong number of type arguments: expected 1, found 0
--> src\main.rs:65:45
|
65 | fn try_from(value: u16) -> Result<Self, <TftpOpcode as TryFrom>::Error> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 type argument
It took me a lot of time to figure out that TryFrom needs its type argument just like in the impl block to compile properly. In my experience the compiler's hints have usually been helpful and following them fixed things most of the time.
My question is: Is it possible to have this error message include the type parameters to TryFrom so that one can literally follow (i.e. copy-paste) the compiler's suggestion?
If not, is it possible to better indicate the position where the type argument is missing? I was thinking something was wrong with my usage of Result first.