I've always wondered if there is a better way of doing the following, specifically if there is some variation of the impl
declaration that would allow me to use Self:X(X)
instead of XY:X(X)
. Currently swapping XY
with Self
results in a somewhat misleading compile err/suggestion. I suppose I could also use the anon lifetime in the impl decl but I notice it makes the aforementioned compiler diagnostic even worse.
#[derive(Debug)]
struct X;
#[derive(Debug)]
struct Y;
struct Union {
x: X,
y: Y
}
enum Tag {
X, Y
}
#[derive(Debug)]
enum XY<'a> {
X(&'a X),
Y(&'a Y)
}
impl<'s> XY<'s> {
fn new(t: Tag, u: &Union) -> XY {
match t {
Tag::X => XY::X(&u.x),
Tag::Y => XY::Y(&u.y),
}
}
}
fn main() {
let u = Union{ x: X, y: Y };
let xy = XY::new(Tag::X, &u);
dbg!(xy);
}