Skip to content

derive Transmutable for sector-backed types#105

Open
val-levy wants to merge 1 commit into
mainfrom
transmutable-derive
Open

derive Transmutable for sector-backed types#105
val-levy wants to merge 1 commit into
mainfrom
transmutable-derive

Conversation

@val-levy
Copy link
Copy Markdown
Contributor

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:

  • Fixed size at compile time
  • Contains no invalid bit patterns
  • Type can be converted into raw bytes

without this, stuff like

self.sectors.len() / Sector::LEN

wont compile.

also, got rid of a prev impl for this for the Order struct

@val-levy val-levy requested review from alnoki and finnfujimura April 27, 2026 05:21
@val-levy val-levy added the enhancement New feature or request label Apr 27, 2026
@finnfujimura finnfujimura requested a review from Copilot April 27, 2026 22:46
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 Transmutable derive entrypoint to instruction-macros.
  • Implement the derive helper that generates an unsafe impl Transmutable with LEN.
  • Switch interface::state::Order from a manual unsafe impl Transmutable to #[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>();
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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(())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants