Skip to main content

Writer

Struct Writer 

pub struct Writer<'a> {
    flash: &'a dyn Flash,
    next: u32,
    buffer: &'a mut [u8],
    filled: usize,
    wear: WearCheck,
}
Expand description

Writer over the good blocks of an MTD device.

The writer collects data until it fills a block. It then erases the next good block, writes the data and reads it back to verify it. When a block fails to erase, write or verify, the writer marks it bad and uses the next good block. This keeps the layout that super::Reader and bootloaders read. If the next block fails too, the writer returns the error.

Call Writer::finish to write the last partial block, or Writer::finish_and_erase_rest to also erase the blocks after it. io::Write::flush only writes a full block, because a partial one would leave a gap in the data. Errors of io::Write are DeviceErrors wrapped in io::Error.

A full block is only written by the next call to write, flush, finish or finish_and_erase_rest, so that call returns its error. Call flush after each full block to get its error right away.

Fields§

§flash: &'a dyn Flash§next: u32

Physical block where the search for the next good block starts.

§buffer: &'a mut [u8]

Buffer for the data of one block.

§filled: usize

Number of bytes in buffer.

§wear: WearCheck

Implementations§

§

impl<'a> Writer<'a>

pub fn new( flash: &'a dyn Flash, buffer: &'a mut [u8], ) -> Result<Self, DeviceError>

Creates a writer that starts at the first good block.

The writer collects the data of a block in buffer, which must be exactly one block long.

§Errors

Returns DeviceError::BufferLength if buffer isn’t one block long.

pub fn starting_at( flash: &'a dyn Flash, buffer: &'a mut [u8], block: u32, ) -> Result<Self, DeviceError>

Creates a writer that starts at good block block.

block counts good blocks only. Bad blocks before it are skipped.

§Errors

Returns DeviceError::NoSpace if the flash has fewer good blocks, DeviceError::BufferLength if buffer isn’t one block long, or an error if the bad block markers can’t be read.

fn with_start( flash: &'a dyn Flash, buffer: &'a mut [u8], start: u32, ) -> Result<Self, DeviceError>

Creates a writer that starts at physical block start.

pub fn finish(self) -> Result<(), DeviceError>

Writes the pending data and consumes the writer.

A partial last page is padded with 0xFF, the value of erased flash. The remaining pages of the last block are left erased.

§Errors

Returns DeviceError::NoSpace if no good block is left, or an error if the flash fails in a way that isn’t a worn block.

pub fn finish_and_erase_rest(self) -> Result<(), DeviceError>

Writes the pending data, erases all good blocks after it and consumes the writer.

Use this to write an image to a whole partition. Otherwise the blocks after the image keep their old data. UBI, for example, scans every block of the partition and would find stale headers there. Blocks that fail to erase are marked bad.

§Errors

Returns an error if Writer::finish would fail, or if the flash fails in a way that isn’t a worn block.

fn write_pending(&mut self) -> Result<(), DeviceError>

Writes the buffer, padded to whole pages.

fn write_full_block(&mut self) -> Result<(), DeviceError>

Writes the buffer if it holds a full block.

Trait Implementations§

§

impl Debug for Writer<'_>

§

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

Formats the value using the given formatter. Read more
§

impl Drop for Writer<'_>

§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
§

impl Write for Writer<'_>

§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Writes a buffer into this writer, returning how many bytes were written. Read more
§

fn flush(&mut self) -> Result<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · Source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Writer<'a>

§

impl<'a> !Send for Writer<'a>

§

impl<'a> !Sync for Writer<'a>

§

impl<'a> !UnwindSafe for Writer<'a>

§

impl<'a> Freeze for Writer<'a>

§

impl<'a> Unpin for Writer<'a>

§

impl<'a> UnsafeUnpin for Writer<'a>

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 = !

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.