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 IoErrorwhy a file operation failedOpen-- the source is missing, refused (permissions) or no fileCreate-- the target (its sibling) could not be created: refused, a folder, no such folderReadWriteCommit-- the finished file could not be put in placeCancelled-- the CancelToken was cancelled (or the future dropped) before the end
struct Ctlprogress 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 chunkchunk: usize-- bytes per chunk (0: CHUNK)
impl Ctl
fn new() -> Ctla fresh Progress and CancelToken, CHUNK bytes per chunkfn read_file(path: &str, ctl: &Ctl) -> impl Future<Output = Result<Vec<u8>, IoError>> + use<>the contents of a file, read in chunks on the poolfn 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 placefn 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 copiedstruct Readera 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) -> u64the size when it was openedfn pos(&self) -> u64where the next read startsfn seek(&mut self, pos: u64)the next read starts at posasync 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 Writeran 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) -> u64bytes written so farasync 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)