Skip to content

Instantly share code, notes, and snippets.

@kylewlacy
Last active May 27, 2025 04:05
Show Gist options
  • Save kylewlacy/be1a419d20a67127eec6ddd078ce6b01 to your computer and use it in GitHub Desktop.
Save kylewlacy/be1a419d20a67127eec6ddd078ce6b01 to your computer and use it in GitHub Desktop.
facet - size_of in const context
[package]
name = "facet-size-test"
version = "0.1.0"
edition = "2024"
[dependencies]
facet = { version = "0.27.6", default-features = false }
// #![no_std]
#[derive(facet::Facet)]
struct Foobar {
foo: u32,
bar: u64,
}
const fn size_of<'a, T: facet::Facet<'a>>() -> usize {
let facet::ShapeLayout::Sized(layout) = T::SHAPE.layout else {
panic!("type is unsized");
};
layout.size()
}
fn main() {
let buf: [u8; size_of::<Foobar>()] = [0; size_of::<Foobar>()];
println!("buf is {} bytes", buf.len());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment