Skip to content

Commit d9be065

Browse files
committed
Replace SIZE and ALIGN constants with a single LAYOUT constant
1 parent d4ddfcb commit d9be065

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

rust/binaryninja-derive/src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,11 @@ fn impl_abstract_structure_type(
298298
(quote! { #width }, quote! { #align })
299299
}
300300
FieldKind::Ty(ty) => (
301-
quote! { <#ty as ::binaryninja::types::AbstractType>::SIZE },
302-
quote! { { <#ty as ::binaryninja::types::AbstractType>::ALIGN } },
301+
quote! { { <#ty as ::binaryninja::types::AbstractType>::LAYOUT.size() } },
302+
quote! { { <#ty as ::binaryninja::types::AbstractType>::LAYOUT.align() } },
303303
),
304304
};
305-
quote! { #ident: #field_wrapper<[u8; #size], #align> }
305+
quote! { #ident: #field_wrapper<#size, #align> }
306306
})
307307
.collect::<Vec<_>>();
308308
let args = abstract_fields
@@ -330,7 +330,7 @@ fn impl_abstract_structure_type(
330330
.map(|n| {
331331
(
332332
quote! { #[repr(align(#n))] },
333-
quote! { .set_alignment(Self::ALIGN) },
333+
quote! { .set_alignment(Self::LAYOUT.align()) },
334334
)
335335
})
336336
.unzip();
@@ -348,12 +348,12 @@ fn impl_abstract_structure_type(
348348
Ok(quote! {
349349
#[repr(C)]
350350
#[derive(Copy, Clone)]
351-
struct #field_wrapper<T, const N: usize>
351+
struct #field_wrapper<const SIZE: usize, const ALIGN: usize>
352352
where
353-
::binaryninja::elain::Align<N>: ::binaryninja::elain::Alignment
353+
::binaryninja::elain::Align<ALIGN>: ::binaryninja::elain::Alignment
354354
{
355-
t: T,
356-
_align: ::binaryninja::elain::Align<N>,
355+
t: [u8; SIZE],
356+
_align: ::binaryninja::elain::Align<ALIGN>,
357357
}
358358

359359
#[repr(C)]
@@ -364,13 +364,12 @@ fn impl_abstract_structure_type(
364364
}
365365

366366
impl ::binaryninja::types::AbstractType for #name {
367-
const SIZE: usize = ::std::mem::size_of::<#layout_name>();
368-
const ALIGN: usize = ::std::mem::align_of::<#layout_name>();
367+
const LAYOUT: ::std::alloc::Layout = ::std::alloc::Layout::new::<#layout_name>();
369368
fn resolve_type() -> ::binaryninja::rc::Ref<::binaryninja::types::Type> {
370369
::binaryninja::types::Type::structure(
371370
&::binaryninja::types::Structure::builder()
372371
#(.insert(#args))*
373-
.set_width(Self::SIZE as u64)
372+
.set_width(Self::LAYOUT.size() as u64)
374373
.set_packed(#is_packed)
375374
#set_alignment
376375
#set_union

rust/src/types.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::{
3333
use lazy_static::lazy_static;
3434
use std::ptr::null_mut;
3535
use std::{
36+
alloc::Layout,
3637
borrow::{Borrow, Cow},
3738
collections::{HashMap, HashSet},
3839
ffi::CStr,
@@ -698,8 +699,9 @@ impl Drop for TypeBuilder {
698699
#[cfg(feature = "derive")]
699700
pub use binaryninja_derive::*;
700701
pub trait AbstractType: Sized {
701-
const SIZE: usize = std::mem::size_of::<Self>();
702-
const ALIGN: usize = std::mem::align_of::<Self>();
702+
#[doc(hidden)]
703+
const LAYOUT: Layout = Layout::new::<Self>();
704+
703705
fn resolve_type() -> Ref<Type>;
704706
}
705707

0 commit comments

Comments
 (0)