From 1b1ff3374cdbae2d6800aa9ec5f37cd23e75cc6f Mon Sep 17 00:00:00 2001 From: Michal Kosinski Date: Wed, 6 Dec 2023 20:38:05 +0100 Subject: [PATCH] GFile: read/write with fa.*_buffer --- godot-core/src/engine/gfile.rs | 34 +++++++++++++--------------------- 1 file changed, 13 insertions(+), 21 deletions(-) diff --git a/godot-core/src/engine/gfile.rs b/godot-core/src/engine/gfile.rs index 88178f032..b43a1ef55 100644 --- a/godot-core/src/engine/gfile.rs +++ b/godot-core/src/engine/gfile.rs @@ -662,33 +662,26 @@ impl Read for GFile { return Ok(0); } - let mut read_bytes = 0; - while read_bytes < bytes_to_read { - buf[read_bytes] = self.fa.get_8(); - read_bytes += 1; - self.check_error()?; - } + let gd_buffer = self.fa.get_buffer(bytes_to_read as i64); + buf[0..gd_buffer.len()].copy_from_slice(gd_buffer.as_slice()); + + self.check_error()?; - Ok(read_bytes) + Ok(gd_buffer.len()) } } impl Write for GFile { fn write(&mut self, buf: &[u8]) -> IoResult { - // TODO: Refactor to make use of `FileAccess::store_buffer()` method for higher efficiency in writing to file. - let bytes_to_write = buf.len(); - let mut bytes_written = 0; - - while bytes_written < bytes_to_write { - self.fa.store_8(buf[bytes_written]); - bytes_written += 1; - self.check_error()?; - } - Ok(bytes_written) + + self.fa.store_buffer(PackedByteArray::from(buf)); + self.check_error()?; + + Ok(buf.len()) } + // No-op, as still there is no need in `write` to keep anything in its internal buffer. fn flush(&mut self) -> IoResult<()> { - // TODO: After refactoring `write` implementation, assess if this method can stay as no-opt. Ok(()) } } @@ -739,9 +732,7 @@ impl BufRead for GFile { let gd_buffer = self.fa.get_buffer(buffer_read_size as i64); self.check_error()?; - for i in 0..gd_buffer.len() { - self.buffer[i] = gd_buffer.get(i); - } + self.buffer[0..gd_buffer.len()].copy_from_slice(gd_buffer.as_slice()); Ok(&self.buffer[0..gd_buffer.len()]) } @@ -782,6 +773,7 @@ impl BufRead for GFile { /// assert_eq!(error.get_reference_count(), 2) /// } /// ``` +#[derive(Debug)] pub struct NotUniqueError { reference_count: i32, }