Skip to content

Instantly share code, notes, and snippets.

@tapthaker
Last active January 16, 2023 12:25
Show Gist options
  • Save tapthaker/1eb004acefa26ccbd7d20aa4f789492e to your computer and use it in GitHub Desktop.
Save tapthaker/1eb004acefa26ccbd7d20aa4f789492e to your computer and use it in GitHub Desktop.
Rust Experts Question: Symbol is getting allocated on the stack
struct Context<'a> {
bytes_pool: Vec<Vec<u8>>,
symbols: Vec<Symbol<'a>>,
}
#[allow(dead_code)]
struct Symbol<'a> {
name: &'a str,
is_defined: bool
}
fn parse_symbols<'a>(bytes: &'a [u8], symbols: &'a mut Vec<Symbol<'a>>) {
for i in 1..10 {
let name = std::str::from_utf8(&bytes[i*10..i*10+1]).unwrap();
let is_defined = &bytes[i*10]+1 == 1;
let sym = Symbol{name, is_defined};
symbols.push(sym);
}
}
fn main() {
let file_path_str = std::env::args().next().unwrap();
let file_path = std::path::PathBuf::from(file_path_str.clone());
let mut ctx = Box::new(Context{ symbols: Vec::new(), bytes_pool: Vec::new()});
let bytes = std::fs::read(file_path);
if let Ok(bytes) = bytes {
ctx.bytes_pool.push(bytes);
parse_symbols(ctx.bytes_pool.last().unwrap(), &mut ctx.symbols);
}
//for sym in &ctx.symbols {
//
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment