pub struct InMemoryFs { /* private fields */ }Expand description
An in-memory, clone-shared Storage backend.
Content lives behind Arc<RwLock<_>>, so cloning an InMemoryFs is cheap
and every clone sees the same files — the same relationship an Arc<StdFs>
has to the one real filesystem it names, but without needing the Arc
wrapper, since it’s built into the type. std::sync::RwLock (not a
runtime’s async lock) is deliberate: every method here runs to completion
without ever awaiting inside the critical section, so there is nothing
for an async lock to buy, and a plain std::sync primitive is the one
that’s guaranteed to exist — and to compile — on wasm32-unknown-unknown,
which has no threads and no async-runtime assumption to lean on.
Text and binary content are stored separately (a write picks one store
based on whether the bytes are valid UTF-8) so that a round-trip through
export_entries — text only — stays plain
strings, the shape a JS/WASM caller wants. Directories are tracked
explicitly (in a HashSet) rather than inferred from file paths, so an
empty directory create_dir_all created still shows up in
read_dir.
Symlinks may be added with add_symlink: reading or
getting metadata of the link resolves to the
target’s content, matching ReadStorage::metadata’s documented
“follows symlinks” contract. Resolution is a single hop, not a followed
chain — a symlink to a symlink is not resolved further — which is all the
coherence a test double needs; a real filesystem’s chain-following and
cycle detection isn’t reproduced here.
Implementations§
Source§impl InMemoryFs
impl InMemoryFs
Sourcepub fn with_files(entries: Vec<(PathBuf, String)>) -> Self
pub fn with_files(entries: Vec<(PathBuf, String)>) -> Self
A filesystem pre-populated with text files (and the directories that contain them).
Sourcepub fn load_from_entries(entries: Vec<(String, String)>) -> Self
pub fn load_from_entries(entries: Vec<(String, String)>) -> Self
Load files from (path_string, content) pairs — convenience for a
caller (JS/WASM interop) that only has strings, not PathBufs.
Sourcepub fn export_entries(&self) -> Vec<(String, String)>
pub fn export_entries(&self) -> Vec<(String, String)>
Every text file, as (path_string, content) pairs — the counterpart to
load_from_entries, for persisting a
session’s edits back out.
Sourcepub fn export_binary_entries(&self) -> Vec<(String, Vec<u8>)>
pub fn export_binary_entries(&self) -> Vec<(String, Vec<u8>)>
Every binary file, as (path_string, content_bytes) pairs.
Sourcepub fn load_binary_entries(&self, entries: Vec<(String, Vec<u8>)>)
pub fn load_binary_entries(&self, entries: Vec<(String, Vec<u8>)>)
Load binary files from (path_string, content_bytes) pairs.
Sourcepub fn list_all_files(&self) -> Vec<PathBuf>
pub fn list_all_files(&self) -> Vec<PathBuf>
Every text-file path currently stored.
Sourcepub fn clear(&self)
pub fn clear(&self)
Remove every file, directory, and symlink — resetting the filesystem to
empty without needing a fresh InMemoryFs (and its own, separately
shared, clones).
Sourcepub fn add_symlink(&self, link: &Path, target: &Path)
pub fn add_symlink(&self, link: &Path, target: &Path)
Add a symlink from link to target. Reading link (or its
metadata) resolves to target’s content;
link’s entry in its parent’s read_dir reports
FileType::SYMLINK — the un-followed type a caller needs in order to
recognize and skip it, since metadata itself only ever reports the
followed, resolved type.
Trait Implementations§
Source§impl Clone for InMemoryFs
impl Clone for InMemoryFs
Source§fn clone(&self) -> InMemoryFs
fn clone(&self) -> InMemoryFs
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for InMemoryFs
impl Debug for InMemoryFs
Source§impl Default for InMemoryFs
impl Default for InMemoryFs
Source§fn default() -> InMemoryFs
fn default() -> InMemoryFs
Source§impl ReadStorage for InMemoryFs
impl ReadStorage for InMemoryFs
Source§async fn read(&self, path: &Path) -> Result<Vec<u8>>
async fn read(&self, path: &Path) -> Result<Vec<u8>>
std::fs::read.Source§async fn read_to_string(&self, path: &Path) -> Result<String>
async fn read_to_string(&self, path: &Path) -> Result<String>
std::fs::read_to_string.Source§async fn read_dir(&self, path: &Path) -> Result<Vec<DirEntry>>
async fn read_dir(&self, path: &Path) -> Result<Vec<DirEntry>>
std::fs::read_dir, but yields a Vec since async iterators are not
yet stable.Source§impl Storage for InMemoryFs
impl Storage for InMemoryFs
Source§async fn write(&self, path: &Path, contents: &[u8]) -> Result<()>
async fn write(&self, path: &Path, contents: &[u8]) -> Result<()>
std::fs::write.Source§async fn create_dir_all(&self, path: &Path) -> Result<()>
async fn create_dir_all(&self, path: &Path) -> Result<()>
std::fs::create_dir_all.Source§async fn remove_file(&self, path: &Path) -> Result<()>
async fn remove_file(&self, path: &Path) -> Result<()>
std::fs::remove_file.Source§async fn remove_dir_all(&self, path: &Path) -> Result<()>
async fn remove_dir_all(&self, path: &Path) -> Result<()>
std::fs::remove_dir_all.Source§async fn rename(&self, from: &Path, to: &Path) -> Result<()>
async fn rename(&self, from: &Path, to: &Path) -> Result<()>
std::fs::rename.Source§fn capabilities(&self) -> Capabilities
fn capabilities(&self) -> Capabilities
Capabilities::NONE — a backend promises a guarantee only by saying so,
so an adapter that forgets to override this degrades to the most defensive
path rather than to a false promise.Source§async fn write_atomic(&self, path: &Path, contents: &[u8]) -> Result<()>
async fn write_atomic(&self, path: &Path, contents: &[u8]) -> Result<()>
path’s contents with contents atomically and durably: no
observer — concurrent reader or post-crash survivor — ever sees a splice
of old and new bytes, and once this returns the new contents outlive a
power loss. Read more