diff options
Diffstat (limited to 'src/ir/layout.rs')
-rw-r--r-- | src/ir/layout.rs | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/src/ir/layout.rs b/src/ir/layout.rs index 03d43b51..38379261 100644 --- a/src/ir/layout.rs +++ b/src/ir/layout.rs @@ -3,10 +3,10 @@ use super::context::BindgenContext; use super::derive::{CanDeriveCopy, CanDeriveDebug, CanDeriveDefault}; use super::ty::RUST_DERIVE_IN_ARRAY_LIMIT; -use std::cmp; +use std::{cmp, mem}; /// A type that represents the struct layout of a type. -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub struct Layout { /// The size (in bytes) of this layout. pub size: usize, @@ -16,6 +16,13 @@ pub struct Layout { pub packed: bool, } +#[test] +fn test_layout_for_size() { + let ptr_size = mem::size_of::<*mut ()>(); + assert_eq!(Layout::for_size(ptr_size), Layout::new(ptr_size, ptr_size)); + assert_eq!(Layout::for_size(3 * ptr_size), Layout::new(3 * ptr_size, ptr_size)); +} + impl Layout { /// Construct a new `Layout` with the given `size` and `align`. It is not /// packed. @@ -27,6 +34,20 @@ impl Layout { } } + /// Creates a non-packed layout for a given size, trying to use the maximum + /// alignment possible. + pub fn for_size(size: usize) -> Self { + let mut next_align = 2; + while size % next_align == 0 && next_align <= 2 * mem::size_of::<*mut ()>() { + next_align *= 2; + } + Layout { + size: size, + align: next_align / 2, + packed: false, + } + } + /// Is this a zero-sized layout? pub fn is_zero(&self) -> bool { self.size == 0 && self.align == 0 |