-
Notifications
You must be signed in to change notification settings - Fork 26
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
Add derive macros for Dispatch, ExtensionDispatch #98
Conversation
d3e033a
to
6d7a61d
Compare
@daringer @sosthene-nitrokey Any thoughts on the syntax? I think it would make it much easier to maintain the dispatch implementations. |
This hides a lot of redundant code, I love it! especially here: https://github.com/trussed-dev/trussed/pull/98/files#diff-203bd1efad2e5b766e73f7058bc930bc35004df65aeb5088a6c5845f662bdd43L383 lot of boilerplate stuff just gone... +1 |
My main concern is whether it can do what we do with the Other than that, i would put the #[derive(Default, trussed::backend::Dispatch)]
#[dispatch(backend_id = "Backend")]
struct Dispatch {
test: TestBackend,
}
#[derive(Default, ExtensionDispatch)]
#[dispatch(backend_id = "Backend", extension_id = "Extension")]
#[dispatch(extensions(Test = "TestExtension", Sample = "SampleExtension"))] // maps the extension ID to the type
pub struct Backends {
#[dispatch(extensions("Test"))] // declares the extensions supported by this backend
test: TestBackend,
#[dispatch(extensions("Test", "Sample"))]
sample: SampleBackend,
} |
I don’t see a problem with that. We can just use fields with different names but the same type. I think something like this should already work with the current implementation: #[derive(Default, ExtensionDispatch)]
#[dispatch(backend_id = "Backend", extension_id = "Extension")]
#[extensions(Test = "TestExtension", Special = "SpecialExtension", Sample = "SampleExtension")]
pub struct Backends {
#[extensions("Test")]
test: TestBackend,
#[extensions("Test", "Special")]
test_special: TestBackend,
#[extensions("Test", "Sample")]
sample: SampleBackend,
}
I also thought about that, but on the other hand, it just adds more boilerplate code without real benefit. And looking at the most popular derive crates, using multiple attributes is quite common. For example, |
If we don’t want to duplicate the backend, we could probably use a special attribute like: #[derive(Default, ExtensionDispatch)]
#[dispatch(backend_id = "Backend", extension_id = "Extension")]
#[extensions(Test = "TestExtension", Special = "SpecialExtension", Sample = "SampleExtension")]
pub struct Backends {
#[extensions("Test")]
test: TestBackend,
#[extensions("Test", "Special")]
#[dispatch(delegate_to = "test")]
test_special: TestBackend,
#[extensions("Test", "Sample")]
sample: SampleBackend,
} |
Would that work for a |
Hmm, then we could change the type to |
Yes |
a008bc4
to
79764af
Compare
Updated:
|
Updated:
|
This patch adds a new derive crate that can be used to derive implementations of the trussed::backend::Dispatch and trussed::serde_extensions::ExtensionDispatch traits.
Backends may only implement core requests and no extensions, therefore the extensions attribute should not be required for backends.
This patch adds the ExtensionId derive macro to trussed-derive that implements From<T> for u8 and TryFrom<u8> for T.
This makes it easier to select the supported extensions using conditional compilation (cfg_attr).
Please don’t look at the macro implementation yet, it’s quite messy at the moment. Instead look at the diff for
tests/backend.rs
andtests/serde_extensions.rs
to see the derive macros in action. The current syntax is:What do you think?
Missing features:
extensions
attributes to simplify conditional compilation