Docs.rs
  • layout-lib-0.1.1
    • layout-lib 0.1.1
    • Permalink
    • Docs.rs crate page
    • MIT
    • Links
    • Repository
    • crates.io
    • Source
    • Owners
    • hangj
    • Dependencies
      • layout-macro ^0.1.0 normal
    • Versions
    • 14.29% of the crate is documented
  • Platform
    • i686-pc-windows-msvc
    • i686-unknown-linux-gnu
    • x86_64-apple-darwin
    • x86_64-pc-windows-msvc
    • x86_64-unknown-linux-gnu
  • Feature flags
  • docs.rs
    • About docs.rs
    • Privacy policy
  • Rust
    • Rust website
    • The Book
    • Standard Library API Reference
    • Rust by Example
    • The Cargo Guide
    • Clippy Documentation

Crate layout_lib

layout_lib0.1.1

  • All Items

Sections

  • layout-lib
  • Usage
  • The offset calculation

Crate Items

  • Macros
  • Structs
  • Traits
  • Derive Macros

Crates

  • layout_lib

Crate layout_lib

Source
Expand description

§layout-lib

View the data layout of a struct.

§Usage

cargo add layout-lib
use layout_lib::Layout;

#[derive(Layout)]
struct A<T> {
    b: u8,
    c: u64,
    d: T,
}

#[repr(C)]
#[derive(Layout)]
struct B<T> {
    b: u8,
    c: u64,
    d: T,
}

fn main() {
    let layout = A::<Vec<i32>>::get_layout();
    println!("{}", layout);

    let layout = B::<Vec<i32>>::get_layout();
    println!("{}", layout);
}

The output will be something like this

example::A<alloc::vec::Vec<i32>> (size: 40, align: 8)
|  field   | offset |  size  |    type    |
| -------- | ------ | ------ | ---------- |
| c        | 0      | 8      | u64 (align: 8) |
| d        | 8      | 24     | alloc::vec::Vec<i32> (align: 8) |
| b        | 32     | 1      | u8 (align: 1) |

example::B<alloc::vec::Vec<i32>> (size: 40, align: 8)
|  field   | offset |  size  |    type    |
| -------- | ------ | ------ | ---------- |
| b        | 0      | 1      | u8 (align: 1) |
| c        | 8      | 8      | u64 (align: 8) |
| d        | 16     | 24     | alloc::vec::Vec<i32> (align: 8) |

As you can see, the first field of struct A in the layout is c, which is not the first declared field(b). That is because Rust does not guarantee the order of the fields in the layout be the same as the order in which the fields are specified in the declaration of the type. see The Default Representation

§The offset calculation

The offset of the field is simply calculated by this macro

#[macro_export]
macro_rules! offset_of_struct {
    ($struct_name: ty, $field_name: ident) => {
        {
            let p = 0 as *const $struct_name;
            unsafe {&(*p).$field_name as *const _ as usize}
        }
    };
}
let offset = offset_of_struct!(A<Vec<i32>>, b); // 32

Macros§

offset_of_struct

Structs§

Field
LayoutInfo

Traits§

Layout

Derive Macros§

Layout

Results

Settings
Help
    trait
    layout_lib::Layout
    derive macro
    layout_lib::Layout
    struct field
    layout_lib::Field::layout
    extern crate
    layout_lib
    layout-lib
    struct
    layout_lib::LayoutInfo
    trait method
    layout_lib::Layout::get_layout
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.
No results :(
Try on DuckDuckGo?

Or try looking in one of these:
  • The Rust Reference for technical details about the language.
  • Rust By Example for expository code examples.
  • The Rust Book for introductions to language features and the language itself.
  • Docs.rs for documentation of crates released on crates.io.