From 0098c18d2527991cf2eb833cbea51d9ae0635fd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= Date: Fri, 10 May 2024 14:03:13 +0100 Subject: [PATCH] chore: made flush optional --- frontends/sdl/src/main.rs | 2 +- src/util.rs | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/frontends/sdl/src/main.rs b/frontends/sdl/src/main.rs index 8c9acdf2..71fbe702 100644 --- a/frontends/sdl/src/main.rs +++ b/frontends/sdl/src/main.rs @@ -411,7 +411,7 @@ impl Emulator { // into a *.sav file in the file system if counter % store_count == 0 && self.system.rom().has_battery() { let ram_data = self.system.rom().ram_data(); - write_file(&self.ram_path, ram_data).unwrap(); + write_file(&self.ram_path, ram_data, None).unwrap(); } // obtains an event from the SDL sub-system to be diff --git a/src/util.rs b/src/util.rs index ac82676f..9ff1883a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -32,13 +32,15 @@ pub fn read_file(path: &str) -> Result, Error> { } /// Writes the given data to the file at the given path. -pub fn write_file(path: &str, data: &[u8]) -> Result<(), Error> { +pub fn write_file(path: &str, data: &[u8], flush: Option) -> Result<(), Error> { let mut file = File::create(path) .map_err(|_| Error::CustomError(format!("Failed to create file: {}", path)))?; file.write_all(data) .map_err(|_| Error::CustomError(format!("Failed to write to file: {}", path)))?; - file.flush() - .map_err(|_| Error::CustomError(format!("Failed to flush file: {}", path)))?; + if flush.unwrap_or(true) { + file.flush() + .map_err(|_| Error::CustomError(format!("Failed to flush file: {}", path)))?; + } Ok(()) }