Coverage Report

Created: 2026-03-11 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/thread_local-1.1.9/src/cached.rs
Line
Count
Source
1
#![allow(deprecated)]
2
3
use super::{IntoIter, IterMut, ThreadLocal};
4
use std::fmt;
5
use std::panic::UnwindSafe;
6
use std::usize;
7
8
/// Wrapper around [`ThreadLocal`].
9
///
10
/// This used to add a fast path for a single thread, however that has been
11
/// obsoleted by performance improvements to [`ThreadLocal`] itself.
12
#[deprecated(since = "1.1.0", note = "Use `ThreadLocal` instead")]
13
pub struct CachedThreadLocal<T: Send> {
14
    inner: ThreadLocal<T>,
15
}
16
17
impl<T: Send> Default for CachedThreadLocal<T> {
18
0
    fn default() -> CachedThreadLocal<T> {
19
0
        CachedThreadLocal::new()
20
0
    }
21
}
22
23
impl<T: Send> CachedThreadLocal<T> {
24
    /// Creates a new empty `CachedThreadLocal`.
25
    #[inline]
26
0
    pub fn new() -> CachedThreadLocal<T> {
27
0
        CachedThreadLocal {
28
0
            inner: ThreadLocal::new(),
29
0
        }
30
0
    }
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<fuzzy_matcher::skim::MatrixCell>>>>::new
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<char>>>>::new
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<_>>::new
31
32
    /// Returns the element for the current thread, if it exists.
33
    #[inline]
34
0
    pub fn get(&self) -> Option<&T> {
35
0
        self.inner.get()
36
0
    }
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<fuzzy_matcher::skim::MatrixCell>>>>::get
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<char>>>>::get
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<_>>::get
37
38
    /// Returns the element for the current thread, or creates it if it doesn't
39
    /// exist.
40
    #[inline]
41
0
    pub fn get_or<F>(&self, create: F) -> &T
42
0
    where
43
0
        F: FnOnce() -> T,
44
    {
45
0
        self.inner.get_or(create)
46
0
    }
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<fuzzy_matcher::skim::MatrixCell>>>>::get_or::<<fuzzy_matcher::skim::SkimMatcherV2>::fuzzy::{closure#0}>
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<char>>>>::get_or::<<fuzzy_matcher::skim::SkimMatcherV2>::fuzzy::{closure#2}>
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<char>>>>::get_or::<<fuzzy_matcher::skim::SkimMatcherV2>::fuzzy::{closure#1}>
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<char>>>>::get_or::<<fuzzy_matcher::clangd::ClangdMatcher as fuzzy_matcher::FuzzyMatcher>::fuzzy_match::{closure#0}>
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<char>>>>::get_or::<<fuzzy_matcher::clangd::ClangdMatcher as fuzzy_matcher::FuzzyMatcher>::fuzzy_match::{closure#1}>
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<char>>>>::get_or::<<fuzzy_matcher::clangd::ClangdMatcher as fuzzy_matcher::FuzzyMatcher>::fuzzy_indices::{closure#0}>
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<core::cell::RefCell<alloc::vec::Vec<char>>>>::get_or::<<fuzzy_matcher::clangd::ClangdMatcher as fuzzy_matcher::FuzzyMatcher>::fuzzy_indices::{closure#1}>
Unexecuted instantiation: <thread_local::cached::CachedThreadLocal<_>>::get_or::<_>
47
48
    /// Returns the element for the current thread, or creates it if it doesn't
49
    /// exist. If `create` fails, that error is returned and no element is
50
    /// added.
51
    #[inline]
52
0
    pub fn get_or_try<F, E>(&self, create: F) -> Result<&T, E>
53
0
    where
54
0
        F: FnOnce() -> Result<T, E>,
55
    {
56
0
        self.inner.get_or_try(create)
57
0
    }
58
59
    /// Returns a mutable iterator over the local values of all threads.
60
    ///
61
    /// Since this call borrows the `ThreadLocal` mutably, this operation can
62
    /// be done safely---the mutable borrow statically guarantees no other
63
    /// threads are currently accessing their associated values.
64
    #[inline]
65
0
    pub fn iter_mut(&mut self) -> CachedIterMut<T> {
66
0
        CachedIterMut {
67
0
            inner: self.inner.iter_mut(),
68
0
        }
69
0
    }
70
71
    /// Removes all thread-specific values from the `ThreadLocal`, effectively
72
    /// reseting it to its original state.
73
    ///
74
    /// Since this call borrows the `ThreadLocal` mutably, this operation can
75
    /// be done safely---the mutable borrow statically guarantees no other
76
    /// threads are currently accessing their associated values.
77
    #[inline]
78
0
    pub fn clear(&mut self) {
79
0
        self.inner.clear();
80
0
    }
81
}
82
83
impl<T: Send> IntoIterator for CachedThreadLocal<T> {
84
    type Item = T;
85
    type IntoIter = CachedIntoIter<T>;
86
87
0
    fn into_iter(self) -> CachedIntoIter<T> {
88
0
        CachedIntoIter {
89
0
            inner: self.inner.into_iter(),
90
0
        }
91
0
    }
92
}
93
94
impl<'a, T: Send + 'a> IntoIterator for &'a mut CachedThreadLocal<T> {
95
    type Item = &'a mut T;
96
    type IntoIter = CachedIterMut<'a, T>;
97
98
0
    fn into_iter(self) -> CachedIterMut<'a, T> {
99
0
        self.iter_mut()
100
0
    }
101
}
102
103
impl<T: Send + Default> CachedThreadLocal<T> {
104
    /// Returns the element for the current thread, or creates a default one if
105
    /// it doesn't exist.
106
0
    pub fn get_or_default(&self) -> &T {
107
0
        self.get_or(T::default)
108
0
    }
109
}
110
111
impl<T: Send + fmt::Debug> fmt::Debug for CachedThreadLocal<T> {
112
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113
0
        write!(f, "ThreadLocal {{ local_data: {:?} }}", self.get())
114
0
    }
115
}
116
117
impl<T: Send + UnwindSafe> UnwindSafe for CachedThreadLocal<T> {}
118
119
/// Mutable iterator over the contents of a `CachedThreadLocal`.
120
#[deprecated(since = "1.1.0", note = "Use `IterMut` instead")]
121
pub struct CachedIterMut<'a, T: Send + 'a> {
122
    inner: IterMut<'a, T>,
123
}
124
125
impl<'a, T: Send + 'a> Iterator for CachedIterMut<'a, T> {
126
    type Item = &'a mut T;
127
128
    #[inline]
129
0
    fn next(&mut self) -> Option<&'a mut T> {
130
0
        self.inner.next()
131
0
    }
132
133
    #[inline]
134
0
    fn size_hint(&self) -> (usize, Option<usize>) {
135
0
        self.inner.size_hint()
136
0
    }
137
}
138
139
impl<'a, T: Send + 'a> ExactSizeIterator for CachedIterMut<'a, T> {}
140
141
/// An iterator that moves out of a `CachedThreadLocal`.
142
#[deprecated(since = "1.1.0", note = "Use `IntoIter` instead")]
143
pub struct CachedIntoIter<T: Send> {
144
    inner: IntoIter<T>,
145
}
146
147
impl<T: Send> Iterator for CachedIntoIter<T> {
148
    type Item = T;
149
150
    #[inline]
151
0
    fn next(&mut self) -> Option<T> {
152
0
        self.inner.next()
153
0
    }
154
155
    #[inline]
156
0
    fn size_hint(&self) -> (usize, Option<usize>) {
157
0
        self.inner.size_hint()
158
0
    }
159
}
160
161
impl<T: Send> ExactSizeIterator for CachedIntoIter<T> {}