Skip to main content

ForyBuilder

Struct ForyBuilder 

Source
pub struct ForyBuilder { /* private fields */ }
Expand description

Builder for configuring a Fory runtime before first use.

ForyBuilder owns the configuration phase. Call build to create the runtime, then use Fory for registration and serialization operations.

use fory_core::Fory;

let fory = Fory::builder()
    .xlang(true)
    .compress_string(true)
    .max_dyn_depth(10)
    .build();

Implementations§

Source§

impl ForyBuilder

Source

pub fn compatible(self, compatible: bool) -> ForyBuilder

Sets the serialization compatible mode for this Fory builder.

§Arguments
  • compatible - The serialization compatible mode to use. Options are:
    • false: Schema must be consistent between serialization and deserialization. No metadata is shared. This is the fastest mode.
    • true: Supports schema evolution and type metadata sharing for better cross-version compatibility.
§Returns

Returns self for method chaining.

§Note

Setting the compatible mode also automatically configures the share_meta flag:

  • falseshare_meta = false
  • trueshare_meta = true
§Examples
use fory_core::Fory;

let fory = Fory::builder().xlang(true).compatible(true).build();
Source

pub fn xlang(self, xlang: bool) -> ForyBuilder

Enables or disables xlang mode.

§Arguments
  • xlang - If true, uses the xlang wire format compatible with other Fory implementations (Java, Python, C++, etc.). If false, uses Rust native mode.
§Returns

Returns self for method chaining.

§Default

The default value is true.

§Examples
use fory_core::Fory;

// Xlang mode, the default cross-language wire format
let fory = Fory::builder().xlang(true).build();

// Native mode for Rust-only traffic
let fory = Fory::builder().xlang(false).build();
Source

pub fn compress_string(self, compress_string: bool) -> ForyBuilder

Enables or disables meta string compression.

§Arguments
  • compress_string - If true, enables meta string compression to reduce serialized payload size by deduplicating and encoding frequently used strings (such as type names and field names). If false, strings are serialized without compression.
§Returns

Returns self for method chaining.

§Default

The default value is false.

§Trade-offs
  • Enabled: Smaller payload size, slightly higher CPU overhead
  • Disabled: Larger payload size, faster serialization/deserialization
§Examples
use fory_core::Fory;

let fory = Fory::builder().xlang(true).compress_string(true).build();
Source

pub fn check_string_read(self, check_string_read: bool) -> ForyBuilder

Enables or disables checked UTF-8 string reads.

Checked reads validate UTF-8 payload bytes before constructing Rust String values. Disabling this keeps the faster unchecked construction path and must only be used when serialized bytes are trusted to contain valid UTF-8 strings.

§Default

The default value is true.

Source

pub fn check_struct_version(self, check_struct_version: bool) -> ForyBuilder

Enables or disables class version checking for schema consistency.

§Arguments
  • check_struct_version - If true, enables class version checking to ensure schema consistency between serialization and deserialization. When enabled, a version hash computed from field types is written/read to detect schema mismatches. If false, no version checking is performed.
§Returns

Returns self for method chaining.

§Default

The default value is false.

§Note

This feature is only effective when compatible mode is false. In compatible mode, schema evolution is supported and version checking is not needed.

§Examples
use fory_core::Fory;

let fory = Fory::builder().xlang(false)
    .compatible(false)
    .check_struct_version(true)
    .build();
Source

pub fn track_ref(self, track_ref: bool) -> ForyBuilder

Enables or disables reference tracking for shared and circular references.

§Arguments
  • track_ref - If true, enables reference tracking which allows preserving shared object references and circular references during serialization/deserialization.
§Returns

Returns self for method chaining.

§Default

The default value is false.

§Examples
use fory_core::Fory;

let fory = Fory::builder().xlang(true).track_ref(true).build();
Source

pub fn max_dyn_depth(self, max_dyn_depth: u32) -> ForyBuilder

Sets the maximum depth for nested dynamic object serialization.

§Arguments
  • max_dyn_depth - The maximum nesting depth allowed for dynamically-typed objects (e.g., trait objects, boxed types). This prevents stack overflow from deeply nested structures in dynamic serialization scenarios.
§Returns

Returns self for method chaining.

§Default

The default value is 5.

§Behavior

When the depth limit is exceeded during deserialization, an error is returned to prevent potential stack overflow or infinite recursion.

§Examples
use fory_core::Fory;

// Allow deeper nesting for complex object graphs
let fory = Fory::builder().xlang(true).max_dyn_depth(10).build();

// Restrict nesting for safer deserialization
let fory = Fory::builder().xlang(true).max_dyn_depth(3).build();
Source

pub fn max_binary_size(self, max_binary_size: u32) -> ForyBuilder

Sets the maximum allowed size for binary data during deserialization.

§Arguments
  • max_binary_size - The maximum number of bytes allowed for a single binary/primitive-array payload during deserialization. Payloads exceeding this limit will cause a SizeLimitExceeded error.
§Returns

Returns self for method chaining.

§Default

The default value is 64 * 1024 * 1024 (64 MB).

§Examples
use fory_core::Fory;

// Limit binary payloads to 1 MB
let fory = Fory::builder().xlang(true).max_binary_size(1024 * 1024).build();
Source

pub fn max_collection_size(self, max_collection_size: u32) -> ForyBuilder

Sets the maximum allowed number of elements in a collection or entries in a map during deserialization.

§Arguments
  • max_collection_size - The maximum number of elements/entries allowed for a single collection or map during deserialization. Payloads exceeding this limit will cause a SizeLimitExceeded error.
§Returns

Returns self for method chaining.

§Default

The default value is 1024 * 1024 (1 million elements).

§Examples
use fory_core::Fory;

// Limit collections to 10000 elements
let fory = Fory::builder().xlang(true).max_collection_size(10000).build();
Source

pub fn build(self) -> Fory

Builds a Fory runtime with the current builder configuration.

Trait Implementations§

Source§

impl Default for ForyBuilder

Source§

fn default() -> ForyBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.