Skip to main content

hitbox_fn/
lib.rs

1#![doc = include_str!("../README.md")]
2#![warn(missing_docs)]
3
4mod args;
5mod cache;
6mod extractor;
7mod upstream;
8
9pub use args::{Arg, Args, Skipped};
10
11/// Marker trait for types that can be used as skipped fields in `#[derive(CacheableResponse)]`.
12///
13/// Skipped fields are not stored in the cache. On cache hit, they are reconstructed
14/// using `Default::default()`. This trait provides a clear compile error when a
15/// skipped field type doesn't implement `Default`.
16#[diagnostic::on_unimplemented(
17    message = "`{Self}` cannot be used as a skipped field in `#[derive(CacheableResponse)]`",
18    note = "skipped fields are not stored in cache and are reconstructed using `Default::default()` on cache hit",
19    note = "either implement `Default` for `{Self}` or remove the `#[cacheable_response(skip)]` attribute"
20)]
21pub trait SkippedFieldDefault {
22    /// Create a default value for a skipped field.
23    fn skipped_default() -> Self;
24}
25
26impl<T: Default> SkippedFieldDefault for T {
27    fn skipped_default() -> Self {
28        Self::default()
29    }
30}
31
32/// Marker trait for types that can be used as cached (non-skipped) fields in `#[derive(CacheableResponse)]`.
33///
34/// Non-skipped fields are stored in the cache and must be cloneable for cache storage.
35/// This trait provides a clear compile error when a cached field type doesn't implement `Clone`.
36#[diagnostic::on_unimplemented(
37    message = "`{Self}` cannot be used as a cached field in `#[derive(CacheableResponse)]`",
38    note = "non-skipped fields are stored in cache and must implement `Clone` for cache storage",
39    note = "either implement `Clone` for `{Self}` or add the `#[cacheable_response(skip)]` attribute"
40)]
41pub trait CachedFieldClone {
42    /// Clone this field for cache storage.
43    fn cached_clone(&self) -> Self;
44}
45
46impl<T: Clone> CachedFieldClone for T {
47    fn cached_clone(&self) -> Self {
48        self.clone()
49    }
50}
51pub use cache::{
52    Cache, CacheAccess, CacheBuilder, NoBackend, NoContext, NoPolicy, WithBackend, WithContext,
53    WithPolicy,
54};
55pub use extractor::{FnExtractor, KeyExtract};
56pub use upstream::FnUpstream;
57
58// Re-export derive macros when feature is enabled
59// Note: KeyExtract derive macro shares name with KeyExtract trait (different namespaces)
60#[cfg(feature = "derive")]
61pub use hitbox_derive::{CacheableRequest, CacheableResponse, KeyExtract, cached};
62
63/// Prelude for convenient imports.
64pub mod prelude {
65    pub use crate::{Arg, Args, Cache, FnExtractor, FnUpstream, KeyExtract, Skipped};
66
67    // Re-export derive macros (KeyExtract derive is re-exported at crate root)
68    #[cfg(feature = "derive")]
69    pub use hitbox_derive::{CacheableRequest, CacheableResponse, cached};
70
71    // Re-export commonly used hitbox types
72    pub use hitbox::KeyPart;
73    pub use hitbox::policy::PolicyConfig;
74    pub use hitbox::{CacheContext, CacheStatus, ResponseSource};
75}