fory_core/fory.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::buffer::{Reader, Writer};
19use crate::config::Config;
20use crate::context::{ContextCache, ReadContext, WriteContext};
21use crate::ensure;
22use crate::error::Error;
23use crate::resolver::RefMode;
24use crate::resolver::TypeResolver;
25use crate::serializer::{Serializer, StructSerializer};
26use crate::type_id::config_flags::{IS_CROSS_LANGUAGE_FLAG, IS_OUT_OF_BAND_FLAG};
27use crate::type_id::SIZE_OF_REF_AND_TYPE;
28use std::cell::UnsafeCell;
29use std::mem;
30use std::sync::atomic::{AtomicU64, Ordering};
31use std::sync::OnceLock;
32
33/// Global counter to assign unique IDs to each Fory instance.
34static FORY_ID_COUNTER: AtomicU64 = AtomicU64::new(0);
35
36#[cold]
37#[inline(never)]
38fn final_type_resolver_error(error: &Error) -> Error {
39 Error::type_error(format!("Failed to build type resolver: {error}"))
40}
41
42thread_local! {
43 /// Thread-local storage for WriteContext instances with fast path caching.
44 static WRITE_CONTEXTS: UnsafeCell<ContextCache<WriteContext<'static>>> =
45 UnsafeCell::new(ContextCache::new());
46
47 /// Thread-local storage for ReadContext instances with fast path caching.
48 static READ_CONTEXTS: UnsafeCell<ContextCache<ReadContext<'static>>> =
49 UnsafeCell::new(ContextCache::new());
50}
51
52/// Builder for configuring a [`Fory`] instance before first use.
53///
54/// `ForyBuilder` owns the configuration phase. Call [`build`](Self::build) to create the
55/// instance, then use [`Fory`] for registration and serialization operations.
56///
57/// ```rust
58/// use fory_core::Fory;
59///
60/// let fory = Fory::builder()
61/// .compress_string(true)
62/// .max_dyn_depth(10)
63/// .build();
64/// ```
65#[derive(Default)]
66pub struct ForyBuilder {
67 config: Config,
68 compatible_set: bool,
69}
70
71impl ForyBuilder {
72 /// Sets the serialization compatible mode for this Fory builder.
73 ///
74 /// # Arguments
75 ///
76 /// * `compatible` - The serialization compatible mode to use. Options are:
77 /// - `false`: Every reader and writer must use the same schema.
78 /// Use only for smaller, faster same-schema payloads.
79 /// - `true`: Supports schema evolution and type metadata sharing for better
80 /// cross-version compatibility.
81 ///
82 /// # Returns
83 ///
84 /// Returns `self` for method chaining.
85 ///
86 /// # Note
87 ///
88 /// Setting the compatible mode also automatically configures the `share_meta` flag:
89 /// - `false` → `share_meta = false`
90 /// - `true` → `share_meta = true`
91 ///
92 /// # Examples
93 ///
94 /// ```rust
95 /// use fory_core::Fory;
96 ///
97 /// // Same-schema optimization.
98 /// let fory = Fory::builder().compatible(false).build();
99 /// ```
100 pub fn compatible(mut self, compatible: bool) -> Self {
101 self.compatible_set = true;
102 // Setting share_meta individually is not supported currently
103 self.config.share_meta = compatible;
104 self.config.compatible = compatible;
105 if compatible {
106 self.config.check_struct_version = false;
107 } else if self.config.xlang {
108 self.config.check_struct_version = true;
109 }
110 self
111 }
112
113 /// Enables or disables xlang mode.
114 ///
115 /// # Arguments
116 ///
117 /// * `xlang` - If `true`, uses the xlang wire format compatible with other Fory
118 /// implementations (Java, Python, C++, etc.). If `false`, uses Rust native mode.
119 ///
120 /// # Returns
121 ///
122 /// Returns `self` for method chaining.
123 ///
124 /// # Default
125 ///
126 /// The default value is `true`.
127 ///
128 /// # Examples
129 ///
130 /// ```rust
131 /// use fory_core::Fory;
132 ///
133 /// // Xlang mode, the default cross-language wire format
134 /// let fory = Fory::builder().xlang(true).build();
135 ///
136 /// // Native mode for Rust-only traffic
137 /// let fory = Fory::builder().xlang(false).build();
138 /// ```
139 pub fn xlang(mut self, xlang: bool) -> Self {
140 self.config.xlang = xlang;
141 if !self.compatible_set {
142 self.config.share_meta = true;
143 self.config.compatible = true;
144 self.config.check_struct_version = false;
145 return self;
146 }
147 if !self.config.check_struct_version {
148 self.config.check_struct_version = !self.config.compatible;
149 }
150 self
151 }
152
153 /// Enables or disables meta string compression.
154 ///
155 /// # Arguments
156 ///
157 /// * `compress_string` - If `true`, enables meta string compression to reduce serialized
158 /// payload size by deduplicating and encoding frequently used strings (such as type names
159 /// and field names). If `false`, strings are serialized without compression.
160 ///
161 /// # Returns
162 ///
163 /// Returns `self` for method chaining.
164 ///
165 /// # Default
166 ///
167 /// The default value is `false`.
168 ///
169 /// # Trade-offs
170 ///
171 /// - **Enabled**: Smaller payload size, slightly higher CPU overhead
172 /// - **Disabled**: Larger payload size, faster serialization/deserialization
173 ///
174 /// # Examples
175 ///
176 /// ```rust
177 /// use fory_core::Fory;
178 ///
179 /// let fory = Fory::builder().compress_string(true).build();
180 /// ```
181 pub fn compress_string(mut self, compress_string: bool) -> Self {
182 self.config.compress_string = compress_string;
183 self
184 }
185
186 /// Enables or disables checked UTF-8 string reads.
187 ///
188 /// Checked reads validate UTF-8 payload bytes before constructing Rust `String` values.
189 /// Disabling this keeps the faster unchecked construction path and must only be used when
190 /// serialized bytes are trusted to contain valid UTF-8 strings.
191 ///
192 /// # Default
193 ///
194 /// The default value is `true`.
195 pub fn check_string_read(mut self, check_string_read: bool) -> Self {
196 self.config.check_string_read = check_string_read;
197 self
198 }
199
200 /// Enables or disables schema hash checking for same-schema payloads.
201 ///
202 /// # Arguments
203 ///
204 /// * `check_struct_version` - If `true`, enables schema hash checking for same-schema
205 /// serialization and deserialization. When enabled,
206 /// a version hash computed from field types is written/read to detect schema mismatches.
207 /// If `false`, no version checking is performed.
208 ///
209 /// # Returns
210 ///
211 /// Returns `self` for method chaining.
212 ///
213 /// # Default
214 ///
215 /// The default value is `false`.
216 ///
217 /// # Note
218 ///
219 /// This feature is only effective when `compatible` mode is `false`. In compatible mode,
220 /// schema evolution is supported and version checking is not needed.
221 ///
222 /// # Examples
223 ///
224 /// ```rust
225 /// use fory_core::Fory;
226 ///
227 /// let fory = Fory::builder()
228 /// .compatible(false)
229 /// .check_struct_version(true)
230 /// .build();
231 /// ```
232 pub fn check_struct_version(mut self, check_struct_version: bool) -> Self {
233 if self.config.compatible && check_struct_version {
234 // ignore setting if compatible mode is on
235 return self;
236 }
237 self.config.check_struct_version = check_struct_version;
238 self
239 }
240
241 /// Enables or disables reference tracking for shared and circular references.
242 ///
243 /// # Arguments
244 ///
245 /// * `track_ref` - If `true`, enables reference tracking which allows
246 /// preserving shared object references and circular references during
247 /// serialization/deserialization.
248 ///
249 /// # Returns
250 ///
251 /// Returns `self` for method chaining.
252 ///
253 /// # Default
254 ///
255 /// The default value is `false`.
256 ///
257 /// # Examples
258 ///
259 /// ```rust
260 /// use fory_core::Fory;
261 ///
262 /// let fory = Fory::builder().track_ref(true).build();
263 /// ```
264 pub fn track_ref(mut self, track_ref: bool) -> Self {
265 self.config.track_ref = track_ref;
266 self
267 }
268
269 /// Sets the approximate graph-memory gate for one root deserialization.
270 ///
271 /// Mainly gates materialized collections, maps, arrays, structs, and objects. Leaf values are
272 /// gated by unread input bytes instead, and actual process memory can be higher. Defaults to
273 /// 128 MiB. Values must be positive byte limits.
274 pub fn max_graph_memory_bytes(mut self, max_bytes: usize) -> Self {
275 assert!(
276 max_bytes > 0,
277 "max_graph_memory_bytes must be in [1, usize::MAX]"
278 );
279 self.config.max_graph_memory_bytes = max_bytes;
280 self
281 }
282
283 /// Sets the root allowance for collection elements and map entries not
284 /// backed by newly consumed input bytes. Defaults to 8192. Zero is strict.
285 pub fn max_unbacked_container_items(mut self, max_items: usize) -> Self {
286 self.config.max_unbacked_container_items = max_items;
287 self
288 }
289
290 /// Sets the maximum depth for nested dynamic object serialization.
291 ///
292 /// # Arguments
293 ///
294 /// * `max_dyn_depth` - The maximum nesting depth allowed for dynamically-typed objects
295 /// (e.g., trait objects, boxed types). This prevents stack overflow from deeply nested
296 /// structures in dynamic serialization scenarios.
297 ///
298 /// # Returns
299 ///
300 /// Returns `self` for method chaining.
301 ///
302 /// # Default
303 ///
304 /// The default value is `5`.
305 ///
306 /// # Behavior
307 ///
308 /// When the depth limit is exceeded during deserialization, an error is returned to prevent
309 /// potential stack overflow or infinite recursion.
310 ///
311 /// # Examples
312 ///
313 /// ```rust
314 /// use fory_core::Fory;
315 ///
316 /// // Allow deeper nesting for complex object graphs
317 /// let fory = Fory::builder().max_dyn_depth(10).build();
318 ///
319 /// // Restrict nesting for safer deserialization
320 /// let fory = Fory::builder().max_dyn_depth(3).build();
321 /// ```
322 pub fn max_dyn_depth(mut self, max_dyn_depth: u32) -> Self {
323 self.config.max_dyn_depth = max_dyn_depth;
324 self
325 }
326
327 /// Sets the maximum field count accepted in one received struct TypeMeta.
328 pub fn max_type_fields(mut self, max_fields: usize) -> Self {
329 assert!(max_fields > 0, "max_type_fields must be positive");
330 assert!(
331 u32::try_from(max_fields).is_ok(),
332 "max_type_fields is too large"
333 );
334 self.config.max_type_fields = max_fields as u32;
335 self
336 }
337
338 /// Sets the maximum body size accepted for one received TypeMeta.
339 pub fn max_type_meta_bytes(mut self, max_bytes: usize) -> Self {
340 assert!(max_bytes > 0, "max_type_meta_bytes must be positive");
341 assert!(
342 u32::try_from(max_bytes).is_ok(),
343 "max_type_meta_bytes is too large"
344 );
345 self.config.max_type_meta_bytes = max_bytes as u32;
346 self
347 }
348
349 /// Sets the maximum accepted remote metadata versions for one logical type.
350 pub fn max_schema_versions_per_type(mut self, max_versions: usize) -> Self {
351 assert!(
352 max_versions > 0,
353 "max_schema_versions_per_type must be positive"
354 );
355 assert!(
356 u32::try_from(max_versions).is_ok(),
357 "max_schema_versions_per_type is too large"
358 );
359 self.config.max_schema_versions_per_type = max_versions as u32;
360 self
361 }
362
363 /// Sets the maximum accepted average remote metadata versions across logical types.
364 pub fn max_average_schema_versions_per_type(mut self, max_versions: usize) -> Self {
365 assert!(
366 max_versions > 0,
367 "max_average_schema_versions_per_type must be positive"
368 );
369 assert!(
370 u32::try_from(max_versions).is_ok(),
371 "max_average_schema_versions_per_type is too large"
372 );
373 self.config.max_average_schema_versions_per_type = max_versions as u32;
374 self
375 }
376
377 fn finish_config(self) -> Config {
378 let mut config = self.config;
379 if !self.compatible_set {
380 config.share_meta = true;
381 config.compatible = true;
382 config.check_struct_version = false;
383 }
384 config
385 }
386
387 /// Builds a [`Fory`] instance with the current builder configuration.
388 pub fn build(self) -> Fory {
389 let config = self.finish_config();
390 Fory::from_config(config)
391 }
392}
393
394/// The main Fory serialization framework instance.
395///
396/// `Fory` provides high-performance serialization and deserialization with xlang mode,
397/// native mode, reference tracking, and trait object serialization.
398///
399/// # Features
400///
401/// - **Xlang mode**: Default wire format for cross-language payloads
402/// - **Native mode**: Rust-only wire format selected with `.xlang(false)`
403/// - **Schema evolution**: Compatible mode by default, with a same-schema optimization available
404/// - **Reference tracking**: Handles shared and circular references
405/// - **Trait object serialization**: Supports serializing polymorphic trait objects
406/// - **Dynamic depth limiting**: Configurable limit for nested dynamic object serialization
407///
408/// # Examples
409///
410/// Basic usage:
411///
412/// ```rust, ignore
413/// use fory::Fory;
414/// use fory::{ForyEnum, ForyStruct, ForyUnion};
415///
416/// #[derive(ForyStruct)]
417/// struct User {
418/// name: String,
419/// age: u32,
420/// }
421///
422/// let mut fory = Fory::builder().xlang(true).build();
423/// fory.register_by_name::<User>("example.User").unwrap();
424/// let user = User { name: "Alice".to_string(), age: 30 };
425/// let bytes = fory.serialize(&user).unwrap();
426/// let deserialized: User = fory.deserialize(&bytes).unwrap();
427/// ```
428///
429/// Custom configuration:
430///
431/// ```rust
432/// use fory_core::Fory;
433///
434/// let fory = Fory::builder()
435/// .compress_string(true)
436/// .max_dyn_depth(10)
437/// .build();
438/// ```
439pub struct Fory {
440 /// Unique identifier for this Fory instance, used as key in thread-local context maps.
441 id: u64,
442 type_resolver: TypeResolver,
443 /// Lazy-initialized final type resolver (thread-safe, one-time initialization).
444 final_type_resolver: OnceLock<Result<TypeResolver, Error>>,
445 /// Configuration for serialization behavior.
446 ///
447 /// Keep this cold field after the resolver/cache fields. Remote metadata
448 /// limits make Config larger, but serialize hot paths repeatedly access
449 /// the instance id and resolver snapshot, not the cold limit values.
450 config: Config,
451}
452
453impl Default for Fory {
454 fn default() -> Self {
455 Self::builder().build()
456 }
457}
458
459impl Fory {
460 /// Creates a builder for configuring a [`Fory`] instance.
461 pub fn builder() -> ForyBuilder {
462 ForyBuilder::default()
463 }
464
465 fn from_config(config: Config) -> Self {
466 let mut type_resolver = TypeResolver::default();
467 type_resolver.set_compatible(config.compatible);
468 type_resolver.set_xlang(config.xlang);
469 Self {
470 id: FORY_ID_COUNTER.fetch_add(1, Ordering::Relaxed),
471 config,
472 type_resolver,
473 final_type_resolver: OnceLock::new(),
474 }
475 }
476
477 /// Returns whether xlang mode is enabled.
478 pub fn is_xlang(&self) -> bool {
479 self.config.xlang
480 }
481
482 /// Returns whether compatible schema evolution is enabled.
483 ///
484 /// # Returns
485 ///
486 /// `true` if compatible schema evolution is enabled, `false` otherwise.
487 pub fn is_compatible(&self) -> bool {
488 self.config.compatible
489 }
490
491 /// Returns whether string compression is enabled.
492 ///
493 /// # Returns
494 ///
495 /// `true` if meta string compression is enabled, `false` otherwise.
496 pub fn is_compress_string(&self) -> bool {
497 self.config.compress_string
498 }
499
500 /// Returns whether UTF-8 string payload validation is enabled.
501 pub fn is_check_string_read(&self) -> bool {
502 self.config.check_string_read
503 }
504
505 /// Returns whether metadata sharing is enabled.
506 ///
507 /// # Returns
508 ///
509 /// `true` if metadata sharing is enabled, `false` otherwise.
510 pub fn is_share_meta(&self) -> bool {
511 self.config.share_meta
512 }
513
514 /// Returns the maximum depth for nested dynamic object serialization.
515 pub fn get_max_dyn_depth(&self) -> u32 {
516 self.config.max_dyn_depth
517 }
518
519 /// Returns whether class version checking is enabled.
520 ///
521 /// # Returns
522 ///
523 /// `true` if class version checking is enabled, `false` otherwise.
524 pub fn is_check_struct_version(&self) -> bool {
525 self.config.check_struct_version
526 }
527
528 /// Returns a reference to the configuration.
529 pub fn config(&self) -> &Config {
530 &self.config
531 }
532
533 /// Checks whether the final type resolver has already been initialized.
534 ///
535 /// If it has, further type registrations would be silently ignored (the frozen
536 /// snapshot is what serialize/deserialize actually use),so we fail fast with
537 /// a clear error instead.
538 ///
539 /// # errors
540 ///
541 /// returns [`Error::NotAllowed`] when the resolver snapshot has already been
542 /// built (i.e after the first `serialize` / `deserialize` call).
543 #[cold]
544 #[inline(never)]
545 fn check_registration_allowed(&self) -> Result<(), Error> {
546 if self.final_type_resolver.get().is_some() {
547 return Err(Error::not_allowed(
548 "Type registration is not allowed after the first serialize/deserialize call. \
549 The type resolver snapshot has already been finalized. \
550 Please complete all type registrations before performing any serialization or deserialization.",
551 ));
552 }
553 Ok(())
554 }
555
556 /// Serializes a value of type `T` into a byte vector.
557 ///
558 /// This is for ordinary roots whose value type selects its own serializer
559 /// through `T: Serializer<Target = T>`. For an external target whose
560 /// serializer is a separate type, use [`serialize_with`](Self::serialize_with).
561 ///
562 /// # Type Parameters
563 ///
564 /// * `T` - The type of the value to serialize. Must implement `Serializer`.
565 ///
566 /// # Arguments
567 ///
568 /// * `record` - A reference to the value to serialize.
569 ///
570 /// # Returns
571 ///
572 /// A `Vec<u8>` containing the serialized data.
573 ///
574 /// # Examples
575 ///
576 /// ```rust, ignore
577 /// use fory::Fory;
578 /// use fory::{ForyEnum, ForyStruct, ForyUnion};
579 ///
580 /// #[derive(ForyStruct)]
581 /// struct Point { x: i32, y: i32 }
582 ///
583 /// let mut fory = Fory::builder().xlang(true).build();
584 /// fory.register_by_name::<Point>("example.Point").unwrap();
585 /// let point = Point { x: 10, y: 20 };
586 /// let bytes = fory.serialize(&point).unwrap();
587 /// ```
588 pub fn serialize<T>(&self, record: &T) -> Result<Vec<u8>, Error>
589 where
590 T: Serializer<Target = T>,
591 {
592 self.serialize_with::<T>(record)
593 }
594
595 /// Serializes a value using the explicitly selected serializer.
596 ///
597 /// `record` must be exactly [`Serializer::Target`] for `S`. Register `S`
598 /// first when it is an independently registered structural or custom serializer. Fory-owned
599 /// carrier serializers compose their child serializers and are not registered.
600 pub fn serialize_with<S>(&self, record: &S::Target) -> Result<Vec<u8>, Error>
601 where
602 S: Serializer,
603 {
604 self.with_write_context(
605 |context| match self.serialize_with_context::<S>(record, context) {
606 Ok(_) => {
607 let result = context.writer.dump();
608 context.writer.reset();
609 Ok(result)
610 }
611 Err(err) => {
612 context.writer.reset();
613 Err(err)
614 }
615 },
616 )
617 }
618
619 /// Serializes a value of type `T` into the provided byte buffer.
620 ///
621 /// This is for ordinary roots whose value type selects its own serializer
622 /// through `T: Serializer<Target = T>`. For an external target whose
623 /// serializer is a separate type, use
624 /// [`serialize_to_with`](Self::serialize_to_with).
625 ///
626 /// The serialized data is appended to the end of the buffer by default.
627 /// To write from a specific position, resize the buffer before calling this method.
628 ///
629 /// # Type Parameters
630 ///
631 /// * `T` - The type of the value to serialize. Must implement `Serializer`.
632 ///
633 /// # Arguments
634 ///
635 /// * `buf` - A mutable reference to the byte buffer to append the serialized data to.
636 /// The buffer will be resized as needed during serialization.
637 /// * `record` - A reference to the value to serialize.
638 ///
639 /// # Returns
640 ///
641 /// The number of bytes written to the buffer on success, or an error if serialization fails.
642 ///
643 /// # Notes
644 ///
645 /// - Multiple `serialize_to` calls to the same buffer will append data sequentially.
646 ///
647 /// # Examples
648 ///
649 /// Basic usage - appending to a buffer:
650 ///
651 /// ```rust, ignore
652 /// use fory_core::Fory;
653 /// use fory_derive::{ForyEnum, ForyStruct, ForyUnion};
654 ///
655 /// #[derive(ForyStruct)]
656 /// struct Point {
657 /// x: i32,
658 /// y: i32,
659 /// }
660 ///
661 /// let mut fory = Fory::builder().xlang(true).build();
662 /// fory.register_by_name::<Point>("example.Point").unwrap();
663 /// let point = Point { x: 1, y: 2 };
664 ///
665 /// let mut buf = Vec::new();
666 /// let bytes_written = fory.serialize_to(&mut buf, &point).unwrap();
667 /// assert_eq!(bytes_written, buf.len());
668 /// ```
669 ///
670 /// Multiple serializations to the same buffer:
671 ///
672 /// ```rust, ignore
673 /// use fory_core::Fory;
674 /// use fory_derive::{ForyEnum, ForyStruct, ForyUnion};
675 ///
676 /// #[derive(ForyStruct, PartialEq, Debug)]
677 /// struct Point {
678 /// x: i32,
679 /// y: i32,
680 /// }
681 ///
682 /// let mut fory = Fory::builder().xlang(true).build();
683 /// fory.register_by_name::<Point>("example.Point").unwrap();
684 /// let p1 = Point { x: 1, y: 2 };
685 /// let p2 = Point { x: -3, y: 4 };
686 ///
687 /// let mut buf = Vec::new();
688 ///
689 /// // First serialization
690 /// let len1 = fory.serialize_to(&mut buf, &p1).unwrap();
691 /// let offset1 = buf.len();
692 ///
693 /// // Second serialization - appends to existing data
694 /// let len2 = fory.serialize_to(&mut buf, &p2).unwrap();
695 /// let offset2 = buf.len();
696 ///
697 /// assert_eq!(offset1, len1);
698 /// assert_eq!(offset2, len1 + len2);
699 ///
700 /// // Deserialize both objects
701 /// let deserialized1: Point = fory.deserialize(&buf[0..offset1]).unwrap();
702 /// let deserialized2: Point = fory.deserialize(&buf[offset1..offset2]).unwrap();
703 /// assert_eq!(deserialized1, p1);
704 /// assert_eq!(deserialized2, p2);
705 /// ```
706 ///
707 /// Writing to a specific position using `resize`:
708 /// # Notes on `vec.resize()`
709 ///
710 /// When calling `vec.resize(n, 0)`, note that if `n` is smaller than the current length,
711 /// the buffer will be truncated (not shrunk in capacity). The capacity remains unchanged,
712 /// making subsequent writes efficient for buffer reuse patterns:
713 ///
714 /// ```rust, ignore
715 /// use fory_core::Fory;
716 /// use fory_derive::{ForyEnum, ForyStruct, ForyUnion};
717 ///
718 /// #[derive(ForyStruct)]
719 /// struct Point {
720 /// x: i32,
721 /// y: i32,
722 /// }
723 ///
724 /// let mut fory = Fory::builder().xlang(true).build();
725 /// fory.register_by_name::<Point>("example.Point").unwrap();
726 /// let point = Point { x: 1, y: 2 };
727 ///
728 /// let mut buf = Vec::with_capacity(1024);
729 /// buf.resize(16, 0); // Set length to 16 to append the write, capacity stays 1024
730 ///
731 /// let initial_capacity = buf.capacity();
732 /// fory.serialize_to(&mut buf, &point).unwrap();
733 ///
734 /// // Reset to smaller size to append the write - capacity unchanged
735 /// buf.resize(16, 0);
736 /// assert_eq!(buf.capacity(), initial_capacity); // Capacity not shrunk
737 ///
738 /// // Reuse buffer efficiently without reallocation
739 /// fory.serialize_to(&mut buf, &point).unwrap();
740 /// assert_eq!(buf.capacity(), initial_capacity); // Still no reallocation
741 /// ```
742 pub fn serialize_to<T>(&self, buf: &mut Vec<u8>, record: &T) -> Result<usize, Error>
743 where
744 T: Serializer<Target = T>,
745 {
746 self.serialize_to_with::<T>(buf, record)
747 }
748
749 /// Serializes a value into a buffer using the explicitly selected serializer.
750 ///
751 /// This is the buffer-writing counterpart of [`serialize_with`](Self::serialize_with).
752 pub fn serialize_to_with<S>(
753 &self,
754 buf: &mut Vec<u8>,
755 record: &S::Target,
756 ) -> Result<usize, Error>
757 where
758 S: Serializer,
759 {
760 let start = buf.len();
761 self.with_write_context(|context| {
762 // Context from thread-local would be 'static. but context hold the buffer through `writer` field,
763 // so we should make buffer live longer.
764 // After serializing, `detach_writer` will be called, the writer in context will be set to dangling pointer.
765 // So it's safe to make buf live to the end of this method.
766 let outlive_buffer = unsafe { mem::transmute::<&mut Vec<u8>, &mut Vec<u8>>(buf) };
767 context.attach_writer(Writer::from_buffer(outlive_buffer));
768 let result = self.serialize_with_context::<S>(record, context);
769 let written_size = context.writer.len() - start;
770 context.detach_writer();
771 match result {
772 Ok(_) => Ok(written_size),
773 Err(err) => Err(err),
774 }
775 })
776 }
777
778 /// Gets the final type resolver, building it lazily on first access.
779 #[cold]
780 #[inline(never)]
781 fn get_final_type_resolver(&self) -> Result<&TypeResolver, Error> {
782 let result = self
783 .final_type_resolver
784 .get_or_init(|| self.type_resolver.build_final_type_resolver());
785 result.as_ref().map_err(final_type_resolver_error)
786 }
787
788 /// Executes a closure with mutable access to a WriteContext for this Fory instance.
789 /// The context is stored in thread-local storage, eliminating all lock contention.
790 /// Uses fast path caching for O(1) access when using the same Fory instance repeatedly.
791 #[inline(always)]
792 fn with_write_context<R>(
793 &self,
794 f: impl FnOnce(&mut WriteContext) -> Result<R, Error>,
795 ) -> Result<R, Error> {
796 // SAFETY: Thread-local storage is only accessed from the current thread.
797 // We use UnsafeCell to avoid RefCell's runtime borrow checking overhead.
798 // The closure `f` does not recursively call with_write_context, so there's no aliasing.
799 WRITE_CONTEXTS.with(|cache| {
800 let cache = unsafe { &mut *cache.get() };
801 let id = self.id;
802
803 let context = cache.get_or_insert_result(id, || {
804 // Only fetch type resolver when creating a new context
805 let type_resolver = self.get_final_type_resolver()?;
806 Ok(Box::new(WriteContext::new(
807 type_resolver.clone(),
808 self.config.clone(),
809 )))
810 })?;
811 f(context)
812 })
813 }
814
815 /// Serializes a value of type `T` into a byte vector.
816 #[inline(always)]
817 fn serialize_with_context<S: Serializer>(
818 &self,
819 record: &S::Target,
820 context: &mut WriteContext,
821 ) -> Result<(), Error> {
822 let result = self.serialize_with_context_inner::<S>(record, context);
823 context.reset();
824 result
825 }
826
827 #[inline(always)]
828 fn serialize_with_context_inner<S: Serializer>(
829 &self,
830 record: &S::Target,
831 context: &mut WriteContext,
832 ) -> Result<(), Error> {
833 self.write_head::<S>(&mut context.writer);
834 // Use RefMode based on config:
835 // - If track_ref is enabled, use RefMode::Tracking for the root object
836 // - Otherwise, use RefMode::NullOnly which writes NOT_NULL_VALUE_FLAG
837 let ref_mode = if self.config.track_ref {
838 RefMode::Tracking
839 } else {
840 RefMode::NullOnly
841 };
842 // TypeMeta is written inline during serialization (streaming protocol)
843 S::write(record, context, ref_mode, true)?;
844 Ok(())
845 }
846
847 /// Registers a structural serializer with a numeric type ID.
848 ///
849 /// This accepts ordinary and external structural serializers for structs
850 /// and enum-category types.
851 ///
852 /// # Type Parameters
853 ///
854 /// * `S` - The structural serializer to register.
855 ///
856 /// # Arguments
857 ///
858 /// * `id` - A unique numeric identifier for the type. This ID is used in the serialized format
859 /// to identify the type during deserialization.
860 ///
861 /// # Errors
862 ///
863 /// Returns an error when the serializer category, target, type ID, or
864 /// another registration identity conflicts with an existing registration.
865 ///
866 /// # Examples
867 ///
868 /// ```rust, ignore
869 /// use fory::Fory;
870 /// use fory::{ForyEnum, ForyStruct, ForyUnion};
871 ///
872 /// #[derive(ForyStruct)]
873 /// struct User { name: String, age: u32 }
874 ///
875 /// let mut fory = Fory::builder().xlang(true).build();
876 /// fory.register::<User>(100).unwrap();
877 /// ```
878 pub fn register<S: StructSerializer>(&mut self, id: u32) -> Result<(), Error> {
879 self.check_registration_allowed()?;
880 self.type_resolver.register::<S>(id)
881 }
882
883 /// Registers an xlang-compatible union serializer with a numeric type ID.
884 ///
885 /// The serializer may be generated by derive or by the schema compiler.
886 pub fn register_union<S: StructSerializer>(&mut self, id: u32) -> Result<(), Error> {
887 self.check_registration_allowed()?;
888 self.type_resolver.register_union::<S>(id)
889 }
890
891 /// Registers a structural serializer with a qualified type name.
892 ///
893 /// This accepts ordinary and external structural serializers for structs
894 /// and enum-category types.
895 ///
896 /// # Type Parameters
897 ///
898 /// * `S` - The structural serializer to register.
899 ///
900 /// # Arguments
901 ///
902 /// * `name` - The type name, optionally prefixed with a namespace separated by `.`.
903 /// For example, `"com.example.User"` uses namespace `"com.example"` and type name `"User"`.
904 /// Use `"User"` for the default namespace.
905 ///
906 /// # Notes
907 ///
908 /// This registration method is preferred for xlang serialization because it uses
909 /// human-readable type identifiers instead of numeric IDs, which improves compatibility
910 /// across different language implementations.
911 ///
912 /// # Examples
913 ///
914 /// The example uses xlang mode because name-based registration is the preferred
915 /// registration style for cross-language payloads.
916 ///
917 /// ```rust, ignore
918 /// use fory::Fory;
919 /// use fory::{ForyEnum, ForyStruct, ForyUnion};
920 ///
921 /// #[derive(ForyStruct)]
922 /// struct User { name: String, age: u32 }
923 ///
924 /// let mut fory = Fory::builder().xlang(true).build();
925 /// fory.register_by_name::<User>("com.example.User").unwrap();
926 /// ```
927 pub fn register_by_name<S: StructSerializer>(&mut self, name: &str) -> Result<(), Error> {
928 self.check_registration_allowed()?;
929 self.type_resolver.register_by_name::<S>(name)
930 }
931
932 /// Registers an xlang-compatible union serializer with a qualified name.
933 ///
934 /// The serializer may be generated by derive or by the schema compiler.
935 pub fn register_union_by_name<S: StructSerializer>(&mut self, name: &str) -> Result<(), Error> {
936 self.check_registration_allowed()?;
937 self.type_resolver.register_union_by_name::<S>(name)
938 }
939
940 /// Registers a custom serializer with a numeric type ID.
941 ///
942 /// # Type Parameters
943 ///
944 /// * `S` - The custom EXT serializer to register.
945 /// Unlike `register()`, this does not require `StructSerializer`, making it suitable
946 /// for non-struct types or types with custom serialization logic.
947 ///
948 /// # Arguments
949 ///
950 /// * `id` - A unique numeric identifier for the type.
951 ///
952 /// # Use Cases
953 ///
954 /// Use this method for a custom serializer that declares the EXT
955 /// wire category and implements opaque, hand-written serialization for its target.
956 ///
957 /// # Examples
958 ///
959 /// ```rust, ignore
960 /// use fory_core::Fory;
961 ///
962 /// let mut fory = Fory::builder().xlang(false).build();
963 /// fory.register_serializer::<UuidSerializer>(200).unwrap();
964 /// ```
965 pub fn register_serializer<S: Serializer>(&mut self, id: u32) -> Result<(), Error> {
966 self.check_registration_allowed()?;
967 self.type_resolver.register_serializer::<S>(id)
968 }
969
970 /// Registers a custom serializer with a qualified type name.
971 ///
972 /// # Type Parameters
973 ///
974 /// * `S` - The custom EXT serializer to register.
975 ///
976 /// # Arguments
977 ///
978 /// * `name` - The type name, optionally prefixed with a namespace separated by `.`.
979 ///
980 /// # Notes
981 ///
982 /// This is the named equivalent of `register_serializer()`, preferred for
983 /// xlang serialization scenarios.
984 ///
985 pub fn register_serializer_by_name<S: Serializer>(&mut self, name: &str) -> Result<(), Error> {
986 self.check_registration_allowed()?;
987 self.type_resolver.register_serializer_by_name::<S>(name)
988 }
989
990 /// Writes the serialization header to the writer.
991 #[inline(always)]
992 pub fn write_head<S: Serializer>(&self, writer: &mut Writer) {
993 const HEAD_SIZE: usize = 10;
994 writer.reserve(S::reserved_space() + SIZE_OF_REF_AND_TYPE + HEAD_SIZE);
995 let bitmap = if self.config.xlang {
996 IS_CROSS_LANGUAGE_FLAG
997 } else {
998 0
999 };
1000 writer.write_u8(bitmap);
1001 }
1002
1003 /// Deserializes data from a byte slice into a value of type `T`.
1004 ///
1005 /// # Type Parameters
1006 ///
1007 /// * `T` - A local target type whose serializer is selected by the type itself.
1008 ///
1009 /// # Arguments
1010 ///
1011 /// * `bf` - The byte slice containing the serialized data.
1012 ///
1013 /// # Returns
1014 ///
1015 /// * `Ok(T)` - The deserialized value on success.
1016 /// * `Err(Error)` - An error if deserialization fails (e.g., invalid format, type mismatch).
1017 ///
1018 /// # Panics
1019 ///
1020 /// Panics in debug mode if there are unread bytes remaining after successful deserialization,
1021 /// indicating a potential protocol violation.
1022 ///
1023 /// # Examples
1024 ///
1025 /// ```rust, ignore
1026 /// use fory::Fory;
1027 /// use fory::{ForyEnum, ForyStruct, ForyUnion};
1028 ///
1029 /// #[derive(ForyStruct)]
1030 /// struct Point { x: i32, y: i32 }
1031 ///
1032 /// let mut fory = Fory::builder().xlang(true).build();
1033 /// fory.register_by_name::<Point>("example.Point").unwrap();
1034 /// let point = Point { x: 10, y: 20 };
1035 /// let bytes = fory.serialize(&point).unwrap();
1036 /// let deserialized: Point = fory.deserialize(&bytes).unwrap();
1037 /// ```
1038 pub fn deserialize<T>(&self, bf: &[u8]) -> Result<T, Error>
1039 where
1040 T: Serializer<Target = T>,
1041 {
1042 self.deserialize_with::<T>(bf)
1043 }
1044
1045 /// Deserializes a value using the explicitly selected serializer.
1046 ///
1047 /// The result is exactly [`Serializer::Target`] for `S`. Its wire bytes must
1048 /// have been written with the same serializer or a schema-compatible peer.
1049 pub fn deserialize_with<S>(&self, bf: &[u8]) -> Result<S::Target, Error>
1050 where
1051 S: Serializer,
1052 {
1053 self.with_read_context(|context| {
1054 let outlive_buffer = unsafe { mem::transmute::<&[u8], &[u8]>(bf) };
1055 context.attach_reader(Reader::new(outlive_buffer));
1056 context.remaining_graph_memory_bytes = self.config.max_graph_memory_bytes;
1057 context.remaining_unbacked_container_items = self.config.max_unbacked_container_items;
1058 let result = self.deserialize_with_context::<S>(context);
1059 context.detach_reader();
1060 result
1061 })
1062 }
1063
1064 /// Deserializes data from a `Reader` into a value of type `T`.
1065 ///
1066 /// This method is the paired read operation for [`serialize_to`](Self::serialize_to).
1067 /// It reads serialized data from the current position of the reader and automatically
1068 /// advances the cursor to the end of the read data, making it suitable for reading
1069 /// multiple objects sequentially from the same buffer.
1070 ///
1071 /// # Type Parameters
1072 ///
1073 /// * `T` - A local target type whose serializer is selected by the type itself.
1074 ///
1075 /// # Arguments
1076 ///
1077 /// * `reader` - A mutable reference to the `Reader` containing the serialized data.
1078 /// The reader's cursor will be advanced to the end of the deserialized data.
1079 ///
1080 /// # Returns
1081 ///
1082 /// * `Ok(T)` - The deserialized value on success.
1083 /// * `Err(Error)` - An error if deserialization fails (e.g., invalid format, type mismatch).
1084 ///
1085 /// # Notes
1086 ///
1087 /// - The reader's cursor is automatically updated after each successful read.
1088 /// - This method is ideal for reading multiple objects from the same buffer sequentially.
1089 /// - See [`serialize_to`](Self::serialize_to) for complete usage examples.
1090 ///
1091 /// # Examples
1092 ///
1093 /// Basic usage:
1094 ///
1095 /// ```rust, ignore
1096 /// use fory_core::{Fory, Reader};
1097 /// use fory_derive::{ForyEnum, ForyStruct, ForyUnion};
1098 ///
1099 /// #[derive(ForyStruct)]
1100 /// struct Point { x: i32, y: i32 }
1101 ///
1102 /// let mut fory = Fory::builder().xlang(true).build();
1103 /// fory.register_by_name::<Point>("example.Point").unwrap();
1104 /// let point = Point { x: 10, y: 20 };
1105 ///
1106 /// let mut buf = Vec::new();
1107 /// fory.serialize_to(&mut buf, &point).unwrap();
1108 ///
1109 /// let mut reader = Reader::new(&buf);
1110 /// let deserialized: Point = fory.deserialize_from(&mut reader).unwrap();
1111 /// ```
1112 pub fn deserialize_from<T>(&self, reader: &mut Reader) -> Result<T, Error>
1113 where
1114 T: Serializer<Target = T>,
1115 {
1116 self.deserialize_from_with::<T>(reader)
1117 }
1118
1119 /// Deserializes from a reader using the explicitly selected serializer.
1120 ///
1121 /// This is the reader-based counterpart of [`deserialize_with`](Self::deserialize_with).
1122 pub fn deserialize_from_with<S>(&self, reader: &mut Reader) -> Result<S::Target, Error>
1123 where
1124 S: Serializer,
1125 {
1126 self.with_read_context(|context| {
1127 let outlive_buffer = unsafe { mem::transmute::<&[u8], &[u8]>(reader.bf) };
1128 let mut new_reader = Reader::new(outlive_buffer);
1129 new_reader.set_cursor(reader.cursor);
1130 context.attach_reader(new_reader);
1131 context.remaining_graph_memory_bytes = self.config.max_graph_memory_bytes;
1132 context.remaining_unbacked_container_items = self.config.max_unbacked_container_items;
1133 let result = self.deserialize_with_context::<S>(context);
1134 let end = context.detach_reader().get_cursor();
1135 reader.set_cursor(end);
1136 result
1137 })
1138 }
1139
1140 /// Executes a closure with mutable access to a ReadContext for this Fory instance.
1141 /// The context is stored in thread-local storage, eliminating all lock contention.
1142 /// Uses fast path caching for O(1) access when using the same Fory instance repeatedly.
1143 #[inline(always)]
1144 fn with_read_context<R>(
1145 &self,
1146 f: impl FnOnce(&mut ReadContext) -> Result<R, Error>,
1147 ) -> Result<R, Error> {
1148 // SAFETY: Thread-local storage is only accessed from the current thread.
1149 // We use UnsafeCell to avoid RefCell's runtime borrow checking overhead.
1150 // The closure `f` does not recursively call with_read_context, so there's no aliasing.
1151 READ_CONTEXTS.with(|cache| {
1152 let cache = unsafe { &mut *cache.get() };
1153 let id = self.id;
1154
1155 let context = cache.get_or_insert_result(id, || {
1156 // Only fetch type resolver when creating a new context
1157 let type_resolver = self.get_final_type_resolver()?;
1158 Ok(Box::new(ReadContext::new(
1159 type_resolver.clone(),
1160 self.config.clone(),
1161 )))
1162 })?;
1163 f(context)
1164 })
1165 }
1166
1167 #[inline(always)]
1168 fn deserialize_with_context<S: Serializer>(
1169 &self,
1170 context: &mut ReadContext,
1171 ) -> Result<S::Target, Error> {
1172 let result = self.deserialize_with_context_inner::<S>(context);
1173 context.reset();
1174 result
1175 }
1176
1177 #[inline(always)]
1178 fn deserialize_with_context_inner<S: Serializer>(
1179 &self,
1180 context: &mut ReadContext,
1181 ) -> Result<S::Target, Error> {
1182 self.read_head(&mut context.reader)?;
1183 // Use RefMode based on config:
1184 // - If track_ref is enabled, use RefMode::Tracking for the root object
1185 // - Otherwise, use RefMode::NullOnly
1186 let ref_mode = if self.config.track_ref {
1187 RefMode::Tracking
1188 } else {
1189 RefMode::NullOnly
1190 };
1191 let result = S::read(context, ref_mode, true);
1192 context.ref_reader.resolve_callbacks();
1193 result
1194 }
1195
1196 #[inline(always)]
1197 fn read_head(&self, reader: &mut Reader) -> Result<(), Error> {
1198 let bitmap = reader.read_u8()?;
1199 let expected = if self.config.xlang {
1200 IS_CROSS_LANGUAGE_FLAG
1201 } else {
1202 0
1203 };
1204 if bitmap != expected {
1205 return self.read_head_slow(bitmap, expected);
1206 }
1207 Ok(())
1208 }
1209
1210 #[cold]
1211 #[inline(never)]
1212 fn read_head_slow(&self, bitmap: u8, expected: u8) -> Result<(), Error> {
1213 const KNOWN_FLAGS: u8 = IS_CROSS_LANGUAGE_FLAG | IS_OUT_OF_BAND_FLAG;
1214 ensure!(
1215 (bitmap & !KNOWN_FLAGS) == 0 && (bitmap & IS_OUT_OF_BAND_FLAG) == 0,
1216 Error::invalid_data("unsupported root header bitmap")
1217 );
1218 ensure!(
1219 (bitmap & IS_CROSS_LANGUAGE_FLAG) == (expected & IS_CROSS_LANGUAGE_FLAG),
1220 Error::invalid_data("header bitmap mismatch at xlang bit")
1221 );
1222 Ok(())
1223 }
1224}
1225
1226#[cfg(test)]
1227mod tests {
1228 use super::Fory;
1229
1230 #[test]
1231 fn compatible_defaults_and_overrides() {
1232 let default_xlang = Fory::builder().xlang(true).finish_config();
1233 let default_native = Fory::builder().xlang(false).finish_config();
1234 let explicit_same_schema = Fory::builder()
1235 .compatible(false)
1236 .xlang(true)
1237 .finish_config();
1238 let explicit_same_schema_reverse_order = Fory::builder()
1239 .xlang(true)
1240 .compatible(false)
1241 .finish_config();
1242
1243 assert!(default_xlang.compatible);
1244 assert!(default_xlang.share_meta);
1245 assert!(!default_xlang.check_struct_version);
1246 assert!(default_native.compatible);
1247 assert!(default_native.share_meta);
1248 assert!(!default_native.check_struct_version);
1249
1250 assert!(!explicit_same_schema.compatible);
1251 assert!(!explicit_same_schema.share_meta);
1252 assert!(explicit_same_schema.check_struct_version);
1253 assert!(!explicit_same_schema_reverse_order.compatible);
1254 assert!(!explicit_same_schema_reverse_order.share_meta);
1255 assert!(explicit_same_schema_reverse_order.check_struct_version);
1256 }
1257}