pakr-assert-size

Crates.iopakr-assert-size
lib.rspakr-assert-size
version1.0.1
sourcesrc
created_at2021-12-05 13:30:12.112936
updated_at2021-12-05 13:55:59.63747
descriptionCompile-time checks of the size of structures
homepage
repositoryhttps://github.com/p-kraszewski/pakr-assert-size
max_upload_size
id492677
size7,921
Paweł Kraszewski (p-kraszewski)

documentation

README

Examples

Success (real size matches expected):

use pakr_assert_size::*;

#[repr(C, packed)]
#[assert_size(16)]
struct A {
    field1: u64,
    field2: u64,
}

#[assert_size(24)]
#[repr(C, packed)]
struct B {
    field1: u64,
    field2: u64,
    field3: u64,
}

Failure (real size is 24 bytes, expected is 32 bytes):

use pakr_assert_size::*;

#[assert_size(32)]
#[repr(C, packed)]
struct C {
    field1: u64,
    field2: u64,
    field3: u64,
}

Examples assert_size_fits

Success (real size fits in expected size):

use pakr_assert_size::*;

// Exact match
#[repr(C, packed)]
#[assert_size_fits(16)]
struct A {
    field1: u64,
    field2: u64,
}

// Fits in match
#[assert_size_fits(32)]
#[repr(C, packed)]
struct B {
    field1: u64,
    field2: u64,
    field3: u64,
}

Failure (real size is 24 bytes, exceeding maximum of 16 bytes):

use pakr_assert_size::*;

#[assert_size_fits(16)]
#[repr(C, packed)]
struct C {
    field1: u64,
    field2: u64,
    field3: u64,
}
Commit count: 2

cargo fmt