Skip to content

Commit

Permalink
GFile: read/write with fa.*_buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
StatisMike committed Dec 6, 2023
1 parent 9920e2b commit 1b1ff33
Showing 1 changed file with 13 additions and 21 deletions.
34 changes: 13 additions & 21 deletions godot-core/src/engine/gfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> {
// 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(())
}
}
Expand Down Expand Up @@ -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()])
}
Expand Down Expand Up @@ -782,6 +773,7 @@ impl BufRead for GFile {
/// assert_eq!(error.get_reference_count(), 2)
/// }
/// ```
#[derive(Debug)]
pub struct NotUniqueError {
reference_count: i32,
}
Expand Down

0 comments on commit 1b1ff33

Please sign in to comment.