Struct File

Source
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

Source

pub fn open(path: &CStr, mode: FileOpenMode) -> Result<File, ()>

Opens the file with the given mode.

Source

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.

Source

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.

Source

pub fn len(&self) -> Result<usize, ()>

Retrieves the total number of bytes of the file

Source

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.

Source

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

Source

pub unsafe fn seek_unchecked(&mut self, pos: usize) -> Result<(), ()>

Sets the position of the cursor.

The position is specified relative to the beginning of the file.

See also seek for a safe, and slightly more ergonomic way to set the cursor.

§Safety

The position must lie inside the extend of the file. Otherwise, the behaviour is undefined.

Source

pub fn rewind(&mut self) -> Result<(), ()>

Sets the cursor to the beginning of the file.

In comparison to seek, this method does not need to check the length of the file or the provided index.

Trait Implementations§

Source§

impl Debug for File

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for File

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Hash for File

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for File

Source§

fn eq(&self, other: &File) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for File

Source§

impl StructuralPartialEq for File

Auto Trait Implementations§

§

impl Freeze for File

§

impl RefUnwindSafe for File

§

impl Send for File

§

impl Sync for File

§

impl Unpin for File

§

impl UnwindSafe for File

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.