Skip to content

Rust::com Derive macro for CommData#156

Draft
bharatGoswami8 wants to merge 1 commit intoeclipse-score:mainfrom
bharatGoswami8:rust_com_api_derive_macro_commData
Draft

Rust::com Derive macro for CommData#156
bharatGoswami8 wants to merge 1 commit intoeclipse-score:mainfrom
bharatGoswami8:rust_com_api_derive_macro_commData

Conversation

@bharatGoswami8
Copy link
Contributor

  • Implemented derive macro for CommData trait impl

#155

@bharatGoswami8 bharatGoswami8 force-pushed the rust_com_api_derive_macro_commData branch from 719a3c7 to 7524795 Compare February 10, 2026 03:18
* Implemented derive macro for CommData trait impl
@bharatGoswami8 bharatGoswami8 force-pushed the rust_com_api_derive_macro_commData branch from 7524795 to 14a7f51 Compare February 12, 2026 08:14
Copy link
Contributor

@pawelrutkaq pawelrutkaq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, one issues found

#[derive(Debug, Reloc)]
#[derive(Debug, CommData)]
#[repr(C)]
// No explicit ID provided, so it will be auto-generated as "com_api_gen::Exhaust"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing ComData ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked an my bad -all good.

@bharatGoswami8 bharatGoswami8 self-assigned this Mar 6, 2026

/// Derive macro for the `CommData` trait. This macro automatically implements the `CommData` trait
/// for structs and C-like enums, ensuring that the type is marked with `#[repr(C)]`.
/// It internally also derives the `Reloc` trait for the type.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better if Reloc is a supertrait of CommData and the user needs to explicitly #[derive] this as well? The same is true for e.g. Copy, which requirees Clone to be derived as well. It would make this aspect explicit without adding a lot of extra typing required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Auto‑implementing Reloc cuts down on boilerplate, but it hides the fact that the trait is needed. Just like how Copy requires Clone in Rust, CommData should explicitly derive Reloc. This makes the dependency clear in the code, and helps users understand what’s going on. The small amount of extra typing is worth the clarity.

/// It internally also derives the `Reloc` trait for the type.
///
/// # Important
/// Users must NOT implement the `CommData` trait manually. Always use this derive macro instead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? What constraints exist here and could they be enforced somehow so that the user could also implement this manually?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Users shouldn’t implement this manually because their type must have #[repr(C)], and only the macro can verify that.
I tried preventing manual implementations of CommData with a private type and a sealed trait, but that added extra exported items in generated user scope, which still let users implement the trait. So my option is to explain this clearly in the concept crate, with warnings telling users not to implement CommData or Reloc themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible solution -
by making CommData trait unsafe and detail documentation, we can enforce user to use macro of CommData and if manually implementation then also user need to make sure it is implemented correctly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If #[repr(C)] is required then marking this trait unsafe is a requirement because to my knowledge you cannot enforce this using a language construct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me unsafe is a good tradeoff. At the end, in 99% user shall still not do it manually and the advice in doc shall be left to use derive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants