Skip to main content

hitbox_core/
cacheable.rs

1//! Serialization trait for cached values.
2//!
3//! This module provides the [`Cacheable`] trait which defines the serialization
4//! requirements for types that can be stored in cache backends.
5//!
6//! ## Feature-Dependent Bounds
7//!
8//! The trait has different bounds depending on enabled features:
9//!
10//! - **Default**: Requires `Serialize + DeserializeOwned + Send + Sync` (serde)
11//! - **`rkyv_format`**: Additionally requires rkyv traits for zero-copy deserialization
12//!
13//! ## Blanket Implementation
14//!
15//! The trait is automatically implemented for all types that satisfy the bounds,
16//! so you don't need to implement it manually. Just derive the required traits:
17//!
18//! ```ignore
19//! #[derive(serde::Serialize, serde::Deserialize)]
20//! #[cfg_attr(feature = "rkyv_format", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))]
21//! struct MyCachedData {
22//!     value: String,
23//! }
24//! // MyCachedData automatically implements Cacheable
25//! ```
26
27use serde::{Serialize, de::DeserializeOwned};
28
29/// Marker trait for types that can be cached.
30///
31/// This trait abstracts serialization requirements for cached values.
32/// It has a blanket implementation for all types that satisfy the bounds,
33/// so you never need to implement it manually.
34///
35/// # Feature-Dependent Bounds
36///
37/// ## Without `rkyv_format` (default)
38///
39/// Requires serde traits for JSON/binary serialization:
40/// - `Serialize + DeserializeOwned + Send + Sync`
41///
42/// ## With `rkyv_format`
43///
44/// Additionally requires rkyv traits for zero-copy deserialization:
45/// - `Archive` - Type can be archived
46/// - `Serialize` - Can serialize to rkyv format
47/// - `Archived: CheckBytes` - Archived form can be validated
48/// - `Archived: Deserialize` - Can deserialize from archived form
49///
50/// # Example
51///
52/// ```ignore
53/// // Works with default features (serde only)
54/// #[derive(serde::Serialize, serde::Deserialize)]
55/// struct BasicData {
56///     value: String,
57/// }
58///
59/// // Works with rkyv_format feature
60/// #[derive(serde::Serialize, serde::Deserialize)]
61/// #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
62/// #[rkyv(check_bytes)]
63/// struct RkyvData {
64///     value: String,
65/// }
66/// ```
67#[cfg(not(feature = "rkyv_format"))]
68pub trait Cacheable: Serialize + DeserializeOwned + Send + Sync {}
69
70#[cfg(not(feature = "rkyv_format"))]
71impl<T> Cacheable for T where T: Serialize + DeserializeOwned + Send + Sync {}
72
73/// Marker trait for types that can be cached.
74///
75/// This trait abstracts serialization requirements for cached values.
76/// It has a blanket implementation for all types that satisfy the bounds,
77/// so you never need to implement it manually.
78///
79/// # Feature-Dependent Bounds
80///
81/// ## Without `rkyv_format` (default)
82///
83/// Requires serde traits for JSON/binary serialization:
84/// - `Serialize + DeserializeOwned + Send + Sync`
85///
86/// ## With `rkyv_format`
87///
88/// Additionally requires rkyv traits for zero-copy deserialization:
89/// - `Archive` - Type can be archived
90/// - `Serialize` - Can serialize to rkyv format
91/// - `Archived: CheckBytes` - Archived form can be validated
92/// - `Archived: Deserialize` - Can deserialize from archived form
93///
94/// # Example
95///
96/// ```ignore
97/// // Works with default features (serde only)
98/// #[derive(serde::Serialize, serde::Deserialize)]
99/// struct BasicData {
100///     value: String,
101/// }
102///
103/// // Works with rkyv_format feature
104/// #[derive(serde::Serialize, serde::Deserialize)]
105/// #[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
106/// #[rkyv(check_bytes)]
107/// struct RkyvData {
108///     value: String,
109/// }
110/// ```
111#[cfg(feature = "rkyv_format")]
112pub trait Cacheable:
113    Serialize
114    + DeserializeOwned
115    + Send
116    + Sync
117    + rkyv::Archive<
118        Archived: for<'a> rkyv::bytecheck::CheckBytes<
119            rkyv::api::high::HighValidator<'a, rkyv::rancor::Error>,
120        > + rkyv::Deserialize<
121            Self,
122            rkyv::rancor::Strategy<rkyv::de::Pool, rkyv::rancor::Error>,
123        >,
124    > + for<'a> rkyv::Serialize<
125        rkyv::api::high::HighSerializer<
126            rkyv::util::AlignedVec,
127            rkyv::ser::allocator::ArenaHandle<'a>,
128            rkyv::rancor::Error,
129        >,
130    >
131{
132}
133
134#[cfg(feature = "rkyv_format")]
135impl<T> Cacheable for T
136where
137    T: Serialize + DeserializeOwned + Send + Sync + rkyv::Archive,
138    T::Archived: for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, rkyv::rancor::Error>>
139        + rkyv::Deserialize<T, rkyv::rancor::Strategy<rkyv::de::Pool, rkyv::rancor::Error>>,
140    T: for<'a> rkyv::Serialize<
141            rkyv::api::high::HighSerializer<
142                rkyv::util::AlignedVec,
143                rkyv::ser::allocator::ArenaHandle<'a>,
144                rkyv::rancor::Error,
145            >,
146        >,
147{
148}