MCPcopy Create free account
hub / github.com/SeaQL/FireDBG.for.Rust / probe_layout_of

Function probe_layout_of

debugger/src/debugger/probe.rs:68–100  ·  view source on GitHub ↗
(typedef: &str)

Source from the content-addressed store, hash-verified

66}
67
68fn probe_layout_of(typedef: &str) -> anyhow::Result<Layout> {
69 log::trace!("Probing {typedef} ...");
70 let mut src = "extern crate alloc;\n".to_string();
71 write!(src, "{}", typedef)?;
72 write!(
73 src,
74 r#"
75 fn main() {{
76 let layout = core::alloc::Layout::new::<T>();
77 println!("size = {{}}", layout.size());
78 println!("align = {{}}", layout.align());
79 }}
80 "#
81 )?;
82 let obj = compile_rust_program(&src).with_context(|| format!("Fail to compile: `{src}`"))?;
83 let result = execute_rust_program(&obj).with_context(|| format!("Fail to run: `{obj}`"))?;
84 let mut lines = result.lines();
85 let line = lines.next().context("has next")?;
86 let (attr, size) = line.split_once(" = ").context("attr")?;
87 assert_eq!(attr, "size");
88 let line = lines.next().context("has next")?;
89 let (attr, align) = line.split_once(" = ").context("attr")?;
90 assert_eq!(attr, "align");
91 let size: usize = size
92 .parse()
93 .with_context(|| format!("Not size: `{size}`"))?;
94 let align: usize = align
95 .parse()
96 .with_context(|| format!("Not align: `{align}`"))?;
97 let layout = Layout::from_size_align(size, align)?;
98 log::trace!("{layout:?}");
99 Ok(layout)
100}
101
102#[cfg(test)]
103mod test {

Callers 2

get_layout_ofFunction · 0.85
test_probeFunction · 0.85

Calls 5

compile_rust_programFunction · 0.85
execute_rust_programFunction · 0.85
to_stringMethod · 0.80
nextMethod · 0.80
parseMethod · 0.80

Tested by 1

test_probeFunction · 0.68