Skip to main content

Scratch

Trait Scratch 

Source
pub trait Scratch {
    type Error: Debug;

    // Required methods
    fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error>;
    fn len(&self) -> u64;
    fn freeze(&mut self) -> Result<&[u8], Self::Error>;

    // Provided method
    fn is_empty(&self) -> bool { ... }
}
Expand description

An append-only staging area: build it sequentially with write, then freeze it into a readable byte view for random and sequential reads. Dropping it discards the storage.

It is the write-side sibling of Storage: no_std core owns an algorithm that needs to spill more bytes than fit in RAM and read them back, and the host injects the actual backing — typically a temp file that is memory-mapped on freeze (see plugmem-host’s FileScratch). The reference MemScratch backs tests with a plain in-RAM buffer.

§Contract

  • write appends; call it any number of times, in the order the bytes should land.
  • freeze ends the build and returns all written bytes as one contiguous slice; the borrow keeps the region readable. No write after a freeze.
  • What freeze hands back must equal the concatenation of every write.

plugmem uses it for the disk-first maintain/recover rebuild, which streams the large pools (vectors, text) through a Scratch instead of holding them in RAM — but the trait itself knows nothing of that.

Required Associated Types§

Source

type Error: Debug

Implementation-specific failure (I/O error, host callback error…).

Required Methods§

Source

fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error>

Appends bytes to the staging area. Not to be called after freeze.

Source

fn len(&self) -> u64

Bytes appended so far.

Source

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

Ends the build and borrows everything written as one contiguous slice, for random and sequential reads (a file backing typically maps itself here). The borrow keeps the region alive, which is also what forbids a later write.

Provided Methods§

Source

fn is_empty(&self) -> bool

Whether nothing has been written yet.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§