Crates.io | file_linked |
lib.rs | file_linked |
version | 0.1.3 |
source | src |
created_at | 2021-10-01 17:05:57.302108 |
updated_at | 2021-10-07 09:53:45.346696 |
description | Controlling objects linked directly to a file |
homepage | https://github.com/vandomej/GEMLA/tree/master/file_linked |
repository | https://github.com/vandomej/GEMLA/tree/master/file_linked |
max_upload_size | |
id | 459191 |
size | 12,308 |
This library provides a wrapper around objects and ties the data to a file. It uses serde and bincode currently for serializing and deserializing the files.
use file_linked::*;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::string::ToString;
use std::path::PathBuf;
#[derive(Deserialize, Serialize)]
struct Test {
pub a: u32,
pub b: String,
pub c: f64
}
let test = Test {
a: 1,
b: String::from("two"),
c: 3.0
};
let file_path = PathBuf::from("./file");
// Object is consumed and can only be interacted with through the FileLinked object
let mut linked_test = FileLinked::new(test, &file_path)?;
// You can obtain a readonly reference of the underlying data
assert_eq!(linked_test.readonly().b, String::from("two"));
// Whenever a mutable operation is performed, the changed data is rewritten to the file
linked_test.mutate(|x| x.a += 1)?;
assert_eq!(linked_test.readonly().a, 2);
drop(linked_test);
// You can also initialize an object from a file
let from_file = FileLinked::<Test>::from_file(&file_path)?;
assert_eq!(from_file.readonly().a, 2);
assert_eq!(from_file.readonly().b, String::from("two"));
assert_eq!(from_file.readonly().c, 3.0);
This library is still in development and missing some features and so may not be stable: