Revert "Stop special-casing zero fields in macro code" for ruma-events

This partially reverts commit 9b2602649f9e0fcb875404fc86d9c6fdbedd287d.
This commit is contained in:
Jonas Platte 2021-07-26 15:01:47 +02:00
parent 6236b024fd
commit 64fc1c794c
No known key found for this signature in database
GPG Key ID: CC154DE0E30B7C67
2 changed files with 19 additions and 13 deletions

View File

@ -173,15 +173,23 @@ pub fn expand_event_content(
}; };
let redaction_struct_fields = kept_redacted_fields.iter().flat_map(|f| &f.ident); let redaction_struct_fields = kept_redacted_fields.iter().flat_map(|f| &f.ident);
// Used in `EventContent::redacted` which only returns zero sized types (unit structs). // redacted_fields allows one to declare an empty redacted event without braces,
let redacted_return = if kept_redacted_fields.is_empty() { // otherwise `RedactedWhateverEventContent {}` is needed.
quote! { Ok(#redacted_ident {}) } // The redacted_return is used in `EventContent::redacted` which only returns
// zero sized types (unit structs).
let (redacted_fields, redacted_return) = if kept_redacted_fields.is_empty() {
(quote! { ; }, quote! { Ok(#redacted_ident {}) })
} else { } else {
quote! { (
Err(#serde::de::Error::custom( quote! {
format!("this redacted event has fields that cannot be constructed") { #( #kept_redacted_fields, )* }
)) },
} quote! {
Err(#serde::de::Error::custom(
format!("this redacted event has fields that cannot be constructed")
))
},
)
}; };
let has_deserialize_fields = if kept_redacted_fields.is_empty() { let has_deserialize_fields = if kept_redacted_fields.is_empty() {
@ -202,7 +210,7 @@ pub fn expand_event_content(
impl #redacted_ident { impl #redacted_ident {
#[doc = #doc] #[doc = #doc]
pub fn new() -> Self { pub fn new() -> Self {
Self {} Self
} }
} }
} }
@ -244,9 +252,7 @@ pub fn expand_event_content(
#[doc = #doc] #[doc = #doc]
#[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)] #[derive(Clone, Debug, #serde::Deserialize, #serde::Serialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)] #[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct #redacted_ident { pub struct #redacted_ident #redacted_fields
#( #kept_redacted_fields, )*
}
#initializer #initializer

View File

@ -27,7 +27,7 @@ pub struct DummyToDeviceEventContent;
impl DummyToDeviceEventContent { impl DummyToDeviceEventContent {
/// Create a new `DummyToDeviceEventContent`. /// Create a new `DummyToDeviceEventContent`.
pub fn new() -> Self { pub fn new() -> Self {
Self {} Self
} }
} }