Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support feature-gated rust attributes #2813

Merged
merged 5 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/re_types/definitions/rerun/components/instance_key.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct InstanceKey (
"attr.python.array_aliases": "int, npt.NDArray[np.uint64]",
"attr.rerun.legacy_fqname": "rerun.instance_key",
"attr.rust.derive": "Copy, Hash, PartialEq, Eq, PartialOrd, Ord",
"attr.rust.custom_clause":
'cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))',
order: 100
) {
value: uint64 (order: 100);
Expand Down
6 changes: 6 additions & 0 deletions crates/re_types/definitions/rust/attributes.fbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ attribute "attr.rust.tuple_struct";
/// E.g. "attr.rust.derive": "Copy"`.
attribute "attr.rust.derive";

/// Apply to any object to generate an arbitrary clause.
///
/// The value of the attribute will be trimmed out but otherwise left as-is.
/// E.g. "attr.rust.custom_clause": "cfg_attr(feature = "serde", derive(::serde::Serialize))"`.
attribute "attr.rust.custom_clause";

/// Apply to any object to generate a #repr clause with the specified value.
attribute "attr.rust.repr";
2 changes: 1 addition & 1 deletion crates/re_types/source_hash.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/re_types/src/components/instance_key.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions crates/re_types/src/components/instance_key_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,3 @@ impl std::fmt::Display for InstanceKey {
}
}
}

// TODO(jleibs): allow cfg_attr for codegen

#[cfg(feature = "serde")]
impl serde::Serialize for InstanceKey {
#[inline]
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.serialize(serializer)
}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for InstanceKey {
#[inline]
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
u64::deserialize(deserializer).map(Self::from)
}
}
22 changes: 16 additions & 6 deletions crates/re_types_builder/src/codegen/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::{
codegen::{StringExt as _, AUTOGEN_WARNING},
ArrowRegistry, CodeGenerator, Docs, ElementType, Object, ObjectField, ObjectKind, Objects,
Type, ATTR_RERUN_COMPONENT_OPTIONAL, ATTR_RERUN_COMPONENT_RECOMMENDED,
ATTR_RERUN_COMPONENT_REQUIRED, ATTR_RERUN_LEGACY_FQNAME, ATTR_RUST_DERIVE, ATTR_RUST_REPR,
ATTR_RUST_TUPLE_STRUCT,
ATTR_RERUN_COMPONENT_REQUIRED, ATTR_RERUN_LEGACY_FQNAME, ATTR_RUST_CUSTOM_CLAUSE,
ATTR_RUST_DERIVE, ATTR_RUST_REPR, ATTR_RUST_TUPLE_STRUCT,
};

// TODO(cmc): it'd be nice to be able to generate vanilla comments (as opposed to doc-comments)
Expand Down Expand Up @@ -382,6 +382,7 @@ impl QuotedObject {
let quoted_derive_clone_debug = quote_derive_clone_debug();
let quoted_derive_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_DERIVE, "derive");
let quoted_repr_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_REPR, "repr");
let quoted_custom_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_CUSTOM_CLAUSE, "");

let quoted_fields = fields
.iter()
Expand All @@ -405,6 +406,7 @@ impl QuotedObject {
#quoted_derive_clone_debug
#quoted_derive_clause
#quoted_repr_clause
#quoted_custom_clause
#quoted_struct

#quoted_from_impl
Expand Down Expand Up @@ -450,6 +452,7 @@ impl QuotedObject {
let quoted_derive_clone_debug = quote_derive_clone_debug();
let quoted_derive_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_DERIVE, "derive");
let quoted_repr_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_REPR, "repr");
let quoted_custom_clause = quote_meta_clause_from_obj(obj, ATTR_RUST_CUSTOM_CLAUSE, "");

let quoted_fields = fields.iter().map(|obj_field| {
let ObjectField {
Expand Down Expand Up @@ -490,6 +493,7 @@ impl QuotedObject {
#quoted_derive_clone_debug
#quoted_derive_clause
#quoted_repr_clause
#quoted_custom_clause
pub enum #name {
#(#quoted_fields,)*
}
Expand Down Expand Up @@ -658,10 +662,16 @@ fn quote_derive_clone_debug() -> TokenStream {
fn quote_meta_clause_from_obj(obj: &Object, attr: &str, clause: &str) -> TokenStream {
let quoted = obj
.try_get_attr::<String>(attr)
.map(|what| {
syn::parse_str::<syn::MetaList>(&format!("{clause}({what})"))
.with_context(|| format!("illegal meta clause: {what:?}"))
.unwrap()
.map(|contents| {
if clause.is_empty() {
syn::parse_str::<syn::Meta>(contents.as_str())
.with_context(|| format!("illegal meta clause: {clause:?}"))
.unwrap()
} else {
syn::parse_str::<syn::Meta>(&format!("{clause}({contents})"))
.with_context(|| format!("illegal meta clause: {clause}({contents})"))
.unwrap()
}
})
.map(|clause| quote!(#[#clause]));
quote!(#quoted)
Expand Down
1 change: 1 addition & 0 deletions crates/re_types_builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub const ATTR_RERUN_LEGACY_FQNAME: &str = "attr.rerun.legacy_fqname";
pub const ATTR_PYTHON_ALIASES: &str = "attr.python.aliases";
pub const ATTR_PYTHON_ARRAY_ALIASES: &str = "attr.python.array_aliases";

pub const ATTR_RUST_CUSTOM_CLAUSE: &str = "attr.rust.custom_clause";
pub const ATTR_RUST_DERIVE: &str = "attr.rust.derive";
pub const ATTR_RUST_REPR: &str = "attr.rust.repr";
pub const ATTR_RUST_TUPLE_STRUCT: &str = "attr.rust.tuple_struct";
Expand Down