Hi to All, guys
I'm in the situation where i got multiple composed enums, like this :
enum Aenum {
B(Benum),
C(Cdata),
}
enum Benum {
F(Fdata),
G(Gdata),
H(Hdata),
}
struct Fdata {
number: usize,
char: char,
payload: String,
}
struct Gdata {
number: usize,
}
struct Hdata {
number: usize,
payload: String,
}
struct Cdata {
payload: String,
}
impl From<Cdata> for Aenum {
fn from(value: Cdata) -> Self {
Aenum::C(value)
}
}
impl From<Benum> for Aenum {
fn from(value: Benum) -> Self {
Aenum::B(value)
}
}
impl From<Fdata> for Benum {
fn from(value: Fdata) -> Self {
Benum::F(value)
}
}
impl From<Gdata> for Benum {
fn from(value: Gdata) -> Self {
Benum::G(value)
}
}
impl From<Hdata> for Benum {
fn from(value: Hdata) -> Self {
Benum::H(value)
}
}
and i can convert now Fdata to Aenum in this way :
let f = Fdata {
number: 1,
char: 'c',
payload: "Hello!".to_string(),
};
let a_example = Aenum::from(Benum::from(f));
now i want to convert from &Fdata to &Aenum in this fashion:
fn convert(f_ref: &Fdata) -> &Aenum {
todo!()
}
but the only solution(in safe rust at least) that i found is to build a specular enum structure like this :
enum AenumRef<'a> {
B(BenumRef<'a>),
C(&'a Cdata),
}
enum BenumRef<'a> {
F(&'a Fdata),
G(&'a Gdata),
H(&'a Hdata),
}
impl<'a> From<& 'a Cdata> for AenumRef<'a> {
fn from(value: & 'a Cdata) -> Self {
AenumRef::C(value)
}
}
impl<'a> From<BenumRef<'a>> for AenumRef<'a> {
fn from(value: BenumRef<'a>) -> Self {
AenumRef::B(value)
}
}
impl<'a> From<& 'a Fdata> for BenumRef<'a> {
fn from(value: & 'a Fdata) -> Self {
BenumRef::F(value)
}
}
impl<'a> From<& 'a Gdata> for BenumRef<'a> {
fn from(value: & 'a Gdata) -> Self {
BenumRef::G(value)
}
}
impl<'a> From<& 'a Hdata> for BenumRef<'a> {
fn from(value: & 'a Hdata) -> Self {
BenumRef::H(value)
}
}
And now i can convert from ref to ref like this :
fn convert_r<'a>(f_ref: &'a Fdata) -> AenumRef<'a> {
AenumRef::B(BenumRef::from(f_ref))
}
My question is: is this the better way to convert from ref to ref in a situation like this or exist a better and less verbant way to do this ?
Thanks in advance for your time, guys