Generate From impls for event (content) enums.#693
Conversation
|
I decided to create a derive macro for the manually defined enums. I'm not sure if this the best or desired way to do this, and there is probably currently some code duplication that I could refactor. |
jplatte
left a comment
There was a problem hiding this comment.
I wouldn't personally have used a proc-macro when it's only used in four places, but I suppose we would have somewhat boilerplate-y code there otherwise.
| let var_ident = &variant.ident.to_token_stream(); | ||
| let id = &input.ident; | ||
| quote! { | ||
| impl From<#inner_struct> for #id { |
There was a problem hiding this comment.
Can we instead implement From<T> for #id where T: Into<#inner_struct>? That would allow both AnySyncMessageEvent => AnySyncRoomEvent (using the blanket From<T> for T impl from std to fulfill the bound) and SyncMessageEvent<MessageEventContent> => AnySyncRoomEvent.
There was a problem hiding this comment.
I'm not sure we can implement From<T> for #id where T: Into<#inner_struct>.
When I tried, I got a bunch of
conflicting implementations of trait `std::convert::From<_>` for type `enums::AnyRoomEvent`
first implementation here (.....)
I'm guessing that the compiler is being conservative and assumes that someone could implement From<OneVariant> for AnotherVariant, causing an overlap in the impls?
There was a problem hiding this comment.
Oh, right. Somebody in an external crate could implement Into<AnySyncMessageEvent> as well as Into<AnySyncStateEvent> for the same type. Hmm. In that case let's leave this as-is, it doesn't hurt though we should have another issue for allowing more conversions here in one way or another.
jplatte
left a comment
There was a problem hiding this comment.
Would be nice to have a changelog entry for this, otherwise LGTM.
Resolves: #245
Tasks:
From<SpecificEventContent> for Any[kind]EventContentFrom<SpecificEvent> for Any[kind]EventFromimpls for the manually defined enums.I'm not sure what other
Forimplementations which could be useful, but I'd gladly add those too (if any).