Last active
May 27, 2025 04:05
-
-
Save kylewlacy/be1a419d20a67127eec6ddd078ce6b01 to your computer and use it in GitHub Desktop.
facet - size_of in const context
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "facet-size-test" | |
version = "0.1.0" | |
edition = "2024" | |
[dependencies] | |
facet = { version = "0.27.6", default-features = false } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// #![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