1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//! # Fory Core
//!
//! This is the core implementation of the Fory serialization framework.
//! It provides the fundamental building blocks for high-performance serialization
//! and deserialization in Rust.
//!
//! ## Architecture
//!
//! The core library is organized into several key modules:
//!
//! - **`fory`**: Main serialization engine and public API
//! - **`buffer`**: Efficient binary buffer management with Reader/Writer
//! - **`row`**: Row-based serialization for zero-copy operations
//! - **`serializer`**: Type-specific serialization implementations
//! - **`resolver`**: Type resolution and metadata management
//! - **`meta`**: Metadata handling for schema evolution
//! - **`types`**: Core type definitions and constants
//! - **`error`**: Error handling and result types
//! - **`util`**: Utility functions and helpers
//!
//! ## Key Concepts
//!
//! ### Serialization Modes
//!
//! Fory supports two serialization modes:
//!
//! - **SchemaConsistent**: Requires exact type matching between peers
//! - **Compatible**: Allows schema evolution with field additions/deletions
//!
//! ### Type System
//!
//! The framework uses a comprehensive type system that supports:
//! - Primitive types (bool, integers, floats, strings)
//! - Collections (Vec, HashMap, BTreeMap)
//! - Optional types (`Option<T>`)
//! - Date/time types (chrono integration)
//! - Custom structs and enums
//! - Trait objects (Box, Rc, Arc)
//!
//! ### Performance Optimizations
//!
//! - **Zero-copy deserialization** in row mode
//! - **Buffer pre-allocation** to minimize allocations
//! - **Variable-length encoding** for compact representation
//! - **Little-endian byte order** for cross-platform compatibility
//!
//! ### Trait Object Serialization
//!
//! Fory supports polymorphic serialization through trait objects:
//!
//! #### Box-Based Trait Objects
//!
//! Define custom traits and register implementations:
//!
//! ```rust,ignore
//! use fory_core::{Fory, register_trait_type, Serializer};
//! use fory_derive::ForyObject;
//!
//! trait Animal: Serializer {
//! fn speak(&self) -> String;
//! }
//!
//! #[derive(ForyObject, Debug)]
//! struct Dog { name: String }
//!
//! #[derive(ForyObject, Debug)]
//! struct Cat { name: String }
//!
//! impl Animal for Dog {
//! fn speak(&self) -> String { "Woof!".to_string() }
//! }
//!
//! impl Animal for Cat {
//! fn speak(&self) -> String { "Meow!".to_string() }
//! }
//!
//! register_trait_type!(Animal, Dog, Cat);
//!
//! #[derive(ForyObject)]
//! struct Zoo {
//! star_animal: Box<dyn Animal>,
//! }
//!
//! # fn main() {
//! let mut fory = Fory::default().compatible(true);
//! fory.register::<Dog>(100);
//! fory.register::<Cat>(101);
//! fory.register::<Zoo>(102);
//!
//! let zoo = Zoo {
//! star_animal: Box::new(Dog { name: "Buddy".to_string() }),
//! };
//!
//! let bytes = fory.serialize(&zoo);
//! let decoded: Zoo = fory.deserialize(&bytes).unwrap();
//! assert_eq!(decoded.star_animal.speak(), "Woof!");
//! # }
//! ```
//!
//! #### Rc/Arc-Based Trait Objects
//!
//! For reference-counted trait objects, use them directly in struct fields:
//!
//! ```rust,ignore
//! # use fory_core::Serializer;
//! # use fory_derive::ForyObject;
//! # use std::rc::Rc;
//! # use std::sync::Arc;
//! # trait Animal: Serializer { fn speak(&self) -> String; }
//! #[derive(ForyObject)]
//! struct Shelter {
//! animals_rc: Vec<Rc<dyn Animal>>,
//! animals_arc: Vec<Arc<dyn Animal>>,
//! }
//! ```
//!
//! For standalone serialization, use auto-generated wrapper types (e.g., `AnimalRc`, `AnimalArc`)
//! created by `register_trait_type!` due to Rust's orphan rule limitations.
//!
//! ## Usage
//!
//! This crate is typically used through the higher-level `fory` crate,
//! which provides derive macros and a more convenient API. However,
//! you can use the core types directly for advanced use cases.
//!
//! ```rust
//! use fory_core::fory::Fory;
//! use fory_core::error::Error;
//! use fory_core::row::{to_row, from_row};
//! use std::collections::HashMap;
//!
//! // Create a Fory instance
//! let mut fory = Fory::default().compatible(true);
//!
//! // Serialize String
//! let text = String::from("Hello, Fory!");
//! let serialized_str = fory.serialize(&text).unwrap();
//! let deserialized_str: String = fory.deserialize(&serialized_str).unwrap();
//! assert_eq!(text, deserialized_str);
//!
//! // Serialize Vec
//! let vec_data = vec![1, 2, 3, 4, 5];
//! let serialized_vec = fory.serialize(&vec_data).unwrap();
//! let deserialized_vec: Vec<i32> = fory.deserialize(&serialized_vec).unwrap();
//! assert_eq!(vec_data, deserialized_vec);
//!
//! // Serialize HashMap
//! let mut map = HashMap::new();
//! map.insert("key1".to_string(), 100);
//! map.insert("key2".to_string(), 200);
//! let serialized_map = fory.serialize(&map).unwrap();
//! let deserialized_map: HashMap<String, i32> = fory.deserialize(&serialized_map).unwrap();
//! assert_eq!(map, deserialized_map);
//! // Register types for object serialization
//! // fory.register::<MyStruct>(type_id);
//!
//! // Use row-based serialization for zero-copy operations
//! // let row_data = to_row(&my_data);
//! // let row = from_row::<MyStruct>(&row_data);
//! ```
pub use float16 as Float16;
// Re-export paste for use in macros
pub use paste;
pub use crate;
pub use crateConfig;
pub use crateError;
pub use crateFory;
pub use crate;
pub use crate;
pub use crate;
pub use crate;
pub use crate;