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 maximum depth for nested dynamic object serialization.
284 ///
285 /// # Arguments
286 ///
287 /// * `max_dyn_depth` - The maximum nesting depth allowed for dynamically-typed objects
288 /// (e.g., trait objects, boxed types). This prevents stack overflow from deeply nested
289 /// structures in dynamic serialization scenarios.
290 ///
291 /// # Returns
292 ///
293 /// Returns `self` for method chaining.
294 ///
295 /// # Default
296 ///
297 /// The default value is `5`.
298 ///
299 /// # Behavior
300 ///
301 /// When the depth limit is exceeded during deserialization, an error is returned to prevent
302 /// potential stack overflow or infinite recursion.
303 ///
304 /// # Examples
305 ///
306 /// ```rust
307 /// use fory_core::Fory;
308 ///
309 /// // Allow deeper nesting for complex object graphs
310 /// let fory = Fory::builder().max_dyn_depth(10).build();
311 ///
312 /// // Restrict nesting for safer deserialization
313 /// let fory = Fory::builder().max_dyn_depth(3).build();
314 /// ```
315 pub fn max_dyn_depth(mut self, max_dyn_depth: u32) -> Self {
316 self.config.max_dyn_depth = max_dyn_depth;
317 self
318 }
319
320 /// Sets the maximum field count accepted in one received struct TypeMeta.
321 pub fn max_type_fields(mut self, max_fields: usize) -> Self {
322 assert!(max_fields > 0, "max_type_fields must be positive");
323 assert!(
324 u32::try_from(max_fields).is_ok(),
325 "max_type_fields is too large"
326 );
327 self.config.max_type_fields = max_fields as u32;
328 self
329 }
330
331 /// Sets the maximum body size accepted for one received TypeMeta.
332 pub fn max_type_meta_bytes(mut self, max_bytes: usize) -> Self {
333 assert!(max_bytes > 0, "max_type_meta_bytes must be positive");
334 assert!(
335 u32::try_from(max_bytes).is_ok(),
336 "max_type_meta_bytes is too large"
337 );
338 self.config.max_type_meta_bytes = max_bytes as u32;
339 self
340 }
341
342 /// Sets the maximum accepted remote metadata versions for one logical type.
343 pub fn max_schema_versions_per_type(mut self, max_versions: usize) -> Self {
344 assert!(
345 max_versions > 0,
346 "max_schema_versions_per_type must be positive"
347 );
348 assert!(
349 u32::try_from(max_versions).is_ok(),
350 "max_schema_versions_per_type is too large"
351 );
352 self.config.max_schema_versions_per_type = max_versions as u32;
353 self
354 }
355
356 /// Sets the maximum accepted average remote metadata versions across logical types.
357 pub fn max_average_schema_versions_per_type(mut self, max_versions: usize) -> Self {
358 assert!(
359 max_versions > 0,
360 "max_average_schema_versions_per_type must be positive"
361 );
362 assert!(
363 u32::try_from(max_versions).is_ok(),
364 "max_average_schema_versions_per_type is too large"
365 );
366 self.config.max_average_schema_versions_per_type = max_versions as u32;
367 self
368 }
369
370 fn finish_config(self) -> Config {
371 let mut config = self.config;
372 if !self.compatible_set {
373 config.share_meta = true;
374 config.compatible = true;
375 config.check_struct_version = false;
376 }
377 config
378 }
379
380 /// Builds a [`Fory`] instance with the current builder configuration.
381 pub fn build(self) -> Fory {
382 let config = self.finish_config();
383 Fory::from_config(config)
384 }
385}
386
387/// The main Fory serialization framework instance.
388///
389/// `Fory` provides high-performance serialization and deserialization with xlang mode,
390/// native mode, reference tracking, and trait object serialization.
391///
392/// # Features
393///
394/// - **Xlang mode**: Default wire format for cross-language payloads
395/// - **Native mode**: Rust-only wire format selected with `.xlang(false)`
396/// - **Schema evolution**: Compatible mode by default, with a same-schema optimization available
397/// - **Reference tracking**: Handles shared and circular references
398/// - **Trait object serialization**: Supports serializing polymorphic trait objects
399/// - **Dynamic depth limiting**: Configurable limit for nested dynamic object serialization
400///
401/// # Examples
402///
403/// Basic usage:
404///
405/// ```rust, ignore
406/// use fory::Fory;
407/// use fory::{ForyEnum, ForyStruct, ForyUnion};
408///
409/// #[derive(ForyStruct)]
410/// struct User {
411/// name: String,
412/// age: u32,
413/// }
414///
415/// let mut fory = Fory::builder().xlang(true).build();
416/// fory.register_by_name::<User>("example.User").unwrap();
417/// let user = User { name: "Alice".to_string(), age: 30 };
418/// let bytes = fory.serialize(&user).unwrap();
419/// let deserialized: User = fory.deserialize(&bytes).unwrap();
420/// ```
421///
422/// Custom configuration:
423///
424/// ```rust
425/// use fory_core::Fory;
426///
427/// let fory = Fory::builder()
428/// .compress_string(true)
429/// .max_dyn_depth(10)
430/// .build();
431/// ```
432pub struct Fory {
433 /// Unique identifier for this Fory instance, used as key in thread-local context maps.
434 id: u64,
435 type_resolver: TypeResolver,
436 /// Lazy-initialized final type resolver (thread-safe, one-time initialization).
437 final_type_resolver: OnceLock<Result<TypeResolver, Error>>,
438 /// Configuration for serialization behavior.
439 ///
440 /// Keep this cold field after the resolver/cache fields. Remote metadata
441 /// limits make Config larger, but serialize hot paths repeatedly access
442 /// the instance id and resolver snapshot, not the cold limit values.
443 config: Config,
444}
445
446impl Default for Fory {
447 fn default() -> Self {
448 Self::builder().build()
449 }
450}
451
452impl Fory {
453 /// Creates a builder for configuring a [`Fory`] instance.
454 pub fn builder() -> ForyBuilder {
455 ForyBuilder::default()
456 }
457
458 fn from_config(config: Config) -> Self {
459 let mut type_resolver = TypeResolver::default();
460 type_resolver.set_compatible(config.compatible);
461 type_resolver.set_xlang(config.xlang);
462 Self {
463 id: FORY_ID_COUNTER.fetch_add(1, Ordering::Relaxed),
464 config,
465 type_resolver,
466 final_type_resolver: OnceLock::new(),
467 }
468 }
469
470 /// Returns whether xlang mode is enabled.
471 pub fn is_xlang(&self) -> bool {
472 self.config.xlang
473 }
474
475 /// Returns whether compatible schema evolution is enabled.
476 ///
477 /// # Returns
478 ///
479 /// `true` if compatible schema evolution is enabled, `false` otherwise.
480 pub fn is_compatible(&self) -> bool {
481 self.config.compatible
482 }
483
484 /// Returns whether string compression is enabled.
485 ///
486 /// # Returns
487 ///
488 /// `true` if meta string compression is enabled, `false` otherwise.
489 pub fn is_compress_string(&self) -> bool {
490 self.config.compress_string
491 }
492
493 /// Returns whether UTF-8 string payload validation is enabled.
494 pub fn is_check_string_read(&self) -> bool {
495 self.config.check_string_read
496 }
497
498 /// Returns whether metadata sharing is enabled.
499 ///
500 /// # Returns
501 ///
502 /// `true` if metadata sharing is enabled, `false` otherwise.
503 pub fn is_share_meta(&self) -> bool {
504 self.config.share_meta
505 }
506
507 /// Returns the maximum depth for nested dynamic object serialization.
508 pub fn get_max_dyn_depth(&self) -> u32 {
509 self.config.max_dyn_depth
510 }
511
512 /// Returns whether class version checking is enabled.
513 ///
514 /// # Returns
515 ///
516 /// `true` if class version checking is enabled, `false` otherwise.
517 pub fn is_check_struct_version(&self) -> bool {
518 self.config.check_struct_version
519 }
520
521 /// Returns a reference to the configuration.
522 pub fn config(&self) -> &Config {
523 &self.config
524 }
525
526 /// Checks whether the final type resolver has already been initialized.
527 ///
528 /// If it has, further type registrations would be silently ignored (the frozen
529 /// snapshot is what serialize/deserialize actually use),so we fail fast with
530 /// a clear error instead.
531 ///
532 /// # errors
533 ///
534 /// returns [`Error::NotAllowed`] when the resolver snapshot has already been
535 /// built (i.e after the first `serialize` / `deserialize` call).
536 #[cold]
537 #[inline(never)]
538 fn check_registration_allowed(&self) -> Result<(), Error> {
539 if self.final_type_resolver.get().is_some() {
540 return Err(Error::not_allowed(
541 "Type registration is not allowed after the first serialize/deserialize call. \
542 The type resolver snapshot has already been finalized. \
543 Please complete all type registrations before performing any serialization or deserialization.",
544 ));
545 }
546 Ok(())
547 }
548
549 /// Serializes a value of type `T` into a byte vector.
550 ///
551 /// This is for ordinary roots whose value type selects its own serializer
552 /// through `T: Serializer<Target = T>`. For an external target whose
553 /// serializer is a separate type, use [`serialize_with`](Self::serialize_with).
554 ///
555 /// # Type Parameters
556 ///
557 /// * `T` - The type of the value to serialize. Must implement `Serializer`.
558 ///
559 /// # Arguments
560 ///
561 /// * `record` - A reference to the value to serialize.
562 ///
563 /// # Returns
564 ///
565 /// A `Vec<u8>` containing the serialized data.
566 ///
567 /// # Examples
568 ///
569 /// ```rust, ignore
570 /// use fory::Fory;
571 /// use fory::{ForyEnum, ForyStruct, ForyUnion};
572 ///
573 /// #[derive(ForyStruct)]
574 /// struct Point { x: i32, y: i32 }
575 ///
576 /// let mut fory = Fory::builder().xlang(true).build();
577 /// fory.register_by_name::<Point>("example.Point").unwrap();
578 /// let point = Point { x: 10, y: 20 };
579 /// let bytes = fory.serialize(&point).unwrap();
580 /// ```
581 pub fn serialize<T>(&self, record: &T) -> Result<Vec<u8>, Error>
582 where
583 T: Serializer<Target = T>,
584 {
585 self.serialize_with::<T>(record)
586 }
587
588 /// Serializes a value using the explicitly selected serializer.
589 ///
590 /// `record` must be exactly [`Serializer::Target`] for `S`. Register `S`
591 /// first when it is an independently registered structural or manual serializer. Fory-owned
592 /// carrier serializers compose their child serializers and are not registered.
593 pub fn serialize_with<S>(&self, record: &S::Target) -> Result<Vec<u8>, Error>
594 where
595 S: Serializer,
596 {
597 self.with_write_context(
598 |context| match self.serialize_with_context::<S>(record, context) {
599 Ok(_) => {
600 let result = context.writer.dump();
601 context.writer.reset();
602 Ok(result)
603 }
604 Err(err) => {
605 context.writer.reset();
606 Err(err)
607 }
608 },
609 )
610 }
611
612 /// Serializes a value of type `T` into the provided byte buffer.
613 ///
614 /// This is for ordinary roots whose value type selects its own serializer
615 /// through `T: Serializer<Target = T>`. For an external target whose
616 /// serializer is a separate type, use
617 /// [`serialize_to_with`](Self::serialize_to_with).
618 ///
619 /// The serialized data is appended to the end of the buffer by default.
620 /// To write from a specific position, resize the buffer before calling this method.
621 ///
622 /// # Type Parameters
623 ///
624 /// * `T` - The type of the value to serialize. Must implement `Serializer`.
625 ///
626 /// # Arguments
627 ///
628 /// * `buf` - A mutable reference to the byte buffer to append the serialized data to.
629 /// The buffer will be resized as needed during serialization.
630 /// * `record` - A reference to the value to serialize.
631 ///
632 /// # Returns
633 ///
634 /// The number of bytes written to the buffer on success, or an error if serialization fails.
635 ///
636 /// # Notes
637 ///
638 /// - Multiple `serialize_to` calls to the same buffer will append data sequentially.
639 ///
640 /// # Examples
641 ///
642 /// Basic usage - appending to a buffer:
643 ///
644 /// ```rust, ignore
645 /// use fory_core::Fory;
646 /// use fory_derive::{ForyEnum, ForyStruct, ForyUnion};
647 ///
648 /// #[derive(ForyStruct)]
649 /// struct Point {
650 /// x: i32,
651 /// y: i32,
652 /// }
653 ///
654 /// let mut fory = Fory::builder().xlang(true).build();
655 /// fory.register_by_name::<Point>("example.Point").unwrap();
656 /// let point = Point { x: 1, y: 2 };
657 ///
658 /// let mut buf = Vec::new();
659 /// let bytes_written = fory.serialize_to(&mut buf, &point).unwrap();
660 /// assert_eq!(bytes_written, buf.len());
661 /// ```
662 ///
663 /// Multiple serializations to the same buffer:
664 ///
665 /// ```rust, ignore
666 /// use fory_core::Fory;
667 /// use fory_derive::{ForyEnum, ForyStruct, ForyUnion};
668 ///
669 /// #[derive(ForyStruct, PartialEq, Debug)]
670 /// struct Point {
671 /// x: i32,
672 /// y: i32,
673 /// }
674 ///
675 /// let mut fory = Fory::builder().xlang(true).build();
676 /// fory.register_by_name::<Point>("example.Point").unwrap();
677 /// let p1 = Point { x: 1, y: 2 };
678 /// let p2 = Point { x: -3, y: 4 };
679 ///
680 /// let mut buf = Vec::new();
681 ///
682 /// // First serialization
683 /// let len1 = fory.serialize_to(&mut buf, &p1).unwrap();
684 /// let offset1 = buf.len();
685 ///
686 /// // Second serialization - appends to existing data
687 /// let len2 = fory.serialize_to(&mut buf, &p2).unwrap();
688 /// let offset2 = buf.len();
689 ///
690 /// assert_eq!(offset1, len1);
691 /// assert_eq!(offset2, len1 + len2);
692 ///
693 /// // Deserialize both objects
694 /// let deserialized1: Point = fory.deserialize(&buf[0..offset1]).unwrap();
695 /// let deserialized2: Point = fory.deserialize(&buf[offset1..offset2]).unwrap();
696 /// assert_eq!(deserialized1, p1);
697 /// assert_eq!(deserialized2, p2);
698 /// ```
699 ///
700 /// Writing to a specific position using `resize`:
701 /// # Notes on `vec.resize()`
702 ///
703 /// When calling `vec.resize(n, 0)`, note that if `n` is smaller than the current length,
704 /// the buffer will be truncated (not shrunk in capacity). The capacity remains unchanged,
705 /// making subsequent writes efficient for buffer reuse patterns:
706 ///
707 /// ```rust, ignore
708 /// use fory_core::Fory;
709 /// use fory_derive::{ForyEnum, ForyStruct, ForyUnion};
710 ///
711 /// #[derive(ForyStruct)]
712 /// struct Point {
713 /// x: i32,
714 /// y: i32,
715 /// }
716 ///
717 /// let mut fory = Fory::builder().xlang(true).build();
718 /// fory.register_by_name::<Point>("example.Point").unwrap();
719 /// let point = Point { x: 1, y: 2 };
720 ///
721 /// let mut buf = Vec::with_capacity(1024);
722 /// buf.resize(16, 0); // Set length to 16 to append the write, capacity stays 1024
723 ///
724 /// let initial_capacity = buf.capacity();
725 /// fory.serialize_to(&mut buf, &point).unwrap();
726 ///
727 /// // Reset to smaller size to append the write - capacity unchanged
728 /// buf.resize(16, 0);
729 /// assert_eq!(buf.capacity(), initial_capacity); // Capacity not shrunk
730 ///
731 /// // Reuse buffer efficiently without reallocation
732 /// fory.serialize_to(&mut buf, &point).unwrap();
733 /// assert_eq!(buf.capacity(), initial_capacity); // Still no reallocation
734 /// ```
735 pub fn serialize_to<T>(&self, buf: &mut Vec<u8>, record: &T) -> Result<usize, Error>
736 where
737 T: Serializer<Target = T>,
738 {
739 self.serialize_to_with::<T>(buf, record)
740 }
741
742 /// Serializes a value into a buffer using the explicitly selected serializer.
743 ///
744 /// This is the buffer-writing counterpart of [`serialize_with`](Self::serialize_with).
745 pub fn serialize_to_with<S>(
746 &self,
747 buf: &mut Vec<u8>,
748 record: &S::Target,
749 ) -> Result<usize, Error>
750 where
751 S: Serializer,
752 {
753 let start = buf.len();
754 self.with_write_context(|context| {
755 // Context from thread-local would be 'static. but context hold the buffer through `writer` field,
756 // so we should make buffer live longer.
757 // After serializing, `detach_writer` will be called, the writer in context will be set to dangling pointer.
758 // So it's safe to make buf live to the end of this method.
759 let outlive_buffer = unsafe { mem::transmute::<&mut Vec<u8>, &mut Vec<u8>>(buf) };
760 context.attach_writer(Writer::from_buffer(outlive_buffer));
761 let result = self.serialize_with_context::<S>(record, context);
762 let written_size = context.writer.len() - start;
763 context.detach_writer();
764 match result {
765 Ok(_) => Ok(written_size),
766 Err(err) => Err(err),
767 }
768 })
769 }
770
771 /// Gets the final type resolver, building it lazily on first access.
772 #[cold]
773 #[inline(never)]
774 fn get_final_type_resolver(&self) -> Result<&TypeResolver, Error> {
775 let result = self
776 .final_type_resolver
777 .get_or_init(|| self.type_resolver.build_final_type_resolver());
778 result.as_ref().map_err(final_type_resolver_error)
779 }
780
781 /// Executes a closure with mutable access to a WriteContext for this Fory instance.
782 /// The context is stored in thread-local storage, eliminating all lock contention.
783 /// Uses fast path caching for O(1) access when using the same Fory instance repeatedly.
784 #[inline(always)]
785 fn with_write_context<R>(
786 &self,
787 f: impl FnOnce(&mut WriteContext) -> Result<R, Error>,
788 ) -> Result<R, Error> {
789 // SAFETY: Thread-local storage is only accessed from the current thread.
790 // We use UnsafeCell to avoid RefCell's runtime borrow checking overhead.
791 // The closure `f` does not recursively call with_write_context, so there's no aliasing.
792 WRITE_CONTEXTS.with(|cache| {
793 let cache = unsafe { &mut *cache.get() };
794 let id = self.id;
795
796 let context = cache.get_or_insert_result(id, || {
797 // Only fetch type resolver when creating a new context
798 let type_resolver = self.get_final_type_resolver()?;
799 Ok(Box::new(WriteContext::new(
800 type_resolver.clone(),
801 self.config.clone(),
802 )))
803 })?;
804 f(context)
805 })
806 }
807
808 /// Serializes a value of type `T` into a byte vector.
809 #[inline(always)]
810 fn serialize_with_context<S: Serializer>(
811 &self,
812 record: &S::Target,
813 context: &mut WriteContext,
814 ) -> Result<(), Error> {
815 let result = self.serialize_with_context_inner::<S>(record, context);
816 context.reset();
817 result
818 }
819
820 #[inline(always)]
821 fn serialize_with_context_inner<S: Serializer>(
822 &self,
823 record: &S::Target,
824 context: &mut WriteContext,
825 ) -> Result<(), Error> {
826 self.write_head::<S>(&mut context.writer);
827 // Use RefMode based on config:
828 // - If track_ref is enabled, use RefMode::Tracking for the root object
829 // - Otherwise, use RefMode::NullOnly which writes NOT_NULL_VALUE_FLAG
830 let ref_mode = if self.config.track_ref {
831 RefMode::Tracking
832 } else {
833 RefMode::NullOnly
834 };
835 // TypeMeta is written inline during serialization (streaming protocol)
836 S::write(record, context, ref_mode, true)?;
837 Ok(())
838 }
839
840 /// Registers a structural serializer with a numeric type ID.
841 ///
842 /// This accepts ordinary and external structural serializers for structs
843 /// and enum-category types.
844 ///
845 /// # Type Parameters
846 ///
847 /// * `S` - The structural serializer to register.
848 ///
849 /// # Arguments
850 ///
851 /// * `id` - A unique numeric identifier for the type. This ID is used in the serialized format
852 /// to identify the type during deserialization.
853 ///
854 /// # Errors
855 ///
856 /// Returns an error when the serializer category, target, type ID, or
857 /// another registration identity conflicts with an existing registration.
858 ///
859 /// # Examples
860 ///
861 /// ```rust, ignore
862 /// use fory::Fory;
863 /// use fory::{ForyEnum, ForyStruct, ForyUnion};
864 ///
865 /// #[derive(ForyStruct)]
866 /// struct User { name: String, age: u32 }
867 ///
868 /// let mut fory = Fory::builder().xlang(true).build();
869 /// fory.register::<User>(100).unwrap();
870 /// ```
871 pub fn register<S: StructSerializer>(&mut self, id: u32) -> Result<(), Error> {
872 self.check_registration_allowed()?;
873 self.type_resolver.register::<S>(id)
874 }
875
876 /// Registers an xlang-compatible union serializer with a numeric type ID.
877 ///
878 /// The serializer may be generated by derive or by the schema compiler.
879 pub fn register_union<S: StructSerializer>(&mut self, id: u32) -> Result<(), Error> {
880 self.check_registration_allowed()?;
881 self.type_resolver.register_union::<S>(id)
882 }
883
884 /// Registers a structural serializer with a qualified type name.
885 ///
886 /// This accepts ordinary and external structural serializers for structs
887 /// and enum-category types.
888 ///
889 /// # Type Parameters
890 ///
891 /// * `S` - The structural serializer to register.
892 ///
893 /// # Arguments
894 ///
895 /// * `name` - The type name, optionally prefixed with a namespace separated by `.`.
896 /// For example, `"com.example.User"` uses namespace `"com.example"` and type name `"User"`.
897 /// Use `"User"` for the default namespace.
898 ///
899 /// # Notes
900 ///
901 /// This registration method is preferred for xlang serialization because it uses
902 /// human-readable type identifiers instead of numeric IDs, which improves compatibility
903 /// across different language implementations.
904 ///
905 /// # Examples
906 ///
907 /// The example uses xlang mode because name-based registration is the preferred
908 /// registration style for cross-language payloads.
909 ///
910 /// ```rust, ignore
911 /// use fory::Fory;
912 /// use fory::{ForyEnum, ForyStruct, ForyUnion};
913 ///
914 /// #[derive(ForyStruct)]
915 /// struct User { name: String, age: u32 }
916 ///
917 /// let mut fory = Fory::builder().xlang(true).build();
918 /// fory.register_by_name::<User>("com.example.User").unwrap();
919 /// ```
920 pub fn register_by_name<S: StructSerializer>(&mut self, name: &str) -> Result<(), Error> {
921 self.check_registration_allowed()?;
922 self.type_resolver.register_by_name::<S>(name)
923 }
924
925 /// Registers an xlang-compatible union serializer with a qualified name.
926 ///
927 /// The serializer may be generated by derive or by the schema compiler.
928 pub fn register_union_by_name<S: StructSerializer>(&mut self, name: &str) -> Result<(), Error> {
929 self.check_registration_allowed()?;
930 self.type_resolver.register_union_by_name::<S>(name)
931 }
932
933 /// Registers a manual serializer with a numeric type ID.
934 ///
935 /// # Type Parameters
936 ///
937 /// * `S` - The manual EXT serializer to register.
938 /// Unlike `register()`, this does not require `StructSerializer`, making it suitable
939 /// for non-struct types or types with manual serialization logic.
940 ///
941 /// # Arguments
942 ///
943 /// * `id` - A unique numeric identifier for the type.
944 ///
945 /// # Use Cases
946 ///
947 /// Use this method for a manual serializer that declares the EXT
948 /// wire category and implements opaque, hand-written serialization for its target.
949 ///
950 /// # Examples
951 ///
952 /// ```rust, ignore
953 /// use fory_core::Fory;
954 ///
955 /// let mut fory = Fory::builder().xlang(false).build();
956 /// fory.register_serializer::<UuidSerializer>(200).unwrap();
957 /// ```
958 pub fn register_serializer<S: Serializer>(&mut self, id: u32) -> Result<(), Error> {
959 self.check_registration_allowed()?;
960 self.type_resolver.register_serializer::<S>(id)
961 }
962
963 /// Registers a manual serializer with a qualified type name.
964 ///
965 /// # Type Parameters
966 ///
967 /// * `S` - The manual EXT serializer to register.
968 ///
969 /// # Arguments
970 ///
971 /// * `name` - The type name, optionally prefixed with a namespace separated by `.`.
972 ///
973 /// # Notes
974 ///
975 /// This is the named equivalent of `register_serializer()`, preferred for
976 /// xlang serialization scenarios.
977 ///
978 pub fn register_serializer_by_name<S: Serializer>(&mut self, name: &str) -> Result<(), Error> {
979 self.check_registration_allowed()?;
980 self.type_resolver.register_serializer_by_name::<S>(name)
981 }
982
983 /// Writes the serialization header to the writer.
984 #[inline(always)]
985 pub fn write_head<S: Serializer>(&self, writer: &mut Writer) {
986 const HEAD_SIZE: usize = 10;
987 writer.reserve(S::reserved_space() + SIZE_OF_REF_AND_TYPE + HEAD_SIZE);
988 let bitmap = if self.config.xlang {
989 IS_CROSS_LANGUAGE_FLAG
990 } else {
991 0
992 };
993 writer.write_u8(bitmap);
994 }
995
996 /// Deserializes data from a byte slice into a value of type `T`.
997 ///
998 /// # Type Parameters
999 ///
1000 /// * `T` - A local target type whose serializer is selected by the type itself.
1001 ///
1002 /// # Arguments
1003 ///
1004 /// * `bf` - The byte slice containing the serialized data.
1005 ///
1006 /// # Returns
1007 ///
1008 /// * `Ok(T)` - The deserialized value on success.
1009 /// * `Err(Error)` - An error if deserialization fails (e.g., invalid format, type mismatch).
1010 ///
1011 /// # Panics
1012 ///
1013 /// Panics in debug mode if there are unread bytes remaining after successful deserialization,
1014 /// indicating a potential protocol violation.
1015 ///
1016 /// # Examples
1017 ///
1018 /// ```rust, ignore
1019 /// use fory::Fory;
1020 /// use fory::{ForyEnum, ForyStruct, ForyUnion};
1021 ///
1022 /// #[derive(ForyStruct)]
1023 /// struct Point { x: i32, y: i32 }
1024 ///
1025 /// let mut fory = Fory::builder().xlang(true).build();
1026 /// fory.register_by_name::<Point>("example.Point").unwrap();
1027 /// let point = Point { x: 10, y: 20 };
1028 /// let bytes = fory.serialize(&point).unwrap();
1029 /// let deserialized: Point = fory.deserialize(&bytes).unwrap();
1030 /// ```
1031 pub fn deserialize<T>(&self, bf: &[u8]) -> Result<T, Error>
1032 where
1033 T: Serializer<Target = T>,
1034 {
1035 self.deserialize_with::<T>(bf)
1036 }
1037
1038 /// Deserializes a value using the explicitly selected serializer.
1039 ///
1040 /// The result is exactly [`Serializer::Target`] for `S`. Its wire bytes must
1041 /// have been written with the same serializer or a schema-compatible peer.
1042 pub fn deserialize_with<S>(&self, bf: &[u8]) -> Result<S::Target, Error>
1043 where
1044 S: Serializer,
1045 {
1046 self.with_read_context(|context| {
1047 let outlive_buffer = unsafe { mem::transmute::<&[u8], &[u8]>(bf) };
1048 context.attach_reader(Reader::new(outlive_buffer));
1049 context.remaining_graph_memory_bytes = self.config.max_graph_memory_bytes;
1050 let result = self.deserialize_with_context::<S>(context);
1051 context.detach_reader();
1052 result
1053 })
1054 }
1055
1056 /// Deserializes data from a `Reader` into a value of type `T`.
1057 ///
1058 /// This method is the paired read operation for [`serialize_to`](Self::serialize_to).
1059 /// It reads serialized data from the current position of the reader and automatically
1060 /// advances the cursor to the end of the read data, making it suitable for reading
1061 /// multiple objects sequentially from the same buffer.
1062 ///
1063 /// # Type Parameters
1064 ///
1065 /// * `T` - A local target type whose serializer is selected by the type itself.
1066 ///
1067 /// # Arguments
1068 ///
1069 /// * `reader` - A mutable reference to the `Reader` containing the serialized data.
1070 /// The reader's cursor will be advanced to the end of the deserialized data.
1071 ///
1072 /// # Returns
1073 ///
1074 /// * `Ok(T)` - The deserialized value on success.
1075 /// * `Err(Error)` - An error if deserialization fails (e.g., invalid format, type mismatch).
1076 ///
1077 /// # Notes
1078 ///
1079 /// - The reader's cursor is automatically updated after each successful read.
1080 /// - This method is ideal for reading multiple objects from the same buffer sequentially.
1081 /// - See [`serialize_to`](Self::serialize_to) for complete usage examples.
1082 ///
1083 /// # Examples
1084 ///
1085 /// Basic usage:
1086 ///
1087 /// ```rust, ignore
1088 /// use fory_core::{Fory, Reader};
1089 /// use fory_derive::{ForyEnum, ForyStruct, ForyUnion};
1090 ///
1091 /// #[derive(ForyStruct)]
1092 /// struct Point { x: i32, y: i32 }
1093 ///
1094 /// let mut fory = Fory::builder().xlang(true).build();
1095 /// fory.register_by_name::<Point>("example.Point").unwrap();
1096 /// let point = Point { x: 10, y: 20 };
1097 ///
1098 /// let mut buf = Vec::new();
1099 /// fory.serialize_to(&mut buf, &point).unwrap();
1100 ///
1101 /// let mut reader = Reader::new(&buf);
1102 /// let deserialized: Point = fory.deserialize_from(&mut reader).unwrap();
1103 /// ```
1104 pub fn deserialize_from<T>(&self, reader: &mut Reader) -> Result<T, Error>
1105 where
1106 T: Serializer<Target = T>,
1107 {
1108 self.deserialize_from_with::<T>(reader)
1109 }
1110
1111 /// Deserializes from a reader using the explicitly selected serializer.
1112 ///
1113 /// This is the reader-based counterpart of [`deserialize_with`](Self::deserialize_with).
1114 pub fn deserialize_from_with<S>(&self, reader: &mut Reader) -> Result<S::Target, Error>
1115 where
1116 S: Serializer,
1117 {
1118 self.with_read_context(|context| {
1119 let outlive_buffer = unsafe { mem::transmute::<&[u8], &[u8]>(reader.bf) };
1120 let mut new_reader = Reader::new(outlive_buffer);
1121 new_reader.set_cursor(reader.cursor);
1122 context.attach_reader(new_reader);
1123 context.remaining_graph_memory_bytes = self.config.max_graph_memory_bytes;
1124 let result = self.deserialize_with_context::<S>(context);
1125 let end = context.detach_reader().get_cursor();
1126 reader.set_cursor(end);
1127 result
1128 })
1129 }
1130
1131 /// Executes a closure with mutable access to a ReadContext for this Fory instance.
1132 /// The context is stored in thread-local storage, eliminating all lock contention.
1133 /// Uses fast path caching for O(1) access when using the same Fory instance repeatedly.
1134 #[inline(always)]
1135 fn with_read_context<R>(
1136 &self,
1137 f: impl FnOnce(&mut ReadContext) -> Result<R, Error>,
1138 ) -> Result<R, Error> {
1139 // SAFETY: Thread-local storage is only accessed from the current thread.
1140 // We use UnsafeCell to avoid RefCell's runtime borrow checking overhead.
1141 // The closure `f` does not recursively call with_read_context, so there's no aliasing.
1142 READ_CONTEXTS.with(|cache| {
1143 let cache = unsafe { &mut *cache.get() };
1144 let id = self.id;
1145
1146 let context = cache.get_or_insert_result(id, || {
1147 // Only fetch type resolver when creating a new context
1148 let type_resolver = self.get_final_type_resolver()?;
1149 Ok(Box::new(ReadContext::new(
1150 type_resolver.clone(),
1151 self.config.clone(),
1152 )))
1153 })?;
1154 f(context)
1155 })
1156 }
1157
1158 #[inline(always)]
1159 fn deserialize_with_context<S: Serializer>(
1160 &self,
1161 context: &mut ReadContext,
1162 ) -> Result<S::Target, Error> {
1163 let result = self.deserialize_with_context_inner::<S>(context);
1164 context.reset();
1165 result
1166 }
1167
1168 #[inline(always)]
1169 fn deserialize_with_context_inner<S: Serializer>(
1170 &self,
1171 context: &mut ReadContext,
1172 ) -> Result<S::Target, Error> {
1173 self.read_head(&mut context.reader)?;
1174 // Use RefMode based on config:
1175 // - If track_ref is enabled, use RefMode::Tracking for the root object
1176 // - Otherwise, use RefMode::NullOnly
1177 let ref_mode = if self.config.track_ref {
1178 RefMode::Tracking
1179 } else {
1180 RefMode::NullOnly
1181 };
1182 let result = S::read(context, ref_mode, true);
1183 context.ref_reader.resolve_callbacks();
1184 result
1185 }
1186
1187 #[inline(always)]
1188 fn read_head(&self, reader: &mut Reader) -> Result<(), Error> {
1189 let bitmap = reader.read_u8()?;
1190 let expected = if self.config.xlang {
1191 IS_CROSS_LANGUAGE_FLAG
1192 } else {
1193 0
1194 };
1195 if bitmap != expected {
1196 return self.read_head_slow(bitmap, expected);
1197 }
1198 Ok(())
1199 }
1200
1201 #[cold]
1202 #[inline(never)]
1203 fn read_head_slow(&self, bitmap: u8, expected: u8) -> Result<(), Error> {
1204 const KNOWN_FLAGS: u8 = IS_CROSS_LANGUAGE_FLAG | IS_OUT_OF_BAND_FLAG;
1205 ensure!(
1206 (bitmap & !KNOWN_FLAGS) == 0 && (bitmap & IS_OUT_OF_BAND_FLAG) == 0,
1207 Error::invalid_data("unsupported root header bitmap")
1208 );
1209 ensure!(
1210 (bitmap & IS_CROSS_LANGUAGE_FLAG) == (expected & IS_CROSS_LANGUAGE_FLAG),
1211 Error::invalid_data("header bitmap mismatch at xlang bit")
1212 );
1213 Ok(())
1214 }
1215}
1216
1217#[cfg(test)]
1218mod tests {
1219 use super::Fory;
1220
1221 #[test]
1222 fn compatible_defaults_and_overrides() {
1223 let default_xlang = Fory::builder().xlang(true).finish_config();
1224 let default_native = Fory::builder().xlang(false).finish_config();
1225 let explicit_same_schema = Fory::builder()
1226 .compatible(false)
1227 .xlang(true)
1228 .finish_config();
1229 let explicit_same_schema_reverse_order = Fory::builder()
1230 .xlang(true)
1231 .compatible(false)
1232 .finish_config();
1233
1234 assert!(default_xlang.compatible);
1235 assert!(default_xlang.share_meta);
1236 assert!(!default_xlang.check_struct_version);
1237 assert!(default_native.compatible);
1238 assert!(default_native.share_meta);
1239 assert!(!default_native.check_struct_version);
1240
1241 assert!(!explicit_same_schema.compatible);
1242 assert!(!explicit_same_schema.share_meta);
1243 assert!(explicit_same_schema.check_struct_version);
1244 assert!(!explicit_same_schema_reverse_order.compatible);
1245 assert!(!explicit_same_schema_reverse_order.share_meta);
1246 assert!(explicit_same_schema_reverse_order.check_struct_version);
1247 }
1248}