pub struct File { /* private fields */ }
Expand description
A reference to an open file on the host system.
Depending on the options that it was opened with, files can be read and/or written.
Files are auromatically closed when they are dropped. If an error occurs while dropping, the error is silently swallowed.
Use close
if these errors should be explicitly handled.
Because all semihosting operations are very slow, buffering reads and writes might be beneficial.
As the semihosting operations don’t return a specific error when they fail, most methods just return a Result<_, ()>
.
Implementations§
Source§impl File
impl File
Sourcepub fn open(path: &CStr, mode: FileOpenMode) -> Result<File, ()>
pub fn open(path: &CStr, mode: FileOpenMode) -> Result<File, ()>
Opens the file with the given mode.
Sourcepub fn write(&mut self, buf: &[u8]) -> Result<usize, ()>
pub fn write(&mut self, buf: &[u8]) -> Result<usize, ()>
Tries to write all of buffers bytes into the file, and returns the number of bytes that were actually written.
Sourcepub fn close(self) -> Result<(), File>
pub fn close(self) -> Result<(), File>
Closes the file.
The file is also closed when it the file is dropped. However this method is more explicit and allows to check if an error happenend while closing the file.
If an error occured, the unclosed file is returned.
Sourcepub fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()>
pub fn read(&mut self, buf: &mut [u8]) -> Result<usize, ()>
Tries to read buf.len()
bytes from the file and returns the number of bytes that was read into the buffer.
The result Ok(0usize)
suggests that EOF has been reached.
Sourcepub fn seek(&mut self, from: SeekFrom) -> Result<(), SeekError>
pub fn seek(&mut self, from: SeekFrom) -> Result<(), SeekError>
Sets the read/write-cursor to the specified position This actually consists of two semihosting operations: One to get the length, and another to set the cursor
If you want to set the cursor to the beginning of the file, use rewind
instead.
see also: seek_unchecked