diff options
author | Eric Seppanen <eds@reric.net> | 2021-06-02 18:17:15 -0700 |
---|---|---|
committer | Emilio Cobos Álvarez <emilio@crisal.io> | 2021-07-16 21:23:05 +0200 |
commit | b6109c00fcb13a27e06387ce2ef417a54c24cf3f (patch) | |
tree | 3a93f160080ad10e7b009b6b62a9495180279686 /src | |
parent | 67538b64ea1b90a0d01e4f6ebd45713ec03c9222 (diff) |
add custom derives callback
This callback allows us to specify arbitrary derive attributes for each
named struct. This is useful for adding things that can't easily be
implemented separately, such as `serde::Deserialize` or
`zerocopy::FromBytes`.
Diffstat (limited to 'src')
-rw-r--r-- | src/callbacks.rs | 8 | ||||
-rw-r--r-- | src/codegen/mod.rs | 9 |
2 files changed, 17 insertions, 0 deletions
diff --git a/src/callbacks.rs b/src/callbacks.rs index e288af43..9b345449 100644 --- a/src/callbacks.rs +++ b/src/callbacks.rs @@ -95,4 +95,12 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe { ) -> Option<ImplementsTrait> { None } + + /// Provide a list of custom derive attributes. + /// + /// If no additional attributes are wanted, this function should return an + /// empty `Vec`. + fn add_derives(&self, _name: &str) -> Vec<String> { + vec![] + } } diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 515ebf17..c70c1064 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2005,6 +2005,15 @@ impl CodeGenerator for CompInfo { let mut derives: Vec<_> = derivable_traits.into(); derives.extend(item.annotations().derives().iter().map(String::as_str)); + // The custom derives callback may return a list of derive attributes; + // add them to the end of the list. + let custom_derives; + if let Some(cb) = &ctx.options().parse_callbacks { + custom_derives = cb.add_derives(&canonical_name); + // In most cases this will be a no-op, since custom_derives will be empty. + derives.extend(custom_derives.iter().map(|s| s.as_str())); + }; + if !derives.is_empty() { attributes.push(attributes::derives(&derives)) } |