Skip to main content

Crate remotefs_memory

Crate remotefs_memory 

Source
Expand description

§remotefs-memory

remotefs-memory is a remotefs client implementation backed entirely by an in-memory tree, useful for tests and simulations that need a RemoteFs without a real server.

It exposes a single type, MemoryFs, which implements the RemoteFs trait and can therefore be used interchangeably with any other remotefs client.

§Getting Started

Add remotefs-memory to your Cargo.toml:

remotefs = "1"
remotefs-memory = "1"

§Example

use std::path::{Path, PathBuf};

use remotefs_memory::{Inode, MemoryFs, node, Node, Tree};
use remotefs::RemoteFs;
use remotefs::fs::UnixPex;

let tempdir = PathBuf::from("/tmp");
let tree = Tree::new(node!(
    PathBuf::from("/"),
    Inode::dir(0, 0, UnixPex::from(0o755)),
    node!(tempdir.clone(), Inode::dir(0, 0, UnixPex::from(0o755)))
));

let mut client = MemoryFs::new(tree);
client.connect().unwrap();
// Every path is absolute; there is no working directory.
assert!(client.stat(Path::new("/tmp")).unwrap().is_dir());

§Transfers

open, create, and append return owned streams. A write stream stages bytes and commits them into the tree only when finish is called; dropping it discards the staged bytes and logs at debug level.

use std::io::Write;
use std::path::{Path, PathBuf};

use remotefs::RemoteFs;
use remotefs::fs::{ReadOptions, UnixPex, WriteOptions};
use remotefs_memory::{Inode, MemoryFs, node, Node, Tree};

let tree = Tree::new(node!(
    PathBuf::from("/"),
    Inode::dir(0, 0, UnixPex::from(0o755))
));
let mut client = MemoryFs::new(tree);
client.connect().unwrap();

let mut stream = client
    .create(Path::new("/hello.txt"), &WriteOptions::default())
    .unwrap();
stream.write_all(b"hello").unwrap();
stream.finish().unwrap();

let mut output = Vec::new();
client
    .read_file(Path::new("/hello.txt"), &ReadOptions::default(), &mut output)
    .unwrap();
assert_eq!(output, b"hello");

§Async consumers

MemoryFs is a native blocking client. Wrap it in remotefs::adapters::r#async::Unblock (remotefs tokio feature) to use it from an AsyncRemoteFs consumer.

Macros§

node
Create a new Node using a macro

Structs§

Inode
Inode is the data stored in each node of the filesystem.
MemoryFs
MemoryFs is a simple in-memory filesystem that can be used for testing purposes.
Node
Describes a node inside the Tree U: is the type for the node indentifier (must implement PartialEq) T: is the type for the node value
Tree
represent the tree data structure inside the component. U: is the type for the Node indentifier (must implement PartialEq) T: is the type for the Node value

Type Aliases§

FsTree
Alias for the filesystem tree. It is a Tree of PathBuf and Inode.