(
&self,
f: impl FnOnce(&mut WriteContext) -> Result<R, Error>,
)
| 726 | /// Uses fast path caching for O(1) access when using the same Fory instance repeatedly. |
| 727 | #[inline(always)] |
| 728 | fn with_write_context<R>( |
| 729 | &self, |
| 730 | f: impl FnOnce(&mut WriteContext) -> Result<R, Error>, |
| 731 | ) -> Result<R, Error> { |
| 732 | // SAFETY: Thread-local storage is only accessed from the current thread. |
| 733 | // We use UnsafeCell to avoid RefCell's runtime borrow checking overhead. |
| 734 | // The closure `f` does not recursively call with_write_context, so there's no aliasing. |
| 735 | WRITE_CONTEXTS.with(|cache| { |
| 736 | let cache = unsafe { &mut *cache.get() }; |
| 737 | let id = self.id; |
| 738 | |
| 739 | let context = cache.get_or_insert_result(id, || { |
| 740 | // Only fetch type resolver when creating a new context |
| 741 | let type_resolver = self.get_final_type_resolver()?; |
| 742 | Ok(Box::new(WriteContext::new( |
| 743 | type_resolver.clone(), |
| 744 | self.config.clone(), |
| 745 | ))) |
| 746 | })?; |
| 747 | f(context) |
| 748 | }) |
| 749 | } |
| 750 | |
| 751 | /// Serializes a value of type `T` into a byte vector. |
| 752 | #[inline(always)] |
no test coverage detected