Any way to tell the compiler two traits are the same?

Hi Guys,

I have a library that makes basic in-memory data layout called Packman. I have been using it for a while, and I really like it. Now I am doing a migration from two of its versions with breaking changes.

I have two types Pack<T> and VecPack<T>, where for<'de> T: Serialize, Deserialie<'de>, Clone, Debug, Default.

To use a VecPack<T>, data must implement VecPackMember trait, which has one method fn get_id(&self) -> Self::Out;

So now I have an old a new version of my Packman, and I have my stored data in fs in Packman files. I have a migration script, where I open the Packman file as VecPack<T> via my old Packman, and then I create a new Packman VecPack<T>, and store the same T data, but saving with the new VecPack format.

My data has the VecPackMember trait implemented, but thats the old Packman::VecPackMemeber trait, and the new VecPack<T> says type T does not have the necessary VecPackMember implementation. Which is true, but the old and the new Packman VecPackMember are exactly the same.

So do you think there is any way to tell to the compiler, that my Type has a trait implementation and use that one as another trait implementation? As the to implementation would be exactly the same.

This way I would save couple of hours implementing unnecessary implementations.

Thank you in advance.

You might be able to use a newtype for this.

#[derive(Clone,Default,Debug)]
struct WrapMember<T>(T);

impl<T> newlib::VecPackMember for WrapMember<T>
where T: oldlib::VecPackMember {
    type Out = T::Out;
    fn get_id(&self)->Self::Out { self.0.get_id() }
}

impl<T> Serialize for WrapMember<T> where T:Serialize {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: Serializer {
       self.0.serialize(serializer)
   }
}

impl<'de, T> Deserialize<'de> for WrapMember<T> where T:Deserialize<'de> {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'de> {
        Ok(WrapMember(T::deserialize(deserializer)?))
    }
}

// now, construct a newlib::VecPack<WrapMember<T>> from oldlib::VecPack<T> ...
3 Likes

Thank you! This solution works perfectly. Saved my day.