Apache Foryβ’ Rust
Apache Foryβ’ is a blazing fast multi-language serialization framework powered by JIT compilation and zero-copy techniques, providing up to ultra-fast performance while maintaining ease of use and safety.
The Rust implementation provides versatile and high-performance serialization with automatic memory management and compile-time type safety.
π Why Apache Foryβ’ Rust?
- π₯ Blazingly Fast: Zero-copy deserialization and optimized binary protocols
- π Cross-Language: Seamlessly serialize/deserialize data across Java, Python, C++, Go, JavaScript, and Rust
- π― Type-Safe: Compile-time type checking with derive macros
- π Circular References: Automatic tracking of shared and circular references with
Rc/Arcand weak pointers - 𧬠Polymorphic: Serialize trait objects with
Box<dyn Trait>,Rc<dyn Trait>, andArc<dyn Trait> - π¦ Schema Evolution: Compatible mode for independent schema changes
- β‘ Two Modes: Object graph serialization and zero-copy row-based format
π¦ Crates
| Crate | Description | Version |
|---|---|---|
fory |
High-level API with derive macros | |
fory-core |
Core serialization engine | |
fory-derive |
Procedural macros |
π Quick Start
Add Apache Foryβ’ to your Cargo.toml:
[]
= "0.13"
Basic Example
use ;
use ForyObject;
π Core Features
1. Object Graph Serialization
Apache Foryβ’ provides automatic serialization of complex object graphs, preserving the structure and relationships between objects. The #[derive(ForyObject)] macro generates efficient serialization code at compile time, eliminating runtime overhead.
Key capabilities:
- Nested struct serialization with arbitrary depth
- Collection types (Vec, HashMap, HashSet, BTreeMap)
- Optional fields with
Option<T> - Automatic handling of primitive types and strings
- Efficient binary encoding with variable-length integers
use ;
use ForyObject;
use HashMap;
let mut fory = default;
fory.;
fory.;
let person = Person ;
let bytes = fory.serialize;
let decoded: Person = fory.deserialize?;
assert_eq!;
2. Shared and Circular References
Apache Foryβ’ automatically tracks and preserves reference identity for shared objects using Rc<T> and Arc<T>. When the same object is referenced multiple times, Fory serializes it only once and uses reference IDs for subsequent occurrences. This ensures:
- Space efficiency: No data duplication in serialized output
- Reference identity preservation: Deserialized objects maintain the same sharing relationships
- Circular reference support: Use
RcWeak<T>andArcWeak<T>to break cycles
Shared References with Rc/Arc
use Fory;
use Rc;
let fory = default;
// Create a shared value
let shared = new;
// Reference it multiple times
let data = vec!;
// The shared value is serialized only once
let bytes = fory.serialize;
let decoded: = fory.deserialize?;
// Verify reference identity is preserved
assert_eq!;
assert_eq!;
// All three Rc pointers point to the same object
assert!;
assert!;
For thread-safe shared references, use Arc<T>.
Circular References with Weak Pointers
To serialize circular references like parent-child relationships or doubly-linked structures, use RcWeak<T> or ArcWeak<T> to break the cycle. These weak pointers are serialized as references to their strong counterparts, preserving the graph structure without causing memory leaks or infinite recursion.
How it works:
- Weak pointers serialize as references to their target objects
- If the strong pointer has been dropped, weak serializes as
Null - Forward references (weak appearing before target) are resolved via callbacks
- All clones of a weak pointer share the same internal cell for automatic updates
use ;
use ForyObject;
use RcWeak;
use Rc;
use RefCell;
let mut fory = default;
fory.;
// Build a parent-child tree
let parent = new;
let child1 = new;
let child2 = new;
parent.borrow_mut.children.push;
parent.borrow_mut.children.push;
// Serialize and deserialize the circular structure
let bytes = fory.serialize;
let decoded: = fory.deserialize?;
// Verify the circular relationship
assert_eq!;
for child in &decoded.borrow.children
Thread-Safe Circular Graphs with Arc:
use ;
use ForyObject;
use ArcWeak;
use ;
let mut fory = default;
fory.;
let parent = new;
let child1 = new;
let child2 = new;
parent.lock.unwrap.children.push;
parent.lock.unwrap.children.push;
let bytes = fory.serialize;
let decoded: = fory.deserialize?;
assert_eq!;
for child in &decoded.lock.unwrap.children
3. Trait Object Serialization
Apache Foryβ’ supports polymorphic serialization through trait objects, enabling dynamic dispatch and type flexibility. This is essential for plugin systems, heterogeneous collections, and extensible architectures.
Supported trait object types:
Box<dyn Trait>- Owned trait objectsRc<dyn Trait>- Reference-counted trait objectsArc<dyn Trait>- Thread-safe reference-counted trait objectsVec<Box<dyn Trait>>,HashMap<K, Box<dyn Trait>>- Collections of trait objects
Basic Trait Object Serialization
use ;
use Serializer;
use ForyObject;
// Register trait implementations
register_trait_type!;
let mut fory = default.compatible;
fory.;
fory.;
fory.;
let zoo = Zoo ;
let bytes = fory.serialize;
let decoded: Zoo = fory.deserialize?;
assert_eq!;
assert_eq!;
Serializing dyn Any Trait Objects
Apache Foryβ’ supports serializing Rc<dyn Any> and Arc<dyn Any> for runtime type dispatch. This is useful when you need maximum flexibility and don't want to define a custom trait.
Key points:
- Works with any type that implements
Serializer - Requires downcasting after deserialization to access the concrete type
- Type information is preserved during serialization
- Useful for plugin systems and dynamic type handling
use Rc;
use Any;
let dog_rc: = new;
// Convert to Rc<dyn Any> for serialization
let dog_any: = dog_rc.clone;
// Serialize the Any wrapper
let bytes = fory.serialize;
let decoded: = fory.deserialize?;
// Downcast back to the concrete type
let unwrapped = decoded..unwrap;
assert_eq!;
For thread-safe scenarios, use Arc<dyn Any>:
use Arc;
use Any;
let dog_arc: = new;
// Convert to Arc<dyn Any>
let dog_any: = dog_arc.clone;
let bytes = fory.serialize;
let decoded: = fory.deserialize?;
// Downcast to concrete type
let unwrapped = decoded..unwrap;
assert_eq!;
Rc/Arc-Based Trait Objects in Structs
For fields with Rc<dyn Trait> or Arc<dyn Trait>, Fory automatically handles the conversion:
use Arc;
use Rc;
use HashMap;
let mut fory = default.compatible;
fory.;
fory.;
fory.;
let shelter = AnimalShelter ;
let bytes = fory.serialize;
let decoded: AnimalShelter = fory.deserialize?;
assert_eq!;
assert_eq!;
Standalone Trait Object Serialization
Due to Rust's orphan rule, Rc<dyn Trait> and Arc<dyn Trait> cannot implement Serializer directly. For standalone serialization (not inside struct fields), the register_trait_type! macro generates wrapper types.
Note: If you don't want to use wrapper types, you can serialize as Rc<dyn Any> or Arc<dyn Any> instead (see the dyn Any section above).
The register_trait_type! macro generates AnimalRc and AnimalArc wrapper types:
// For Rc<dyn Trait>
let dog_rc: = new;
let wrapper = from;
let bytes = fory.serialize;
let decoded: AnimalRc = fory.deserialize?;
// Unwrap back to Rc<dyn Animal>
let unwrapped: = decoded.unwrap;
assert_eq!;
// For Arc<dyn Trait>
let dog_arc: = new;
let wrapper = from;
let bytes = fory.serialize;
let decoded: AnimalArc = fory.deserialize?;
let unwrapped: = decoded.unwrap;
assert_eq!;
4. Schema Evolution
Apache Foryβ’ supports schema evolution in Compatible mode, allowing serialization and deserialization peers to have different type definitions. This enables independent evolution of services in distributed systems without breaking compatibility.
Features:
- Add new fields with default values
- Remove obsolete fields (skipped during deserialization)
- Change field nullability (
TβOption<T>) - Reorder fields (matched by name, not position)
- Type-safe fallback to default values for missing fields
Compatibility rules:
- Field names must match (case-sensitive)
- Type changes are not supported (except nullable/non-nullable)
- Nested struct types must be registered on both sides
use Fory;
use ForyObject;
use HashMap;
let mut fory1 = default.compatible;
fory1.;
let mut fory2 = default.compatible;
fory2.;
let person_v1 = PersonV1 ;
// Serialize with V1
let bytes = fory1.serialize;
// Deserialize with V2 - missing fields get default values
let person_v2: PersonV2 = fory2.deserialize?;
assert_eq!;
assert_eq!;
assert_eq!;
5. Enum Support
Apache Foryβ’ supports three types of enum variants with full schema evolution in Compatible mode:
Variant Types:
- Unit: C-style enums (
Status::Active) - Unnamed: Tuple-like variants (
Message::Pair(String, i32)) - Named: Struct-like variants (
Event::Click { x: i32, y: i32 })
Features:
- Efficient varint encoding for variant ordinals
- Schema evolution support (add/remove variants, add/remove fields)
- Default variant support with
#[default] - Automatic type mismatch handling
use ;
let mut fory = default;
fory.?;
let value = Object ;
let bytes = fory.serialize?;
let decoded: Value = fory.deserialize?;
assert_eq!;
Schema Evolution
Compatible mode enables robust schema evolution with variant type encoding (2 bits):
0b0= Unit,0b1= Unnamed,0b10= Named
use ;
// Old version
// New version - added field and variant
let mut fory = builder.compatible.build;
// Serialize with old schema
let old_bytes = fory.serialize?;
// Deserialize with new schema - timestamp gets default value (0)
let new_event: NewEvent = fory.deserialize?;
assert!;
Evolution capabilities:
- Unknown variants β Falls back to default variant
- Named variant fields β Add/remove fields (missing fields use defaults)
- Unnamed variant elements β Add/remove elements (extras skipped, missing use defaults)
- Variant type mismatches β Automatically uses default value for current variant
Best practices:
- Always mark a default variant with
#[default] - Named variants provide better evolution than unnamed
- Use compatible mode for cross-version communication
6. Tuple Support
Apache Foryβ’ supports tuples up to 22 elements out of the box with efficient serialization in both compatible and non-compatible modes.
Features:
- Automatic serialization for tuples from 1 to 22 elements
- Heterogeneous type support (each element can be a different type)
- Schema evolution in Compatible mode (handles missing/extra elements)
Serialization modes:
- Non-compatible mode: Serializes elements sequentially without collection headers for minimal overhead
- Compatible mode: Uses collection protocol with type metadata for schema evolution
use ;
let mut fory = default;
// Tuple with heterogeneous types
let data: = ;
let bytes = fory.serialize?;
let decoded: = fory.deserialize?;
assert_eq!;
7. Custom Serializers
For types that don't support #[derive(ForyObject)], implement the Serializer trait manually. This is useful for:
- External types from other crates
- Types with special serialization requirements
- Legacy data format compatibility
- Performance-critical custom encoding
use ;
use Any;
let mut fory = default;
fory.;
let custom = CustomType ;
let bytes = fory.serialize;
let decoded: CustomType = fory.deserialize?;
assert_eq!;
7. Row-Based Serialization
Apache Foryβ’ provides a high-performance row format for zero-copy deserialization. Unlike traditional object serialization that reconstructs entire objects in memory, row format enables random access to fields directly from binary data without full deserialization.
Key benefits:
- Zero-copy access: Read fields without allocating or copying data
- Partial deserialization: Access only the fields you need
- Memory-mapped files: Work with data larger than RAM
- Cache-friendly: Sequential memory layout for better CPU cache utilization
- Lazy evaluation: Defer expensive operations until field access
When to use row format:
- Analytics workloads with selective field access
- Large datasets where only a subset of fields is needed
- Memory-constrained environments
- High-throughput data pipelines
- Reading from memory-mapped files or shared memory
How it works:
- Fields are encoded in a binary row with fixed offsets for primitives
- Variable-length data (strings, collections) stored with offset pointers
- Null bitmap tracks which fields are present
- Nested structures supported through recursive row encoding
use ;
use ForyRow;
use BTreeMap;
let profile = UserProfile ;
// Serialize to row format
let row_data = to_row;
// Zero-copy deserialization - no object allocation!
let row = ;
// Access fields directly from binary data
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
// Access collections efficiently
let scores = row.scores;
assert_eq!;
assert_eq!;
assert_eq!;
let prefs = row.preferences;
assert_eq!;
assert_eq!;
assert_eq!;
Performance comparison:
| Operation | Object Format | Row Format |
|---|---|---|
| Full deserialization | Allocates all objects | Zero allocation |
| Single field access | Full deserialization required | Direct offset read |
| Memory usage | Full object graph in memory | Only accessed fields in memory |
| Suitable for | Small objects, full access | Large objects, selective access |
8. Thread-Safe Serialization
Apache Foryβ’ Rust is fully thread-safe: Fory implements both Send and Sync, so one configured instance can be shared across threads for concurrent work. The internal read/write context pools are lazily initialized with thread-safe primitives, letting worker threads reuse buffers without coordination.
use ;
use ForyObject;
use Arc;
use thread;
Tip: Perform registrations (such as fory.register::<T>(id)) before spawning threads so every worker sees the same metadata. Once configured, wrapping the instance in Arc is enough to fan out serialization and deserialization tasks safely.
π§ Supported Types
Primitive Types
| Rust Type | Description |
|---|---|
bool |
Boolean |
i8, i16, i32, i64 |
Signed integers |
f32, f64 |
Floating point |
String |
UTF-8 string |
Collections
| Rust Type | Description |
|---|---|
Vec<T> |
Dynamic array |
VecDeque<T> |
Double-ended queue |
LinkedList<T> |
Doubly-linked list |
HashMap<K, V> |
Hash map |
BTreeMap<K, V> |
Ordered map |
HashSet<T> |
Hash set |
BTreeSet<T> |
Ordered set |
BinaryHeap<T> |
Binary heap |
Option<T> |
Optional value |
Smart Pointers
| Rust Type | Description |
|---|---|
Box<T> |
Heap allocation |
Rc<T> |
Reference counting (shared refs tracked) |
Arc<T> |
Thread-safe reference counting (shared refs tracked) |
RcWeak<T> |
Weak reference to Rc<T> (breaks circular refs) |
ArcWeak<T> |
Weak reference to Arc<T> (breaks circular refs) |
RefCell<T> |
Interior mutability (runtime borrow checking) |
Mutex<T> |
Thread-safe interior mutability |
Date and Time
| Rust Type | Description |
|---|---|
chrono::NaiveDate |
Date without timezone |
chrono::NaiveDateTime |
Timestamp without timezone |
Custom Types
| Macro | Description |
|---|---|
#[derive(ForyObject)] |
Object graph serialization |
#[derive(ForyRow)] |
Row-based serialization |
π Cross-Language Serialization
Apache Foryβ’ supports seamless data exchange across multiple languages:
use Fory;
// Enable cross-language mode
let mut fory = default
.compatible
.xlang;
// Register types with consistent IDs across languages
fory.;
// Or use namespace-based registration
fory.;
See xlang_type_mapping.md for type mapping across languages.
β‘ Performance
Apache Foryβ’ Rust is designed for maximum performance:
- Zero-Copy Deserialization: Row format enables direct memory access without copying
- Buffer Pre-allocation: Minimizes memory allocations during serialization
- Compact Encoding: Variable-length encoding for space efficiency
- Little-Endian: Optimized for modern CPU architectures
- Reference Deduplication: Shared objects serialized only once
Run benchmarks:
π Documentation
- API Documentation - Complete API reference
- Protocol Specification - Serialization protocol details
- Type Mapping - Cross-language type mappings
π― Use Cases
Object Serialization
- Complex data structures with nested objects and references
- Cross-language communication in microservices
- General-purpose serialization with full type safety
- Schema evolution with compatible mode
- Graph-like data structures with circular references
Row-Based Serialization
- High-throughput data processing
- Analytics workloads requiring fast field access
- Memory-constrained environments
- Real-time data streaming applications
- Zero-copy scenarios
ποΈ Architecture
The Rust implementation consists of three main crates:
fory/ # High-level API
βββ src/lib.rs # Public API exports
fory-core/ # Core serialization engine
βββ src/
β βββ fory.rs # Main serialization entry point
β βββ buffer.rs # Binary buffer management
β βββ serializer/ # Type-specific serializers
β βββ resolver/ # Type resolution and metadata
β βββ meta/ # Meta string compression
β βββ row/ # Row format implementation
β βββ types.rs # Type definitions
fory-derive/ # Procedural macros
βββ src/
β βββ object/ # ForyObject macro
β βββ fory_row.rs # ForyRow macro
π Serialization Modes
Apache Foryβ’ supports two serialization modes:
SchemaConsistent Mode (Default)
Type declarations must match exactly between peers:
let fory = default; // SchemaConsistent by default
Compatible Mode
Allows independent schema evolution:
let fory = default.compatible;
βοΈ Configuration
Maximum Dynamic Object Nesting Depth
Apache Foryβ’ provides protection against stack overflow from deeply nested dynamic objects during deserialization. By default, the maximum nesting depth is set to 5 levels for trait objects and containers.
Default configuration:
let fory = default; // max_dyn_depth = 5
Custom depth limit:
let fory = default.max_dyn_depth; // Allow up to 10 levels
When to adjust:
- Increase: For legitimate deeply nested data structures
- Decrease: For stricter security requirements or shallow data structures
Protected types:
Box<dyn Any>,Rc<dyn Any>,Arc<dyn Any>Box<dyn Trait>,Rc<dyn Trait>,Arc<dyn Trait>(trait objects)RcWeak<T>,ArcWeak<T>- Collection types (Vec, HashMap, HashSet)
- Nested struct types in Compatible mode
Note: Static data types (non-dynamic types) are secure by nature and not subject to depth limits, as their structure is known at compile time.
π§ͺ Troubleshooting
- Type registry errors: An error like
TypeId ... not found in type_info registrymeans the type was never registered with the currentForyinstance. Confirm that every serializable struct or trait implementation callsfory.register::<T>(type_id)before serialization and that the same IDs are reused on the deserialize side. - Quick error lookup: Prefer the static constructors on
fory_core::error::Error(Error::type_mismatch,Error::invalid_data,Error::unknown, etc.) rather than instantiating variants manually. This keeps diagnostics consistent and makes opt-in panics work. - Panic on error for backtraces: Toggle
FORY_PANIC_ON_ERROR=1(ortrue) alongsideRUST_BACKTRACE=1when running tests or binaries to panic at the exact site an error is constructed. Reset the variable afterwards to avoid aborting user-facing code paths. - Struct field tracing: Add the
#[fory(debug)]attribute (or#[fory(debug = true)]) alongside#[derive(ForyObject)]to tell the macro to emit hook invocations for that type. Once compiled with debug hooks, callset_before_write_field_func,set_after_write_field_func,set_before_read_field_func, orset_after_read_field_func(fromfory-core/src/serializer/struct_.rs) to plug in custom callbacks, and usereset_struct_debug_hooks()when you want the defaults back. - Lightweight logging: Without custom hooks, enable
ENABLE_FORY_DEBUG_OUTPUT=1to print field-level read/write events emitted by the default hook functions. This is especially useful when investigating alignment or cursor mismatches. - Test-time hygiene: Some integration tests expect
FORY_PANIC_ON_ERRORto remain unset. Export it only for focused debugging sessions, and prefercargo test --features tests -p tests --test <case>when isolating failing scenarios.
π οΈ Development
Building
Testing
# Run all tests
# Run specific test
Code Quality
# Format code
# Check formatting
# Run linter
πΊοΈ Roadmap
- Static codegen based on rust macro
- Row format serialization
- Cross-language object graph serialization
- Shared and circular reference tracking
- Weak pointer support
- Trait object serialization with polymorphism
- Schema evolution in compatible mode
- SIMD optimizations for string encoding
- Cross-language support for shared and circular reference tracking
- Cross-language support for trait objects
- Performance optimizations
- More comprehensive benchmarks
π License
Licensed under the Apache License, Version 2.0. See LICENSE for details.
π€ Contributing
We welcome contributions! Please see our Contributing Guide for details.
π Support
- Documentation: docs.rs/fory
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Slack: Apache Fory Slack
Apache Foryβ’ - Blazingly fast multi-language serialization framework.