Last active
January 16, 2023 12:25
-
-
Save tapthaker/1eb004acefa26ccbd7d20aa4f789492e to your computer and use it in GitHub Desktop.
Rust Experts Question: Symbol is getting allocated on the stack
This file contains 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
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