Struct zaplib::universal_file::UniversalFile
source · [−]pub struct UniversalFile(_);
Expand description
A file handle that abstracts over the different ways we have to deal with different kinds of files (local files, file URLs, dragged in files).
It tries to somewhat follow the std::fs::File
API, but there are some major differences:
- For the WebAssembly target, opening a file will read the entire file in memory synchronously,
over HTTP(S). This is quite different from the native behavior. This is required if we never
want
std::io::Seek::seek
to fail. TODO(JP): We should consider different behaviors here – potentially configurable by the user – such as completely disallowing seeking, or seeking into a buffer with a certain size, or even using HTTP Range Requests when available to fetch the data starting at a seek point (when not buffered already). This can somewhat mirrorstd::fs::OpenOptions
. We can take some inspiration from https://github.com/cruise-automation/webviz/blob/4dcd47d/packages/webviz-core/src/util/CachedFilelike.js and https://github.com/cruise-automation/webviz/blob/4dcd47d/packages/webviz-core/src/dataProviders/BrowserHttpReader.js. Even just an option to defer loading until the first read would be useful, so you can open a file on the main thread and pass it to another thread for processing (without having to create multiple functions for processing e.g. handles fromcrate::AppOpenFilesEvent
differently). UniversalFile::open_url
exists, which is not available in the regularstd::fs::File
API. This matches the behavior of the WebAssembly URL loading described above, but works on both WebAssembly and native targets.- You can use
std::clone::Clone::clone
to get a truly new handle, e.g. with its ownstd::io::Seek
state. Also note that it’s not atry_clone
– it will always succeed. This means that if you clone a handle to a file that doesn’t exist anymore, then you’ll get that error on the next read, not while cloning. - Currently it only supports reading, not writing. This might change in the future, but requires some thinking about how that should work in WebAssembly.
Note that you typically want to load files in a thread. Even on native targets the file system can be slow, e.g. when the user has mounted a remote file system, so you want to avoid blocking the UI thread when possible.
TODO(JP): File handles in WebAssembly ([UniversalFileInner::WasmFile
]) can’t be moved used in threads
that were spawned before the file handle became available. It would be nice to figure out some way
around this, or to prevent (at compile time) from using these file handles in older threads.
Implementations
Open a local/relative file. On the web target this will block until the entire file is loaded.
Will return an error if the file does not exist.
This is mostly intended for reading application files. User files should typically be obtained through
an crate::AppOpenFilesEvent
.
On the web target, this will load files relative to the base path, which you can override using the
Trait Implementations
Resets the cursor position, as opposed to std::fs::File
.
If the underlying file doesn’t exist anymore, you will get an error during the next read or seek call.
Performs copy-assignment from source
. Read more
Like read
, except that it reads into a slice of buffers. Read more
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
Read all bytes until EOF in this source, placing them into buf
. Read more
Read all bytes until EOF in this source, appending them to buf
. Read more
Read the exact number of bytes required to fill buf
. Read more
read_buf
)Pull some bytes from this source into the specified buffer. Read more
read_buf
)Read the exact number of bytes required to fill buf
. Read more
Creates a “by reference” adaptor for this instance of Read
. Read more
Creates an adapter which will chain this stream with another. Read more
Auto Trait Implementations
impl RefUnwindSafe for UniversalFile
impl Send for UniversalFile
impl Sync for UniversalFile
impl Unpin for UniversalFile
impl UnwindSafe for UniversalFile
Blanket Implementations
Mutably borrows from an owned value. Read more