Skip to content

Commit 7f6d6c8

Browse files
finnbearl-7-l
authored andcommitted
Fix thread local access error. (SoftbearStudios#93)
1 parent 1954e56 commit 7f6d6c8

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/pack_ints.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,21 @@ macro_rules! impl_smaller {
236236
// Scratch space to bridge gap between pack_ints and pack_bytes.
237237
// In theory, we could avoid this intermediate step, but it would result in a lot of generated code.
238238
#[cfg(feature = "std")]
239-
fn with_scratch<T>(f: impl FnOnce(&mut Vec<u8>) -> T) -> T {
239+
fn with_scratch<T>(mut f: impl FnMut(&mut Vec<u8>) -> T) -> T {
240240
thread_local! {
241241
static SCRATCH: core::cell::RefCell<Vec<u8>> = const { core::cell::RefCell::new(Vec::new()) }
242242
}
243-
SCRATCH.with(|s| {
244-
let s = &mut s.borrow_mut();
245-
s.clear();
246-
f(s)
247-
})
243+
SCRATCH
244+
.try_with(|s| {
245+
let s = &mut s.borrow_mut();
246+
s.clear();
247+
f(s)
248+
})
249+
.unwrap_or_else(|_| f(&mut Vec::new()))
248250
}
249251
// Resort to allocation.
250252
#[cfg(not(feature = "std"))]
251-
fn with_scratch<T>(f: impl FnOnce(&mut Vec<u8>) -> T) -> T {
253+
fn with_scratch<T>(mut f: impl FnMut(&mut Vec<u8>) -> T) -> T {
252254
f(&mut Vec::new())
253255
}
254256

0 commit comments

Comments
 (0)