#padding #size #calculate #proc-macro #abc #abc3 #abc2

size-of-no-padding

A procedural macro to calculate the size of a type without padding

2 unstable releases

new 0.2.0 Mar 23, 2025
0.1.5 Mar 15, 2025

#2558 in Procedural macros

Download history 111/week @ 2025-03-12

111 downloads per month
Used in cramfs

MIT/Apache

6KB
80 lines

Size Of No Padding

This crate provides the derive proc macro SizeOfNoPadding, it will generate size_of_no_padding() method for your struct or union which can calcuate the size of struct or union without padding even if it is not marked as #[repr(packed)].

Example

use size_of_no_padding::SizeOfNoPadding;

#[derive(SizeOfNoPadding)]
struct Abc {
    a: u8,
    b: u32,
    c: u8,
}

fn main() {
    assert_eq!(8, std::mem::size_of::<Abc>());
    assert_eq!(6, Abc::size_of_no_padding());
}

License

Licensed under either of

at your option.


lib.rs:

Size Of No Padding

This crate provides two derive proc-macro.

SizeOfNoPadding

Only support struct can be marked as Copy

Create a shadow struct which as same as original struct except it's marked #[repr(packed)]. call size_of_no_padding() method on struct will invoke std::mem::size_of() function on shadow one in compile time.

Example

use size_of_no_padding::SizeOfNoPadding;
use size_of_no_padding::SizeOf;

#[derive(SizeOfNoPadding)]
struct Abc {
    a: u8,
    b: u32,
    c: u8,
}

assert_eq!(8, std::mem::size_of::<Abc>());
assert_eq!(6, Abc::size_of_no_padding());

SizeOfNoPaddingAny

Support struct whose non-Copy fields impl SizeOfAny trait

Calculate the size for every field, add up all the size. call size_of_no_padding(&self) will give the size of whole struct without padding in runtime.

Example

use size_of_no_padding::SizeOfNoPaddingAny;
use size_of_no_padding::SizeOfAny;

#[derive(SizeOfNoPaddingAny)]
struct Abc2 {
    a: u8,
    b: u32,
    c: u8,
    d: Vec<u16>,
    e: Abc3,
}

#[derive(SizeOfNoPaddingAny)]
struct Abc3([u32; 4], u16);

let abc = Abc2 {
    a: 1,
    b: 2,
    c: 3,
    d: vec![4, 5, 6],
    e: Abc3([7, 8, 9, 10], 11),
};

assert_eq!(std::mem::size_of::<Abc2>(), 56);
assert_eq!(abc.size_of_no_padding_any(), 30);

License

Licensed under either of

at your option.

Dependencies

~190–620KB
~15K SLoC