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
Errortype
§Modules
constants: Data format selection and related constantserror: 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
- Create a serializable struct and instantiate a
FileLinkedobject. - Use
.readonly()for shared access,.mutate()or.replace()for mutation (which automatically syncs to file). - Use
.from_file()to restore from disk.
§Crate Organization
- All core logic is in
FileLinked. - Data format selection is in
constants. - Error types and helpers are in
error.
Modules§
- constants
- Constants used in the
file_linkedcrate. - error
- Error types for the
file_linkedcrate. This module defines a unified error type for the crate, wrapping errors from dependencies and providing conversions from common error types.
Structs§
- File
Linked - A wrapper around an object
Tthat ties the object to a physical file