xvc_ecs/ecs/storable.rs
1//! Contains [Storable] trait that marks a type suitable to use in [crate::XvcStore].
2use serde::{Deserialize, Serialize};
3
4/// Marks the traits as storable in XvcStore
5///
6/// It requires to implement an fn to describe the store names.
7/// It also requires the implementer to be (serde) serializable, and to implement Clone, Debug, Ord
8/// and PartialEq.
9///
10/// These are to use JSON as storage format, and to be able to use BTreeMaps as data structures.
11pub trait Storable:
12 Serialize + for<'lt> Deserialize<'lt> + Clone + std::fmt::Debug + Ord + PartialEq
13{
14 /// A string representation for the type.
15 ///
16 /// By convention this should be in kebab-case, like `xvc-path` for [xvc-core::XvcPath].
17 /// Xvc uses this to create filenames in serialization.
18 ///
19 /// See [crate::persist] macro to specify this representation conveniently.
20 fn type_description() -> String;
21}
22
23impl Storable for String {
24 fn type_description() -> String {
25 "string".to_string()
26 }
27}
28
29impl<T: Storable> Storable for Option<T> {
30 fn type_description() -> String {
31 format!("option-{}", <T as Storable>::type_description())
32 }
33}
34
35impl<T: Storable, U: Storable> Storable for (T, U) {
36 fn type_description() -> String {
37 format!(
38 "tuple-{}-{}",
39 <T as Storable>::type_description(),
40 <U as Storable>::type_description()
41 )
42 }
43}
44impl Storable for i32 {
45 fn type_description() -> String {
46 "i32".to_string()
47 }
48}
49
50impl Storable for usize {
51 fn type_description() -> String {
52 "usize".to_string()
53 }
54}