Problem
I want to write a single function that allows me to convert from type A (in this case, u8
) to type B (a custom type), and then back from type B to type A. According to the first paragraph of the entry about the traits From
and Into
of Rust by Example:
The
From
andInto
traits are inherently linked, and this is actually part of its implementation. If you are able to convert type A from type B, then it should be easy to believe that we should be able to convert type B to type A.
However, implementing From
(i.e.impl From<A> for B
) allowed me to convert from A to B and only A to B, except in two different ways (still not sure why two ways are necessary but anyway). Can I convert from B to A using the same implementation? Or is there no way to use the information already there?
What I have tried
I tried implementing From
(or TryFrom
in this case) on my type NormalMneumonic
like so
impl TryFrom<&str> for NormalMneumonic {
type Error = Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"JP" => Ok(Self::Jump),
// --snip--
_ => Err("Input is not a normal menumonic."),
}
}
}
With that I'm able to do
let mneumonic_1 /*: Result<NormalMneumonic, _>*/ = NormalMneumonic::try_from("JP");
let mneumonic_2: Result<NormalMneumonic, _> = "JP".try_into();
assert_eq!(mneumonic_1, mneumonic_2);
but I haven't found a way to convert, in this case, from NormalMneumonic
back into &str
.
I'm looking for something like
let mneumonic_string = NormalMneumonic::Jump.try_into(); // or perhaps &str::try_from(NormalMneumonic::Jump)
Some context
I'm trying to write an assembler and a linker for a simplified assembly language using Rust. One of the data types I have defined to help with that is NormalMneumonic
, which is just an enum with a variant for each valid mneumonic.
On writing the assembler, I'll need to read some text file and write some binary, and on linking I'll need to read back some binary files and write a different binary file. With that in mind, I was looking for a way to convert back and forth between a string slice (&str
) or a byte (u8
) and a NormalMneumonic
variant.
From the quote I mentioned, I thought converting back and forth between types was the use case for the From
trait, but it seems the book in this case just uses misleading language.