简体   繁体   中英

How to cascading implement Into trait for struct?

I'm trying to blanket implement some convert trait for struct, to avoid unnecessary into() mess. Here is what I'm trying:

struct TypeA {}
struct TypeB {}

impl Into<TypeA> for TypeB {
    fn into(self) -> TypeA { unimplemented!(); }
}

impl<T: Into<TypeB>> Into<TypeA> for T {
    fn into(self) -> TypeA {
        unimplemented! ();
    }
}

struct TypeC {}

impl Into<TypeB> for TypeC {
    fn into(self) -> TypeB { unimplemented!(); }
}

But the compiler throw the following error

conflicting implementations of trait `std::convert::Into<TypeA>`
= note: conflicting implementation in crate `core`:
            - impl<T, U> Into<U> for T
              where U: From<T>;

So, how to blanket implement trait Into<TypeA> for any type that implement Into<TypeB>? Both TypeA, TypeB and TypeC is in local crate.

I just had to figure this out converting errors. For TypeA , I implemented From on Into<TypeB> . This allows anything that can convert Into<TypeB> to convert Into<TypeA> . That last test converts from TypeC to TypeA .

struct TypeA {
    name: String,
}
struct TypeB {
    name: String,
}
struct TypeC {
    name: String,
}

impl From<TypeC> for TypeB {
    fn from(c: TypeC) -> Self {
        TypeB { name: c.name }
    }
}

impl<T: Into<TypeB>> From<T> for TypeA {
    fn from(b: T) -> Self {
        let b = b.into();
        TypeA { name: b.name }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn c_to_b() {
        let c = TypeC {
            name: "C".to_string(),
        };
        let b: TypeB = c.into();
        assert_eq!("C", b.name.as_str())
    }

    #[test]
    fn b_to_a() {
        let b = TypeB {
            name: "B".to_string(),
        };
        let a: TypeA = b.into();
        assert_eq!("B", a.name.as_str())
    }

    #[test]
    fn c_to_a() {
        let c = TypeC {
            name: "C".to_string(),
        };
        let a: TypeA = c.into();
        assert_eq!("C", a.name.as_str())
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM