How to impl From<&T> when T is copy?

Say

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum A {
    Int8,

pub enum B {
    Int8,

I have

impl From<A> for B {
    fn from(item: A) -> Self {
        match item {
            A::Int8 => B::Int8,
        }
    }
}

When users have let a: &A, they need to write let b: B = (*a).into();

is there a way to avoid the (*a) and allow both &A and A to be converted?

You should be able to add the impl you mentioned.

impl<'a> From<&'a A> for B {
    fn from(item: &'a A) -> Self {
        match item {
            A::Int8 => B::Int8,
        }
    }
}
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.