From 49d029fb793f10f353e37ea61af7e76793362397 Mon Sep 17 00:00:00 2001 From: Finn Bear Date: Wed, 26 Nov 2025 13:20:50 -0800 Subject: [PATCH] Fix thread local access error. --- src/pack_ints.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/pack_ints.rs b/src/pack_ints.rs index d2d1d9c..68b4431 100644 --- a/src/pack_ints.rs +++ b/src/pack_ints.rs @@ -236,19 +236,21 @@ macro_rules! impl_smaller { // Scratch space to bridge gap between pack_ints and pack_bytes. // In theory, we could avoid this intermediate step, but it would result in a lot of generated code. #[cfg(feature = "std")] -fn with_scratch(f: impl FnOnce(&mut Vec) -> T) -> T { +fn with_scratch(mut f: impl FnMut(&mut Vec) -> T) -> T { thread_local! { static SCRATCH: core::cell::RefCell> = const { core::cell::RefCell::new(Vec::new()) } } - SCRATCH.with(|s| { - let s = &mut s.borrow_mut(); - s.clear(); - f(s) - }) + SCRATCH + .try_with(|s| { + let s = &mut s.borrow_mut(); + s.clear(); + f(s) + }) + .unwrap_or_else(|_| f(&mut Vec::new())) } // Resort to allocation. #[cfg(not(feature = "std"))] -fn with_scratch(f: impl FnOnce(&mut Vec) -> T) -> T { +fn with_scratch(mut f: impl FnMut(&mut Vec) -> T) -> T { f(&mut Vec::new()) }