Reputation: 62
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?
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)
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.
Upvotes: 0
Views: 437
Reputation: 59882
No, a From<A> for B
implementation will not create a From<B> for A
implementation.
The From
trait is composed of only a single method fn from(a: A) -> B
. With just this signature, would you be able to create the reverse implementation for all A
and B
? Of course not! And the compiler will not look at the existing implementation's body to try to deduce the other. For one thing, many conversions are lossy, many conversions are fallible, and may have hurdles converting one way that don't exist when converting the opposite direction. So even if the compiler did look at the existing implementation, its not practical or even possible in general.
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.
Indeed, you've misinterpreted the quote. It is essentially saying the same thing twice, but in a different context: "convert type A from type B" is the same operation as "convert type B to type A", both are B -> A
, just the subject of the phrasing has changed. And this reflects the only difference between From
and Into
. The syntaxes A::from(b)
and b.into()
(with inferred A
) cannot be done with a single trait.
If you're looking to make your life easier when dealing with enums, as already mentioned, the strum crate has many derive macros designed to:
IntoStaticStr
and/or ToString
EnumString
u8
: FromRepr
u8
can be done with just as u8
if #[repr(u8)]
is added)See these existing answers for other options:
Upvotes: 3
Reputation: 1
Maybe strum_macros can help.
It can generate code to convert enum value to &str
use strum_macros::IntoStaticStr;
#[derive(IntoStaticStr)]
enum NormalMneumonic {
}
Upvotes: 0