MCPcopy Create free account
hub / github.com/apache/fory / with_write_context

Method with_write_context

rust/fory-core/src/fory.rs:728–749  ·  view source on GitHub ↗
(
        &self,
        f: impl FnOnce(&mut WriteContext) -> Result<R, Error>,
    )

Source from the content-addressed store, hash-verified

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)]

Callers 2

serializeMethod · 0.80
serialize_toMethod · 0.80

Calls 4

get_or_insert_resultMethod · 0.80
getMethod · 0.65
cloneMethod · 0.45

Tested by

no test coverage detected