Comments (8)
from rustbreak.
Thanks! I'll be porting my code to v2..
Btw, in v1 I was storing different types (structs) as values in the db, how would you recommend doing the same in v2? (without introducing an enum with a case for each struct that could occur, because I'm doing it in a general way and the only info I have about the struct is that it is Serialize+Deserialize and has a name (via the TypeName trait)).
I want to keep serializing/deserializing my structs in a general way, I have to port this v1 code:
pub mod disk_cache {
lazy_static! {
static ref DB: Database<String> = Database::open(env::var("DISK_CACHE_PATH").unwrap()).unwrap();
}
pub fn load<T: Default + TypeName>() -> T where for<'de> T: Deserialize<'de> {
DB.retrieve(T::type_name()).unwrap_or(T::default())
}
pub fn try_load<T: TypeName>() -> Option<T> where for<'de> T: Deserialize<'de> {
DB.retrieve(T::type_name()).ok()
}
pub fn store<'de, T: Serialize + TypeName>(x: T) -> rustbreak::Result<()> {
DB.insert(T::type_name(), x)
}
pub fn save() -> rustbreak::Result<()> {
DB.flush()
}
pub fn contains_key<T: TypeName>() -> rustbreak::Result<bool> {
DB.contains_key::<(), _>(T::type_name())
}
}
pub trait PersistRepr: ToFrm + TypeName {
fn try_load() -> Option<Self>;
fn persist(self);
}
impl<T: ToFrm + TypeName> PersistRepr for T {
fn try_load() -> Option<Self> {
println!("{}::try_load()", Self::type_name());
if disk_cache::contains_key::<Self>().unwrap() {
Some(disk_cache::try_load().unwrap())
} else {
None
}
}
fn persist(self) {
println!("{}::persist()", Self::type_name());
disk_cache::store(self).unwrap();
}
}
from rustbreak.
Would it work in v2 with a HashMap<String, Box<Serialize + Deserialize>>
for the db?
from rustbreak.
I don't think so as is, as Serialize and Deserialize is not object safe. If you want the same functionality you will need to have a HashMap<String, Vec<u8>>
and encode it in what you want to have, and decode that when needed. Which is what v1 did.
Another thing you could do is to use a serde_json::Value
and then cast that using from_value
when you need it.
from rustbreak.
But it should work with Serialize + Deserialize
from erased-serde, right?
from rustbreak.
Ah no, only auto traits can be used for + Trait
:(
And it looks like serde_json::Value
is not Serialize + Deserialize
, so it can't be used, right?
from rustbreak.
https://docs.serde.rs/serde_json/value/enum.Value.html#impl-Deserialize%3C%27de%3E
From what I can see it has both.
from rustbreak.
Ah thanks, I was looking at the derive
s...
from rustbreak.
Related Issues (20)
- Missing detailed information from the back-end HOT 2
- Errors with newest stable Rust HOT 5
- Problems with Ron and json Value integers HOT 6
- Load performance problems HOT 15
- [FR] we should be able to minify the output HOT 6
- Bincode not loading in v2 rc3 HOT 14
- Database::try_read() HOT 2
- FileBackend::from_file / Database::from_file docs missing: Does it load()? HOT 2
- Loading database with empty File fails HOT 18
- Update RON HOT 1
- Transition to Rust edition 2018
- Use `data.read()` instead of `data.write()` in `save()` HOT 1
- Describing generic database HOT 6
- Run `cargo fmt` HOT 1
- More checks in CI HOT 3
- UB with yaml DeSer HOT 5
- RUSTSEC-2020-0077: memmap is unmaintained
- Out of memory if file size > memory size HOT 1
- support speedy https://docs.rs/speedy/ HOT 1
- wasm support + wasi support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rustbreak.