feat(s2n-events): Support defining associated C types for events #2830
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolved issues:
Part of #2767
Description of changes:
This PR allows C types to be defined and associated with events. This will allow the future C APIs that publish events to be provided with the defined C types as arguments.
For an event to be published, the s2n-events publisher must be provided with information that's specific to each event. For example, for an
ApplicationProtocolInformation
event, the publisher must be provided with the array of bytes associated with the received ALPN. This is handled by a "builder" event struct that's generated according to how the event is defined by the user.The C API for s2n-events will allow s2n-tls to invoke the s2n-events publisher, so s2n-tls will also need to provide the publisher with information that's specific to each event. My plan is to use a similar event struct which contains the event-specific data that will be passed to the
on_event
API for that event in the publisher. Unfortunately, it will be difficult to automatically generate the C argument struct for this purpose since the Rust and C structs will generally be different. For example, consider theApplicationProtocolInformation
event, which needs an array of bytes as input. In Rust, this is handled with a single&'a [u8]
field. In C, this needs to be handled with adata: *const u8
field and alen: u32
field.Rather than try to do this conversion as part of the code generation, this PR adds a way for the developer to specify a C-specific struct for each event. The developer can choose the representation of the event data that makes the most sense for C, and then implement
IntoEvent
on the C struct to convert it into the Rust "builder" struct so the event can be published.Call-outs:
This PR only adds support for associating C types with events. These C types will be used when generating the C API for the publisher interface, but this will be done in a separate PR.
Testing:
New tests were added to make sure the
IntoEvent
implementations to convert C structs to the Rust event structs are correct.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.