pub struct FieldLayout { /* private fields */ }Expand description
One named field’s placement within a structure or union.
Implementations§
Source§impl FieldLayout
impl FieldLayout
Sourcepub fn name(&self) -> &str
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
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}Sourcepub const fn layout(&self) -> Layout
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
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}Sourcepub const fn offset(&self) -> u64
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
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}Sourcepub const fn align(&self) -> u64
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}Sourcepub const fn padding_before(&self) -> u64
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
impl Clone for FieldLayout
Source§impl Debug for FieldLayout
impl Debug for FieldLayout
impl Eq for FieldLayout
Source§impl PartialEq for FieldLayout
impl PartialEq for FieldLayout
impl StructuralPartialEq for FieldLayout
Auto Trait Implementations§
impl Freeze for FieldLayout
impl RefUnwindSafe for FieldLayout
impl Send for FieldLayout
impl Sync for FieldLayout
impl Unpin for FieldLayout
impl UnsafeUnpin for FieldLayout
impl UnwindSafe for FieldLayout
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more