JFLib

plat/aio.rs#

Source: lib/plat/aio.rs

aio -- files without blocking the calling thread: the work runs on the blocking pool
(task.rs) in chunks, with progress and cancellation between the chunks           (aio.rs)

  read_file(path, &ctl) -> future of Result<Vec<u8>, IoError>
  write_file(path, data, &ctl) -> future of Result<(), IoError>   atomic like write_file:
                        AtomicFile (a sibling written in chunks, put in place by the commit);
                        cancelled or failed, the sibling is removed and the old file stays
  copy_file(src, dst, &ctl) -> future of Result<u64, IoError>     src read in chunks into an
                        AtomicFile of dst -> the bytes copied
  Reader::open(path) -> reader;  reader.read(max).await -> the next chunk ([] at the end),
                        size() / pos() / seek(pos): a plat::File (OS file or a mount's stream)
                        read chunk by chunk, each chunk a job of the pool
  Writer::create(path) -> writer;  writer.write(data).await, writer.commit().await: an
                        AtomicFile filled chunk by chunk (dropped without commit: discarded;
                        a write's future dropped before its end spoils the writer)
  Ctl { progress, cancel, chunk }   what an operation reports to (bytes done / total) and
                        listens to; clones share both

Names, routing and permissions are those of the blocking functions (guarded.rs, vfs.rs): the
same calls, made on a pool thread (a caller inside policy's trusted() stays trusted there).
A cancel is seen between chunks: the operation cleans up and ends with Err(Cancelled).
Dropping one of the futures cancels it the same way.  Where no thread can start (the browser
without shared memory) the pool's jobs run on the event loop's thread between frames.
/// the chunk of read_file / write_file / copy_file unless Ctl says otherwise
CHUNK: usize = 256 * 1024
  • enum IoError why a file operation failed

    • Open -- the source is missing, refused (permissions) or no file
    • Create -- the target (its sibling) could not be created: refused, a folder, no such folder
    • Read
    • Write
    • Commit -- the finished file could not be put in place
    • Cancelled -- the CancelToken was cancelled (or the future dropped) before the end
  • struct Ctl progress and cancellation of one operation (clones share them)

    • progress: Progress -- bytes done and the total (set when the size is known)
    • cancel: CancelToken -- cancel() stops the operation at the next chunk
    • chunk: usize -- bytes per chunk (0: CHUNK)

impl Ctl

  • fn new() -> Ctl a fresh Progress and CancelToken, CHUNK bytes per chunk

  • fn read_file(path: &str, ctl: &Ctl) -> impl Future<Output = Result<Vec<u8>, IoError>> + use<> the contents of a file, read in chunks on the pool

  • fn write_file(path: &str, data: Vec<u8>, ctl: &Ctl) -> impl Future<Output = Result<(), IoError>> + use<> write data in chunks into an AtomicFile of path, then put it in place

  • fn copy_file(src: &str, dst: &str, ctl: &Ctl) -> impl Future<Output = Result<u64, IoError>> + use<> copy src to dst (atomic like write_file) in chunks -> the bytes copied

  • struct Reader a file read chunk by chunk on the pool

impl Reader

  • fn open(path: &str) -> impl Future<Output = Result<Reader, IoError>> + use<> open path for reading (on the pool)

  • fn size(&self) -> u64 the size when it was opened

  • fn pos(&self) -> u64 where the next read starts

  • fn seek(&mut self, pos: u64) the next read starts at pos

  • async fn read(&mut self, max: usize) -> Result<Vec<u8>, IoError> the next chunk of at most max bytes (empty at the end of the file)

  • struct Writer an AtomicFile filled chunk by chunk on the pool; dropped without commit, it is discarded (the sibling is removed by whoever drops the last reference: the dropping thread, or the pool when a write was still running). A write whose future was dropped before it finished may still land (its job may be running): the writer is spoilt then, later writes give Err(Write) and the commit Err(Commit)

impl Writer

  • fn create(path: &str) -> impl Future<Output = Result<Writer, IoError>> + use<> a writer of path (checked and routed like write_file; on the pool)
  • fn written(&self) -> u64 bytes written so far
  • async fn write(&mut self, data: Vec<u8>) -> Result<(), IoError> append data (on the pool)
  • async fn commit(mut self) -> Result<(), IoError> put the written file in place (on the pool)

Source: docs/API.md in the JFLib repository.