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
it also makes generalizing things more involved (implementing X trait on members means implementing it on Member and PartialMember, most likely duplicating much of the function body because of type-safety), this came up in the now-stale cdn endpoints creator module and happens in twilight-mention (for example Mention isnt implemented for PartialMember or PartialUser and it's implemented twice for User and CurrentUser)
proposal
essentially rename PartialX to X, add an Option<Enum> field to X, where Enum has variants for each of the specialized fields
structMemberMessage{permissions:Option<Permissions>,// other fields specific to `PartialMember`}structMemberUpdate{flags:MemberFlags,// other fields specific to `MemberUpdate`}enumMemberData{Message(MemberMessage),// what's currently named `PartialMember`Update(MemberUpdate),}structMember{id:Id<UserMarker>,// common fields across all `Member` typesdata:Option<MemberData>,// can be non-option if there's always some variant that's returned}
this would also improve documentation, currently we have many duplicate types that aren't put in the same module
drawbacks
sometimes we might not know which variant to use, meaning we want to use serde's untagged representation
sometimes the main struct might not be enough for some methods, meaning those methods fail if the data's variant isn't expected, see alternative for a solution
there will still be duplicate fields across different enum variants, unless we nest enums inside enums which i think is going too far
adding new variants is a breaking change unless we make the enums #[non_exhaustive] which might be inconvenient to work with for the users, see alternative for a solution
alternative
we could also make the types have a generic parameter which is used in the data field, this solves some of the drawbacks but introduces others, see below for them
structMemberMessage{permissions:Option<Permissions>,// other fields specific to `PartialMember`}structMemberUpdate{flags:MemberFlags,// other fields specific to `MemberUpdate`}structMember<T>{id:Id<UserMarker>,// common fields across all `Member` typesdata:T,}// `Member` will be used likeMember<NoData> // akin to `Member.data = None`Member<MemberMessage>
Member<MemberUpdate>
drawbacks
not applicable when we don't know the type of the data field
might make it harder for users to know what T can be, we could implement a marker trait for possible T types
The text was updated successfully, but these errors were encountered:
This increases the complexity to work with normal - non partial - structs. I don't think that this is good DX:
let member = get_member();let flags = match member {MemberData::Message(data) => data.flags,
_ => unreachable!(),}
vs.
let member = get_member();let flags = member.flags;
This is imho the biggest drawback. You know for 100% which variant it is - e.g. because the member comes from a GuildMemberAdd event - but you still need to match the enum, in order to get the data out.
Additionally we would also need to implement custom deserializers for every struct which has a partial representation, which is also not really feasible.
Generally imho there are too many drawbacks compared to the few benefits this would bring.
Keeping code DRY is definitely something we should keep in mind, but overdoing it is also not good, we need to find the middle way.
This is imho the biggest drawback. You know for 100% which variant it is - e.g. because the member comes from a GuildMemberAdd event - but you still need to match the enum, in order to get the data out.
Just want to mention let-else as a better alternative to matching:
let member = get_member();letMemberData::Message(data) = member else { unreachable!()};let flags = data.flags;
discord doesn't always end us the full data, which is handled by creating partially duplicate types such as https://api.twilight.rs/twilight_model/guild/struct.PartialMember.html and https://api.twilight.rs/twilight_model/guild/struct.Member.html but this comes with the obvious problem of duplicate fields
it also makes generalizing things more involved (implementing
X
trait on members means implementing it onMember
andPartialMember
, most likely duplicating much of the function body because of type-safety), this came up in the now-stale cdn endpoints creator module and happens intwilight-mention
(for exampleMention
isnt implemented forPartialMember
orPartialUser
and it's implemented twice forUser
andCurrentUser
)proposal
essentially rename
PartialX
toX
, add anOption<Enum>
field toX
, whereEnum
has variants for each of the specialized fieldsthis would also improve documentation, currently we have many duplicate types that aren't put in the same module
drawbacks
#[non_exhaustive]
which might be inconvenient to work with for the users, see alternative for a solutionalternative
we could also make the types have a generic parameter which is used in the
data
field, this solves some of the drawbacks but introduces others, see below for themdrawbacks
data
fieldT
can be, we could implement a marker trait for possibleT
typesThe text was updated successfully, but these errors were encountered: