Skip to content

Commit 57fb272

Browse files
committed
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`.
1 parent b60339e commit 57fb272

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

src/callbacks.rs

+5
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,9 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
9595
) -> Option<ImplementsTrait> {
9696
None
9797
}
98+
99+
/// Provide a list of custom derive attributes.
100+
fn add_derives(&self, _name: &str) -> Option<Vec<String>> {
101+
None
102+
}
98103
}

src/codegen/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1997,6 +1997,16 @@ impl CodeGenerator for CompInfo {
19971997
let mut derives: Vec<_> = derivable_traits.into();
19981998
derives.extend(item.annotations().derives().iter().map(String::as_str));
19991999

2000+
// Check to see if there is a custom derives callback, and call that
2001+
// function to see if any custom derive attributes should be added.
2002+
let custom_derives;
2003+
if let Some(cb) = &ctx.options().parse_callbacks {
2004+
custom_derives = cb.add_derives(&canonical_name);
2005+
if let Some(custom_derive_vec) = &custom_derives {
2006+
derives.extend(custom_derive_vec.iter().map(|s| s.as_str()));
2007+
}
2008+
};
2009+
20002010
if !derives.is_empty() {
20012011
attributes.push(attributes::derives(&derives))
20022012
}

0 commit comments

Comments
 (0)