Skip to content

Commit d9190eb

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

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
@@ -31,6 +31,7 @@ use crate::{
3131

3232
use lazy_static::lazy_static;
3333
use std::{
34+
alloc::Layout,
3435
borrow::{Borrow, Cow},
3536
collections::{HashMap, HashSet},
3637
ffi::CStr,
@@ -694,8 +695,9 @@ impl Drop for TypeBuilder {
694695
#[cfg(feature = "derive")]
695696
pub use binaryninja_derive::*;
696697
pub trait AbstractType: Sized {
697-
const SIZE: usize = std::mem::size_of::<Self>();
698-
const ALIGN: usize = std::mem::align_of::<Self>();
698+
#[doc(hidden)]
699+
const LAYOUT: Layout = Layout::new::<Self>();
700+
699701
fn resolve_type() -> Ref<Type>;
700702
}
701703

0 commit comments

Comments
 (0)