From d126ecff01d63fa51a9fb25402d1300f9297b416 Mon Sep 17 00:00:00 2001 From: Haven Selph Date: Thu, 1 Aug 2024 21:06:05 -0500 Subject: [PATCH] fix: my own mistakes --- src/main.rs | 6 +++--- src/report.rs | 2 +- src/scanner.rs | 39 +++++++++++++-------------------------- 3 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/main.rs b/src/main.rs index a28c889..eca5fc2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,7 +56,7 @@ fn check_reports(receiver: &Receiver>, reports: &mut Vec) -> had_error } -fn print_reports_and_exit(reports: &mut Vec, args: &args::Args) { +fn print_reports_and_exit(reports: &mut Vec, args: &args::Args) -> ! { if *args.level == Level::Silent { exit(1); } @@ -69,7 +69,8 @@ fn print_reports_and_exit(reports: &mut Vec, args: &args::Args) { report.display(*args.code_context); } }); - exit(1); + + exit(1) } fn main() { @@ -88,7 +89,6 @@ fn main() { Scanner::get_file(*args.file), ReportSender::new(sender.clone()), ); - lexer.lex_tokens(); lexer.tokens.goto_front(); diff --git a/src/report.rs b/src/report.rs index 3d3e6a8..81d71fe 100644 --- a/src/report.rs +++ b/src/report.rs @@ -171,7 +171,7 @@ impl Display for ReportFormatter<'_> { match report.label.as_ref() { Some(label) => { let span = &label.span; - let contents = crate::Scanner::get_file(span.filename); + let contents = crate::Scanner::get(span.filename); let line_index = match contents[..=span.start_index].rfind('\n') { Some(val) => val + 1, None => 0, diff --git a/src/scanner.rs b/src/scanner.rs index 86b2390..3a5645a 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -8,7 +8,7 @@ use crate::report::{ReportKind, ReportLabel, UnwrapReport}; pub struct Scanner { filename: &'static str, index: usize, - contents: String, + contents: &'static mut String, reader: BufReader, } @@ -16,58 +16,49 @@ static CACHE: LazyLock>> = LazyLock::new(|| RwLock::new(HashMap::new())); impl Scanner { - pub fn get_cached(filename: &'static str) -> Option<&str> { - let cache = CACHE.read().expect("failed to lock on cache"); - - cache.get(&filename).copied() - } - - pub fn get_file(filename: &'static str) -> &str { - if let Some(contents) = Scanner::get_cached(filename) { + pub fn get(filename: &'static str) -> &'static str { + if let Some(contents) = CACHE.read().unwrap().get(&filename) { return contents; } - let contents = Scanner::new(filename) + let contents = Self::new(filename) .unwrap_or_fatal( ReportKind::IOError - .new(format!("Failed to open file: '{}'", filename)) + .new(format!("Failed to open file: '{filename}'")) .into(), ) .read() .unwrap_or_fatal( ReportKind::IOError - .new(format!("Failed to read file: '{}'", filename)) + .new(format!("Failed to read file: '{filename}'")) .into(), ) - .leak(); - - let mut cache = CACHE.write().expect("failed to lock on cache"); + .contents; - cache.insert(filename, contents); + CACHE.write().unwrap().insert(filename, contents); contents } fn new(filename: &'static str) -> io::Result { let file = File::open(filename)?; - let file_size = file.metadata()?.len() as usize; + + let file_size = usize::try_from(file.metadata()?.len()).unwrap(); Ok(Self { filename, index: 0, - contents: String::with_capacity(file_size), + contents: Box::leak(String::with_capacity(file_size).into()), reader: BufReader::new(file), }) } fn read(mut self) -> io::Result { - let mut buf = [0; 1]; + let mut buf = [0u8; 1]; while self.reader.read(&mut buf)? > 0 { match std::str::from_utf8(&buf) { Ok(s) => match s { - "\r" => { - continue; - } + "\r" => continue, _ => self.contents.push_str(s), }, Err(_) => { @@ -98,8 +89,4 @@ impl Scanner { Ok(self) } - - fn leak(self) -> &'static str { - Box::leak(self.contents.into_boxed_str()) - } }