load_into_vfs!() { /* proc-macro */ }Expand description
Embeds files from a directory into a virtual filesystem at compile time.
For general usage of the load_into_vfs! macro, please refer to the
crate-level documentation.
§Pseudo Signature
load_into_vfs!<FS: vfs::FileSystem>(path: &str, fs: &FS) -> vfs::VfsResult<FS>§Implementation Details
The load_into_vfs! macro is designed to load the contents of a directory into a virtual
filesystem at compile time.
Here’s how it works:
-
Directory Walking: During compile time, the specified directory is recursively walked. The macro collects the absolute paths of all entries within the directory, along with whether each entry is a file or a directory.
-
Trait Generation: The macro generates a private trait called
LoadIntoFileSystem, which is implemented for any type that implements thevfs::FileSystemtrait. This trait includes a single function,load_into_vfs, which is generated at compile time. -
Directory Handling: For directories, the generated code will create the corresponding directory in the VFS using:
ⓘself.create_dir(#vfs_path)?; -
File Handling: For files, the macro generates a block of code to include the file contents as bytes and write them into the VFS:
ⓘ{ static BYTES: &[u8] = ::std::include_bytes!(#real_path); let mut file = self.create_file(#vfs_path)?; file.write_all(BYTES)?; }This ensures the file’s bytes are included in the binary and written to the VFS at runtime.
-
Returning the Filesystem: After processing all entries, the macro returns the filesystem wrapped in a
vfs::VfsResult, allowing further interactions with the virtual filesystem.