Skip to content

Commit

Permalink
Support feature-gated rust attributes (#2813)
Browse files Browse the repository at this point in the history
Fixes #2801 by adding a `attr.rust.custom_clause`.

### What

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested [demo.rerun.io](https://demo.rerun.io/pr/2813) (if
applicable)

- [PR Build Summary](https://build.rerun.io/pr/2813)
- [Docs
preview](https://rerun.io/preview/pr%3Acmc%2Fcodegen_feature_gates/docs)
- [Examples
preview](https://rerun.io/preview/pr%3Acmc%2Fcodegen_feature_gates/examples)
  • Loading branch information
teh-cmc authored Jul 26, 2023
1 parent c265cf6 commit 3c3b56a
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 25 deletions.
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

0 comments on commit 3c3b56a

Please sign in to comment.