Skip to main content

hitbox_core/predicate/
neutral.rs

1//! Neutral predicate that always returns `Cacheable`.
2
3use std::marker::PhantomData;
4
5use async_trait::async_trait;
6
7use super::{Predicate, PredicateResult};
8
9/// A predicate that always returns `Cacheable`.
10///
11/// Useful as a starting point for predicate chains or as a no-op predicate.
12#[derive(Clone, Copy)]
13pub struct Neutral<S> {
14    _phantom: PhantomData<fn(S) -> S>,
15}
16
17impl<S> std::fmt::Debug for Neutral<S> {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        f.debug_struct("Neutral").finish()
20    }
21}
22
23impl<S> Default for Neutral<S> {
24    fn default() -> Self {
25        Self::new()
26    }
27}
28
29impl<S> Neutral<S> {
30    /// Creates a new neutral predicate.
31    pub fn new() -> Self {
32        Self {
33            _phantom: PhantomData,
34        }
35    }
36}
37
38#[async_trait]
39impl<S> Predicate for Neutral<S>
40where
41    S: Send,
42{
43    type Subject = S;
44
45    async fn check(&self, subject: Self::Subject) -> PredicateResult<Self::Subject> {
46        PredicateResult::Cacheable(subject)
47    }
48}