Crate file_linked

Crate file_linked 

Source
Expand description

§file_linked

file_linked is a Rust library for binding objects directly to files, providing persistent, file-backed state management for any serializable type. It is designed for use cases where you want to transparently synchronize in-memory data with a file, supporting both synchronous and asynchronous mutation patterns.

§Features

  • Generic wrapper FileLinked<T> for any serializable type
  • Automatic file synchronization on mutation or replacement
  • Asynchronous and thread-safe access using tokio::RwLock
  • Support for multiple serialization formats (JSON, Bincode)
  • Simple API for reading, mutating, and replacing data
  • Error handling with unified Error type

§Modules

  • constants: Data format selection and related constants
  • error: Error types for serialization, I/O, and general errors

§Example


#[derive(Deserialize, Serialize)]
struct Test {
    pub a: u32,
    pub b: String,
    pub c: f64
}

#[tokio::main]
async fn main() {
    let test = Test { a: 1, b: String::from("two"), c: 3.0 };
    let mut linked = FileLinked::new(test, &PathBuf::from("./file"), DataFormat::Json).await.unwrap();
    linked.mutate(|x| x.a += 1).await.unwrap();
    assert_eq!(linked.readonly().read().await.a, 2);
}

§Usage

  1. Create a serializable struct and instantiate a FileLinked object.
  2. Use .readonly() for shared access, .mutate() or .replace() for mutation (which automatically syncs to file).
  3. Use .from_file() to restore from disk.

§Crate Organization

Modules§

constants
Constants used in the file_linked crate.
error
Error types for the file_linked crate. This module defines a unified error type for the crate, wrapping errors from dependencies and providing conversions from common error types.

Structs§

FileLinked
A wrapper around an object T that ties the object to a physical file