Skip to main content

FieldLayout

Struct FieldLayout 

Source
pub struct FieldLayout { /* private fields */ }
Expand description

One named field’s placement within a structure or union.

Implementations§

Source§

impl FieldLayout

Source

pub fn name(&self) -> &str

Returns the field name.

Examples found in repository?
examples/elf.rs (line 23)
12fn main() -> Result<(), LayoutError> {
13    for class in [ElfClass::Elf32, ElfClass::Elf64] {
14        let header = elf_header(class)?;
15        println!(
16            "{class:?}: size={}, align={}",
17            header.size(),
18            header.align()
19        );
20        for field in header.fields() {
21            println!(
22                "  {:<12} offset={:>2}, size={}",
23                field.name(),
24                field.offset(),
25                field.layout().size()
26            );
27        }
28    }
29    Ok(())
30}
More examples
Hide additional examples
examples/header.rs (line 20)
9fn main() -> Result<(), LayoutError> {
10    let header = StructLayout::builder()
11        .field("tag", Layout::new(1, 1)?)
12        .field("length", Layout::new(4, 4)?)
13        .field("flags", Layout::new(2, 2)?)
14        .build()?;
15
16    println!("Header: size={}, align={}", header.size(), header.align());
17    for field in header.fields() {
18        println!(
19            "  {:<8} offset={}, size={}, align={}, padding_before={}",
20            field.name(),
21            field.offset(),
22            field.layout().size(),
23            field.align(),
24            field.padding_before(),
25        );
26    }
27    println!("  tail padding={}", header.tail_padding());
28
29    for (name, target) in [
30        ("wasm32", TargetLayout::wasm32()),
31        ("x86_64", TargetLayout::x86_64()),
32    ] {
33        let pointer_record = target.struct_layout([("tag", Type::U8), ("data", Type::Pointer)])?;
34        println!(
35            "{name}: Foo size={}, align={}, data offset={}",
36            pointer_record.size(),
37            pointer_record.align(),
38            pointer_record.offset("data").expect("data field exists"),
39        );
40    }
41    Ok(())
42}
Source

pub const fn layout(&self) -> Layout

Returns the original layout, before any outer packing constraint.

Examples found in repository?
examples/elf.rs (line 25)
12fn main() -> Result<(), LayoutError> {
13    for class in [ElfClass::Elf32, ElfClass::Elf64] {
14        let header = elf_header(class)?;
15        println!(
16            "{class:?}: size={}, align={}",
17            header.size(),
18            header.align()
19        );
20        for field in header.fields() {
21            println!(
22                "  {:<12} offset={:>2}, size={}",
23                field.name(),
24                field.offset(),
25                field.layout().size()
26            );
27        }
28    }
29    Ok(())
30}
More examples
Hide additional examples
examples/header.rs (line 22)
9fn main() -> Result<(), LayoutError> {
10    let header = StructLayout::builder()
11        .field("tag", Layout::new(1, 1)?)
12        .field("length", Layout::new(4, 4)?)
13        .field("flags", Layout::new(2, 2)?)
14        .build()?;
15
16    println!("Header: size={}, align={}", header.size(), header.align());
17    for field in header.fields() {
18        println!(
19            "  {:<8} offset={}, size={}, align={}, padding_before={}",
20            field.name(),
21            field.offset(),
22            field.layout().size(),
23            field.align(),
24            field.padding_before(),
25        );
26    }
27    println!("  tail padding={}", header.tail_padding());
28
29    for (name, target) in [
30        ("wasm32", TargetLayout::wasm32()),
31        ("x86_64", TargetLayout::x86_64()),
32    ] {
33        let pointer_record = target.struct_layout([("tag", Type::U8), ("data", Type::Pointer)])?;
34        println!(
35            "{name}: Foo size={}, align={}, data offset={}",
36            pointer_record.size(),
37            pointer_record.align(),
38            pointer_record.offset("data").expect("data field exists"),
39        );
40    }
41    Ok(())
42}
Source

pub const fn offset(&self) -> u64

Returns the field’s byte offset from its immediate aggregate’s start. Union fields always have offset zero.

Examples found in repository?
examples/elf.rs (line 24)
12fn main() -> Result<(), LayoutError> {
13    for class in [ElfClass::Elf32, ElfClass::Elf64] {
14        let header = elf_header(class)?;
15        println!(
16            "{class:?}: size={}, align={}",
17            header.size(),
18            header.align()
19        );
20        for field in header.fields() {
21            println!(
22                "  {:<12} offset={:>2}, size={}",
23                field.name(),
24                field.offset(),
25                field.layout().size()
26            );
27        }
28    }
29    Ok(())
30}
More examples
Hide additional examples
examples/header.rs (line 21)
9fn main() -> Result<(), LayoutError> {
10    let header = StructLayout::builder()
11        .field("tag", Layout::new(1, 1)?)
12        .field("length", Layout::new(4, 4)?)
13        .field("flags", Layout::new(2, 2)?)
14        .build()?;
15
16    println!("Header: size={}, align={}", header.size(), header.align());
17    for field in header.fields() {
18        println!(
19            "  {:<8} offset={}, size={}, align={}, padding_before={}",
20            field.name(),
21            field.offset(),
22            field.layout().size(),
23            field.align(),
24            field.padding_before(),
25        );
26    }
27    println!("  tail padding={}", header.tail_padding());
28
29    for (name, target) in [
30        ("wasm32", TargetLayout::wasm32()),
31        ("x86_64", TargetLayout::x86_64()),
32    ] {
33        let pointer_record = target.struct_layout([("tag", Type::U8), ("data", Type::Pointer)])?;
34        println!(
35            "{name}: Foo size={}, align={}, data offset={}",
36            pointer_record.size(),
37            pointer_record.align(),
38            pointer_record.offset("data").expect("data field exists"),
39        );
40    }
41    Ok(())
42}
Source

pub const fn align(&self) -> u64

Returns the alignment used for placement, after applying packing.

Examples found in repository?
examples/header.rs (line 23)
9fn main() -> Result<(), LayoutError> {
10    let header = StructLayout::builder()
11        .field("tag", Layout::new(1, 1)?)
12        .field("length", Layout::new(4, 4)?)
13        .field("flags", Layout::new(2, 2)?)
14        .build()?;
15
16    println!("Header: size={}, align={}", header.size(), header.align());
17    for field in header.fields() {
18        println!(
19            "  {:<8} offset={}, size={}, align={}, padding_before={}",
20            field.name(),
21            field.offset(),
22            field.layout().size(),
23            field.align(),
24            field.padding_before(),
25        );
26    }
27    println!("  tail padding={}", header.tail_padding());
28
29    for (name, target) in [
30        ("wasm32", TargetLayout::wasm32()),
31        ("x86_64", TargetLayout::x86_64()),
32    ] {
33        let pointer_record = target.struct_layout([("tag", Type::U8), ("data", Type::Pointer)])?;
34        println!(
35            "{name}: Foo size={}, align={}, data offset={}",
36            pointer_record.size(),
37            pointer_record.align(),
38            pointer_record.offset("data").expect("data field exists"),
39        );
40    }
41    Ok(())
42}
Source

pub const fn padding_before(&self) -> u64

Returns padding between the previous structure field’s end and this field. Union fields always report zero.

Examples found in repository?
examples/header.rs (line 24)
9fn main() -> Result<(), LayoutError> {
10    let header = StructLayout::builder()
11        .field("tag", Layout::new(1, 1)?)
12        .field("length", Layout::new(4, 4)?)
13        .field("flags", Layout::new(2, 2)?)
14        .build()?;
15
16    println!("Header: size={}, align={}", header.size(), header.align());
17    for field in header.fields() {
18        println!(
19            "  {:<8} offset={}, size={}, align={}, padding_before={}",
20            field.name(),
21            field.offset(),
22            field.layout().size(),
23            field.align(),
24            field.padding_before(),
25        );
26    }
27    println!("  tail padding={}", header.tail_padding());
28
29    for (name, target) in [
30        ("wasm32", TargetLayout::wasm32()),
31        ("x86_64", TargetLayout::x86_64()),
32    ] {
33        let pointer_record = target.struct_layout([("tag", Type::U8), ("data", Type::Pointer)])?;
34        println!(
35            "{name}: Foo size={}, align={}, data offset={}",
36            pointer_record.size(),
37            pointer_record.align(),
38            pointer_record.offset("data").expect("data field exists"),
39        );
40    }
41    Ok(())
42}

Trait Implementations§

Source§

impl Clone for FieldLayout

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FieldLayout

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for FieldLayout

Source§

impl PartialEq for FieldLayout

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for FieldLayout

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.