You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm able to define impls for traits in modules in which neither the trait nor the type are defined. My understanding is that this is illegal (the so-called 'the orphan rule').
Bug
mod serde {traitSerialize<letN:u32>{fnserialize(self) -> [Field;N];}}usecrate::serde::Serialize;// Even though Field is primitive, this should be illegal - the definition should go inside the serde// module (where the trait is defined).implSerialize<1>forField{fnserialize(self) -> [Field;1]{[self]}}mod many_field {structManyFieldStruct{a:Field,b:Field,}}usecrate::many_field::ManyFieldStruct;// In this case neither the struct nor the trait have been defined in this module, yet I can still provide// an impl and break the orphan rule.implSerialize<2>forManyFieldStruct{fnserialize(self) -> [Field;2]{[self.a.serialize()[0],self.b.serialize()[0]]}}#[test]fntest(){let bar = ManyFieldStruct{a:13,b:42};let bar_ser = bar.serialize();assert_eq(bar_ser[0],13);assert_eq(bar_ser[1],42);}
I tried wrapping trait with an inner module (i.e. serde::foo::Serialize), and then place a second impl for Field there, and got a duplicate impl error, so at least that seems to work as expected.
The text was updated successfully, but these errors were encountered:
I think this is working as expected. The orphan rule seems to be about not allowing these across crates, but here everything is defined in the same crate.
Aim
I'm able to define impls for traits in modules in which neither the trait nor the type are defined. My understanding is that this is illegal (the so-called 'the orphan rule').
Bug
I tried wrapping trait with an inner module (i.e.
serde::foo::Serialize
), and then place a second impl for Field there, and got a duplicate impl error, so at least that seems to work as expected.The text was updated successfully, but these errors were encountered: