1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#![doc = include_str!("../README.md")]
#![deny(missing_debug_implementations)]
#![deny(missing_docs)]
mod atomic;
mod internal;
#[cfg(test)]
mod test;
use std::ops::{BitAnd, BitAndAssign};

/// A condition that a cache can depend on. Is automatically invalidated when dropped.
///
/// ```
/// # use renege::Condition;
/// let cond = Condition::new();
/// let token = cond.token();
/// assert!(token.is_valid());
/// drop(cond);
/// assert!(!token.is_valid());
/// ```
pub struct Condition(Token);

impl Condition {
    /// Creates a new [`Condition`].
    pub fn new() -> Self {
        Self(internal::ThreadAllocator::with(internal::source))
    }

    /// Gets a [`Token`] which is valid for as long as this [`Condition`] is alive.
    pub fn token(&self) -> Token {
        self.0
    }
}

impl Drop for Condition {
    fn drop(&mut self) {
        let Token { block, ext_id } = self.0;
        internal::ThreadAllocator::with(|alloc| {
            internal::invalidate_source(alloc, block.as_ref(), ext_id)
        });
    }
}

impl Default for Condition {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for Condition {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_tuple("Condition")
            .field(&internal::TokenId::from(self.0.ext_id).index())
            .finish()
    }
}

/// Tracks the valdity of an arbitrary set of [`Condition`]s.
#[derive(Clone, Copy)]
pub struct Token {
    block: internal::TokenBlockRef<'static>,
    ext_id: internal::ExtTokenId,
}

impl Token {
    /// Gets a token which is always valid. This is the [`Default`] token.
    ///
    /// ```
    /// # use renege::Token;
    /// assert!(Token::always().is_valid())
    /// ```
    pub fn always() -> Self {
        internal::always()
    }

    /// Gets a token which is never valid.
    ///
    /// ```
    /// # use renege::Token;
    /// assert!(!Token::never().is_valid())
    /// ```
    pub fn never() -> Self {
        internal::never()
    }

    /// Indicates whether this token is still valid. Once this returns `false`, it will never
    /// return `true` again.
    pub fn is_valid(&self) -> bool {
        internal::is_valid(*self)
    }
}

impl Default for Token {
    fn default() -> Self {
        Self::always()
    }
}

impl BitAnd for Token {
    type Output = Token;
    fn bitand(self, rhs: Self) -> Token {
        internal::ThreadAllocator::with(|alloc| internal::dependent(alloc, self, rhs))
    }
}

impl BitAndAssign for Token {
    fn bitand_assign(&mut self, rhs: Self) {
        *self = *self & rhs;
    }
}

impl std::fmt::Debug for Token {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut d = f.debug_tuple("Token");
        let mut deps = Vec::new();
        if internal::dependencies(*self, |dep| {
            deps.push(internal::TokenId::from(dep.ext_id).index())
        }) {
            d.field(&deps);
        } else {
            d.field(&format_args!("<invalid>"));
        }
        d.finish()
    }
}