derive Transmutable for sector-backed types#105
Open
val-levy wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a #[derive(Transmutable)] proc-macro to implement the Transmutable trait for raw, sector-backed state structs, and migrates Order to use the derive instead of a hand-written impl.
Changes:
- Add a
Transmutablederive entrypoint toinstruction-macros. - Implement the derive helper that generates an
unsafe impl TransmutablewithLEN. - Switch
interface::state::Orderfrom a manualunsafe impl Transmutableto#[derive(instruction_macros::Transmutable)].
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| interface/src/state/order.rs | Replace manual Transmutable impl for Order with the new derive. |
| instruction-macros/crates/instruction-macros-derive/src/lib.rs | Register the new #[proc_macro_derive(Transmutable)] entrypoint. |
| instruction-macros/crates/instruction-macros-derive/src/derive/transmutable.rs | Add the derive implementation that generates the unsafe impl. |
| instruction-macros/crates/instruction-macros-derive/src/derive/mod.rs | Export the new transmutable derive helper module. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| fn validate_bit_patterns(_bytes: &[u8]) -> crate::error::DropsetResult { | ||
| Ok(()) | ||
| } | ||
| } |
Comment on lines
+38
to
+40
| unsafe impl crate::state::transmutable::Transmutable for #struct_ident { | ||
| const LEN: usize = ::core::mem::size_of::<Self>(); | ||
|
|
Comment on lines
+25
to
+39
| pub fn derive_transmutable(input: DeriveInput) -> syn::Result<TokenStream> { | ||
| let struct_ident = input.ident.clone(); | ||
|
|
||
| if !has_repr_c_or_transparent(&input) { | ||
| return Err(syn::Error::new_spanned( | ||
| &struct_ident, | ||
| "Transmutable requires `#[repr(C)]` or `#[repr(transparent)]`", | ||
| )); | ||
| } | ||
|
|
||
| require_data_struct(input)?; | ||
|
|
||
| Ok(quote! { | ||
| unsafe impl crate::state::transmutable::Transmutable for #struct_ident { | ||
| const LEN: usize = ::core::mem::size_of::<Self>(); |
Contributor
Author
There was a problem hiding this comment.
added type checking: dont want to pass anything other than account data types as to not introduce more compiling issues given a foreign type transmute.
Comment on lines
+37
to
+45
| Ok(quote! { | ||
| unsafe impl crate::state::transmutable::Transmutable for #struct_ident { | ||
| const LEN: usize = ::core::mem::size_of::<Self>(); | ||
|
|
||
| #[inline(always)] | ||
| fn validate_bit_patterns(_bytes: &[u8]) -> crate::error::DropsetResult { | ||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Adds #[derive(Transmutable)] to types that live in raw sector memory
Currently there is a lot of casting from &[u8] > structs (e.g. Sector, Order, MarketSeat). these types need to be raw and have a fixed size, so Transmutable gives T::LEN
Makes sure:
without this, stuff like
wont compile.
also, got rid of a prev impl for this for the Order struct