#traits #loading #async #object #directory #save-load #object-file #file-loading #load-file #blanket

save-load-traits

Provides traits for saving and loading objects asynchronously to and from files and directories

4 releases

Uses new Rust 2024

0.2.2 Jul 13, 2025
0.2.1 Mar 31, 2025
0.2.0 Feb 27, 2025
0.1.0 Feb 27, 2025

#10 in #file-loading

Download history 184/week @ 2025-10-05 389/week @ 2025-10-12 345/week @ 2025-10-19 278/week @ 2025-10-26 301/week @ 2025-11-02 307/week @ 2025-11-09 268/week @ 2025-11-16 260/week @ 2025-11-23 365/week @ 2025-11-30 275/week @ 2025-12-07 292/week @ 2025-12-14 266/week @ 2025-12-21 250/week @ 2025-12-28 283/week @ 2026-01-04 99/week @ 2026-01-11 369/week @ 2026-01-18

1,030 downloads per month
Used in 128 crates (21 directly)

MIT license

280KB
6K SLoC

save-load-traits

The save-load-traits crate provides traits for saving and loading objects asynchronously to and from files and directories. It also includes a blanket implementation for loading objects from a directory using the LoadFromFile trait.

Key Features

  • SaveToFile Trait: Defines a trait for saving objects to a file asynchronously.
  • LoadFromFile Trait: Defines a trait for loading objects from a file asynchronously.
  • LoadFromDirectory Trait: Extends the functionality of loading from a directory, supporting automatic loading of files from a directory based on the LoadFromFile trait.

Traits and Implementations

SaveToFile Trait

The SaveToFile trait allows objects to be saved asynchronously to a file.

#[async_trait]
pub trait SaveToFile {
    type Error;

    async fn save_to_file(&self, filename: impl AsRef<Path> + Send) -> Result<(), Self::Error>;
}

LoadFromFile Trait

The LoadFromFile trait allows objects to be loaded asynchronously from a file.

#[async_trait]
pub trait LoadFromFile: Sized {
    type Error;

    async fn load_from_file(filename: impl AsRef<Path> + Send) -> Result<Self, Self::Error>;
}

LoadFromDirectory Trait

The LoadFromDirectory trait is designed for asynchronously loading objects from a directory. It provides a blanket implementation for any type that implements LoadFromFile.

#[async_trait]
pub trait LoadFromDirectory: Sized {
    type Error;

    async fn load_from_directory(
        dir: impl AsRef<Path> + Send,
    ) -> Result<Vec<Self>, Self::Error>;
}

Error Handling

This crate uses custom error types to handle I/O and JSON parsing errors:

error_tree! {
    pub enum SaveLoadError {
        IoError(std::io::Error),
        JsonParseError(JsonParseError),
        InvalidDirectory { dir: PathBuf },
    }
}

Usage

Save an Object to a File

To save an object to a file asynchronously, implement the SaveToFile trait for the type:

let obj = YourStruct {};
obj.save_to_file("file.json").await?;

Load an Object from a File

To load an object from a file asynchronously, implement the LoadFromFile trait for the type:

let obj = YourStruct::load_from_file("file.json").await?;

Load Objects from a Directory

To load multiple objects from a directory, use the LoadFromDirectory trait:

let objects = YourStruct::load_from_directory("directory_path").await?;

License

This crate is licensed under the MIT License. See LICENSE for details.

Dependencies

~17–35MB
~436K SLoC