pub struct UniversalFile(/* private fields */);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::seekto 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::AppOpenFilesEventdifferently). UniversalFile::open_urlexists, which is not available in the regularstd::fs::FileAPI. 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::cloneto get a truly new handle, e.g. with its ownstd::io::Seekstate. 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§
Source§impl UniversalFile
impl UniversalFile
Sourcepub fn open(path: &str) -> Result<Self>
pub fn open(path: &str) -> Result<Self>
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§
Source§impl Clone for UniversalFile
impl Clone for UniversalFile
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
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.
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for UniversalFile
impl Debug for UniversalFile
Source§impl Read for UniversalFile
impl Read for UniversalFile
1.36.0 · Source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read moreSource§impl Seek for UniversalFile
impl Seek for UniversalFile
Source§fn seek(&mut self, style: SeekFrom) -> Result<u64>
fn seek(&mut self, style: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len)