#assets #memory #packaging #dead #data #file

rbfs

Dead simple in-memory filesystem for packaging assets

8 releases

0.1.7 Dec 7, 2022
0.1.6 Dec 7, 2022

#969 in Filesystem

22 downloads per month

GPL-3.0 license

16KB
116 lines

Rust Build-time File System

Crates.io Docs.rs

RBFS is a dead simple interface for creating a virtual filesystem in memory. Once populated, probably in your build.rs, RBFS generates Rust source code which is linked into your main Rust program. During runtime, you can access any files you shoved into your RBFS code.

----------------                    --------------------
|   build.rs   | --- generates ---> |   resources.rs   |
----------------                    --------------------

Figure 1: Program can now access the files packed into RBFS in the build script by including the
resources.rs file.

It's very useful in, for example, games, where you might want to package image data, configuration files, audio, etc...

Usage

Include it in your Cargo.toml manifest. The following is a basic example.

// Create the filesystem.
let fs = FileSystem::new();

// Create a path to a virtual location in the filesystem, representing images..
let mut image_directory = fs.get_base_path();
image_directory.push("assets");
image_directory.push("imgs");

// Refering to an icon.png, which will be included via include_bytes! at preprocess time.
// If you want the file to be loaded as a string, change the true to a false to denote
// it's existing as being a binary file.
let icon = fs.add_file(PathBuf::from("icon.png"), &image_directory, false, true);

// If you want to make RBFS output a different filename than supplied, do the following:
let config = fs.add_file(PathBuf::from("matt.conf"), &PathBuf::from("movie.conf"), true, true);

// Generate the Rust code.
let code = fs.code();

let resources_rs = File::create(concat!(env!("CARGO_MANIFEST_DIR"), "/src/resources.rs")).unwrap();
writeln!(resources_rs, "{}", code).unwrap();

Authors

Made with 🫀 by Milo Banks.

Dependencies