Skip to main content

Crate isit

Crate isit 

Source
Expand description

isit is a compile time checking library. It has a multitude of functions for performing compile time checks to ensure that your program will work as expected before you ever run it.

§Examples

§const_assert

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::const_assert(std::mem::offset_of!(Fred, bar) == 8);

§const_assert_all

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::const_assert_all([
    std::mem::size_of::<Fred>() == 24,
    std::mem::offset_of!(Fred, foo) == 0,
    std::mem::offset_of!(Fred, bar) == 8,
    std::mem::offset_of!(Fred, baz) == 16,
]);

§const_assert_any

#[repr(C)]
struct Fred {
    foo: u32,
    bar: usize,
    baz: bool,
}
const _: () = isit::const_assert_any([
    // 64-bit systems
    std::mem::size_of::<Fred>() == 24,
    // 32-bit systems
    std::mem::size_of::<Fred>() == 12,
    // 16-bit systems
    std::mem::size_of::<Fred>() == 8,
]);

§const_assert_none

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::const_assert_none([
    // align is 8, so none of these will be true.
    std::mem::align_of::<Fred>() == 1,
    std::mem::align_of::<Fred>() == 2,
    std::mem::align_of::<Fred>() == 4,
]);

§assert_size

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_size::<Fred, 24>();

§assert_not_size

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
// If the type were not `C` layout, and were instead `Rust`
// layout, the size would end up being 16 instead of 24, and
// this check would fail.
const _: () = isit::assert_not_size::<Fred, 16>();

§assert_same_size

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_same_size::<Fred, [u64; 3]>();

§assert_different_size

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
/// 
 
#[repr(C)]
struct NotFred {
    bar: u64,
    foo: u32,
    baz: bool,
}
const _: () = isit::assert_different_size::<Fred, NotFred>();

§assert_compatible_size

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_compatible_size::<[u64; 1], Fred>();
const _: () = isit::assert_compatible_size::<[u64; 2], Fred>();
const _: () = isit::assert_compatible_size::<[u64; 3], Fred>();

§assert_incompatible_size

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_incompatible_size::<Fred, [u64; 2]>();

§assert_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_align::<Fred, 8>();

§assert_not_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_not_align::<Fred, 4>();
const _: () = isit::assert_not_align::<Fred, 16>();

§assert_same_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_same_align::<Fred, u64>();

§assert_different_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_different_align::<Fred, u32>();
const _: () = isit::assert_different_align::<Fred, bool>();

§assert_compatible_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_compatible_align::<Fred, u64>();

§assert_incompatible_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_incompatible_align::<Fred, u32>();
const _: () = isit::assert_incompatible_align::<Fred, bool>();

§assert_size_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_size_align::<Fred, 24, 8>();

§assert_not_size_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_not_size_align::<Fred, 16, 4>();

§assert_same_size_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_same_size_align::<Fred, [u64; 3]>();

§assert_different_size_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_different_size_align::<Fred, [u32; 4]>();

§assert_compatible_size_align

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_compatible_size_align::<[u32; 3], Fred>();

§assert_incompatible_size_algin

#[repr(C)]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_incompatible_size_align::<Fred, u32>();

§assert_pointer_size

use std::ptr::NonNull;
#[repr(transparent)]
struct Foo {
    ptr: NonNull<u8>,
}
const _: () = isit::assert_pointer_size::<Foo>();

§assert_not_pointer_size

#[repr(C)]
struct Foo {
    a: bool,
    b: u16,
    c: bool,
    d: u16,
    e: bool,
}
const _: () = isit::assert_not_pointer_size::<Foo>();

§assert_pointer_align

#[cfg_attr(target_pointer_width = "64", repr(C, align(8)))]
#[cfg_attr(target_pointer_width = "32", repr(C, align(4)))]
#[cfg_attr(target_pointer_width = "16", repr(C, align(2)))]
struct Foo(usize);
const _: () = isit::assert_pointer_align::<Foo>();

§assert_not_pointer_align

#[repr(transparent)]
struct Foo {
    inner: u8,
}
const _: () = isit::assert_not_pointer_align::<Foo>();

§assert_pointer_size_align

use std::ptr::NonNull;
/// 
 
#[repr(transparent)]
struct Foo(NonNull<Foo>);
const _: () = isit::assert_pointer_size_align::<Foo>();

§assert_not_pointer_size_align

#[repr(transparent)]
struct Foo(u8);
const _: () = isit::assert_not_pointer_size_align::<Foo>();

§assert_pointer_compatible_size_align

#[repr(transparent)]
struct Foo(u8);
const _: () = isit::assert_pointer_compatible_size_align::<Foo>();

§assert_pointer_incompatible_size_align

#[repr(C, align(16))]
struct Fred {
    foo: u32,
    bar: u64,
    baz: bool,
}
const _: () = isit::assert_pointer_incompatible_size_align::<Fred>();

§assert_t_niche

use std::ptr::NonNull;
/// 
 
#[repr(transparent)]
struct Foo {
    ptr: NonNull<Foo>,
}
const _: () = isit::assert_t_niche::<Foo, NonNull<Foo>>();

§assert_no_t_niche

#[repr(transparent)]
struct Foo {
    foo: u32,
}
const _: () = isit::assert_no_t_niche::<Foo, u32>();

§assert_t_niche_compatible

use std::num::NonZero;
#[repr(transparent)]
struct Foo(NonZero<u8>);
const _: () = isit::assert_t_niche_compatible::<Foo, NonZero<u8>>();
const _: () = isit::assert_t_niche_compatible::<Foo, NonZero<u16>>();

§assert_niche

use std::ptr::NonNull;
/// 
 
#[repr(transparent)]
struct Foo(NonNull<Foo>);
const _: () = isit::assert_niche::<Foo>();

§assert_no_niche

#[repr(transparent)]
struct Foo(u64);
const _: () = isit::assert_no_niche::<Foo>();

§assert_single_niche

use std::ptr::NonNull;
/// 
 
#[repr(transparent)]
struct Foo(NonNull<Foo>);
const _: () = isit::assert_single_niche::<Foo>();

§assert_pointer_niche

use std::ptr::NonNull;
/// 
 
#[repr(transparent)]
struct Foo(NonNull<Foo>);
const _: () = isit::assert_pointer_niche::<Foo>();

§assert_no_pointer_niche

#[repr(transparent)]
struct Foo(usize);
const _: () = isit::assert_no_pointer_niche::<Foo>();

§assert_pointer_niche_compatible

use std::num::NonZero;
#[repr(transparent)]
struct Foo(NonZero<u128>);
const _: () = isit::assert_pointer_niche_compatible::<Foo>();

§assert_u8_niche

#[repr(u8)]
enum Fred {
    Foo,
    Bar,
    Baz,
}
const _: () = isit::assert_u8_niche::<Fred>();

§assert_u8_size_align

#[repr(transparent)]
struct Foo(u8);
const _: () = isit::assert_u8_size_align::<Foo>();

§assert_u8_compatible

struct Zst;
#[repr(transparent)]
struct U8(u8);
const _: () = isit::assert_u8_compatible::<Zst>();
const _: () = isit::assert_u8_compatible::<U8>();

§assert_u8_incompatible

#[repr(transparent)]
struct Foo(u16);
const _: () = isit::assert_u8_incompatible::<Foo>();

§assert_zst

struct Zst;
const _: () = isit::assert_zst::<Zst>();
const _: () = isit::assert_zst::<()>();
const _: () = isit::assert_zst::<[[u64; u16::MAX as usize]; 0]>();

§assert_not_zst

struct NonZst([u64; u16::MAX as usize]);
const _: () = isit::assert_not_zst::<NonZst>();

§assert_uninhabited_zst

enum Uninhabited {}
struct Holder<T>(T);
const _: () = isit::assert_uninhabited_zst::<Uninhabited>();
const _: () = isit::assert_uninhabited_zst::<(Uninhabited, Uninhabited)>();
const _: () = isit::assert_uninhabited_zst::<[Uninhabited; usize::MAX]>();
const _: () = isit::assert_uninhabited_zst::<Holder<Uninhabited>>();
const _: () = isit::assert_uninhabited_zst::<Holder<[Uninhabited; usize::MAX]>>();
const _: () = isit::assert_uninhabited_zst::<Holder<[[Uninhabited; usize::MAX]; usize::MAX]>>();
const _: () = isit::assert_uninhabited_zst::<Holder<[[[Uninhabited; usize::MAX]; usize::MAX]; usize::MAX]>>();
const _: () = isit::assert_uninhabited_zst::<Holder<(
    Holder<[[[Uninhabited; usize::MAX]; usize::MAX]; usize::MAX]>,
    Holder<[[[Uninhabited; usize::MAX]; usize::MAX]; usize::MAX]>,
)>>();

§assert_not_uninhabited_zst

struct Foo(u8);
const _: () = isit::assert_not_uninhabited_zst::<Foo>();

Functions§

align_condition
Check that type T has alignment of ALIGN at compile time.
all_condition
Check that all conditions are true at compile time.
any_condition
Check that any condition is true at compile time.
assert_align
Assert that type T has alignment of ALIGN at compile time.
assert_compatible_align
Assert that type Min has an alignment that is less than or equal to the alignment of type Max at compile time.
assert_compatible_size
Assert that type Min has a size that is less than or equal to the size of Max at compile time.
assert_compatible_size_align
Assert that the size and alignment of type Min is less than or equal to the size and alignment of type Max at compile time.
assert_different_align
Assert that type L has a different alignment than type R at compile time.
assert_different_size
Assert that type L and type R have different sizes at compile time.
assert_different_size_align
Assert that type L has a different size and alignment from type R at compile time.
assert_incompatible_align
Assert that type Max has an alignment that is greater than the alignment of type Min at compile time.
assert_incompatible_size
Assert that type Max has a size that is greater than the size of type Min at compile time.
assert_incompatible_size_align
Assert that size and alignment of type Max is greater than size and alignment of type Min at compile time.
assert_niche
Assert that type T has the same size and alignment as Option<T> at compile time.
assert_no_niche
Assert that type T has a different size from Option<T> at compile time.
assert_no_pointer_niche
Assert that Option<T> has a size greater than a pointer at compile time.
assert_no_t_niche
Check that Option<Niched> has a greater size than type T at compile time.
assert_not_align
Assert that type T does not have alignment of ALIGN at compile time.
assert_not_pointer_align
Assert that type T is does not have the same alignment as a pointer at compile time.
assert_not_pointer_size
Assert that type T is not the same size as a pointer at compile time.
assert_not_pointer_size_align
Assert that type T has a different size and alignment from a pointer at compile time.
assert_not_size
Assert that the size of T does not match SIZE at compile time.
assert_not_size_align
Assert that type T has either a different size than SIZE and a different alignment than ALIGN at compile time.
assert_not_uninhabited_zst
Assert that type T is not an uninhabited ZST (zero-sized type) at compile time.
assert_not_zst
Assert that type T is not a ZST (zero-sized type) at compile time.
assert_pointer_align
Assert that type T has the same alignment as a pointer at compile time.
assert_pointer_compatible_size_align
Assert that type T has a size and alignment that is less than or equal to the size and alignment of a pointer at compile time.
assert_pointer_incompatible_size_align
Assert that the size or alignment of type T is greater than the size or alignment of a pointer at compile time.
assert_pointer_niche
Assert that Option<T> has the same size and alignment as a pointer at compile time.
assert_pointer_niche_compatible
Assert that the size and alignment of Option<T> is greater or equal to the size and alignment of a pointer at compile time.
assert_pointer_size
Assert that type T has the same size as a pointer at compile time.
assert_pointer_size_align
Assert that type T has the same size and alignment as a pointer at compile time.
assert_same_align
Assert that type L has the same alignment as type R at compile time.
assert_same_size
Assert that type L is the same size as type R at compile time.
assert_same_size_align
Check that type L has the same size and alignment as type R at compile time.
assert_single_niche
Assert that type T has the same size and alignment as Option<T>, but has a different size than Option<Option<T>> at compile time.
assert_size
Assert that the size of T matches SIZE at compile time.
assert_size_align
Assert that type T has the size of SIZE and the alignment of ALIGN at compile time.
assert_t_niche
Assert that type T has the same size and alignment as Option<Niched> at compile time.
assert_t_niche_compatible
Assert that the size and alignment of T is less than or equal to the size and alignment of Option<Niched> at compile time.
assert_u8_compatible
Assert that type T has a size and alignment that is less or equal to the size and alignment of u8 at compile time.
assert_u8_incompatible
Assert that the size or alignment of T is greater than the size or alignment of u8 at compile time.
assert_u8_niche
Assert that the size of Option<T> is equal to the size of u8 at compile time.
assert_u8_size_align
Assert that type T has the same size and alignment as a u8 at compile time.
assert_uninhabited_zst
Assert that type T is an uninhabited ZST (zero-sized type) at compile time.
assert_zst
Assert that type T is a ZST (zero-sized type) at compile time.
compatible_align_condition
Check that type Min has an alignment that is less than or equal to the alignment of type Max at compile time.
compatible_size_align_condition
Check that the size and alignment of type Min is less than or equal to the size and alignment of type Max at compile time.
compatible_size_condition
Check that type Min has a size that is less than or equal to the size of Max at compile time.
const_assert
Assert that a condition is true at compile time.
const_assert_all
Assert that all conditions are true at compile time.
const_assert_any
Assert that any condition is true at compile time.
const_assert_none
Assert that none of the conditions are true at compile time.
different_align_condition
Check that type L has a different alignment than type R at compile time.
different_size_align_condition
Check that type L has a different size and alignment from type R at compile time.
different_size_condition
Check that type L and type R have different sizes at compile time.
incompatible_align_condition
Check that type Max has an alignment that is greater than the alignment of type Min at compile time.
incompatible_size_align_condition
Check that size and alignment of type Max is greater than size and alignment of type Min at compile time.
incompatible_size_condition
Check that type Max has a size that is greater than the size of type Min at compile time.
niche_condition
Check that type T has the same size and alignment as Option<T> at compile time.
no_niche_condition
Check that type T has a different size from Option<T> at compile time.
no_pointer_niche_condition
Check that Option<T> has a size greater than a pointer at compile time.
no_t_niche_condition
Check that Option<Niched> has a greater size than type T at compile time.
none_condition
Check that none of the conditions are true at compile time.
not_align_condition
Check that type T does not have the alignment of ALIGN at compile time.
not_pointer_align_condition
Check that type T is does not have the same alignment as a pointer at compile time.
not_pointer_size_align_condition
Check that type T has a different size and alignment from a pointer at compile time.
not_pointer_size_condition
Check that type T is not the same size as a pointer at compile time.
not_size_align_condition
Check that type T has either a different size than SIZE and a different alignment than ALIGN at compile time.
not_size_condition
Check that the size of T does not match SIZE at compile time.
not_uninhabited_zst_condition
Check that type T is not an uninhabited ZST (zero-sized type) at compile time.
not_zst_condition
Check that type T is not a ZST (zero-sized type) at compile time.
pointer_align_condition
Check that type T has the same alignment as a pointer at compile time.
pointer_compatible_size_align_condition
Check that type T has a size and alignment that is less than or equal to the size and alignment of a pointer at compile time.
pointer_incompatible_size_align_condition
Check that the size or alignment of type T is greater than the size or alignment of a pointer at compile time.
pointer_niche_compatible_condition
Check that the size and alignment of Option<T> is less or equal to the size and alignment of a pointer at compile time.
pointer_niche_condition
Check that Option<T> has the same size and alignment as a pointer at compile time.
pointer_size_align_condition
Check that type T has the same size and alignment as a pointer at compile time.
pointer_size_condition
Check that type T has the same size as a pointer at compile time.
same_align_condition
Check that type L has the same alignment as type R at compile time.
same_size_align_condition
Check that type L has the same size and alignment as type R at compile time.
same_size_condition
Check that type L is the same size as type R at compile time.
single_niche_condition
Check that type T has the same size and alignment as Option<T>, but has a different size than Option<Option<T>> at compile time.
size_align_condition
Check that type T has the size of SIZE and the alignment of ALIGN at compile time.
size_condition
Check that the size of T matches SIZE at compile time.
t_niche_compatible_condition
Check that the size and alignment of T is less than or equal to the size and alignment of Option<Niched> at compile time.
t_niche_condition
Checks that type T has the same size and alignment as Option<Niched> at compile time.
u8_compatible_condition
Check that type T has a size and alignment that is less or equal to the size and alignment of u8 at compile time.
u8_incompatible_condition
Check that the size or alignment of T is greater than the size or alignment of u8 at compile time.
u8_niche_condition
Check that the size of Option<T> is equal to the size of u8 at compile time.
u8_size_align_condition
Check that type T has the same size and alignment as a u8 at compile time.
uninhabited_zst_condition
Check that type T is an uninhabited ZST (zero-sized type) at compile time.
zst_condition
Check that type T is a ZST (zero-sized type) at compile time.