Skip to main content

serializer/machine/
mod.rs

1//! DX-Machine: Pure RKYV + LZ4 Implementation
2//!
3//! Binary serialization format using RKYV for zero-copy deserialization
4//! and LZ4 for fast compression.
5//!
6//! ## Format
7//! - **RKYV**: Zero-copy binary serialization
8//! - **LZ4**: Fast compression (enabled by default with `compression` feature)
9//! - **Arena-based**: Flattened value storage to avoid recursive types
10//!
11//! ## Features
12//! - Serializable & deserializable with full round-trip support
13//! - 41-74% smaller than LLM text format
14//! - Zero-copy deserialization for maximum performance
15//! - LZ4 compression for reduced wire size
16//!
17//! ## Usage
18//! ```rust,ignore
19//! use serializer::{llm_to_machine, machine_to_document};
20//!
21//! // Convert LLM format to machine format
22//! let machine = llm_to_machine(llm_text)?;
23//!
24//! // Deserialize back to document
25//! let doc = machine_to_document(&machine)?;
26//! ```
27//! - **DX-SIMD512**: AVX-512 operations
28//!
29//! These are preserved for future use but not currently active to keep the
30//! implementation simple and focused on pure RKYV.
31
32// Core modules (RKYV-based implementation)
33pub mod api;
34pub mod builder;
35pub mod cache;
36pub mod footer;
37pub mod format;
38pub mod header;
39pub mod machine_types;
40pub mod rkyv_compat;
41#[cfg(feature = "serde-compat")]
42pub mod serde_compat;
43pub mod simd;
44pub mod slot;
45pub mod traits;
46pub mod types;
47
48// Advanced optimization modules.
49#[cfg(feature = "arena")]
50pub mod arena;
51pub mod optimized_rkyv;
52// pub mod arena_batch;
53pub mod blocking;
54pub mod compress;
55// pub mod direct_io;
56// pub mod inline;
57pub mod intern;
58#[cfg(feature = "mmap")]
59pub mod mmap;
60// #[cfg(feature = "parallel")]
61// pub mod parallel;
62// pub mod prefetch;
63#[cfg(feature = "mmap")]
64pub mod quantum;
65// pub mod simd512;
66// pub mod static_ser;
67// #[cfg(target_os = "linux")]
68// pub mod io_uring;
69// #[cfg(target_os = "windows")]
70// pub mod iocp;
71// #[cfg(target_os = "macos")]
72// pub mod kqueue;
73
74/// Unified file I/O trait used by machine-format optimization helpers.
75///
76/// Implementations may use blocking or platform-specific I/O internally, but
77/// the exposed contract remains synchronous for deterministic serializer flows.
78pub trait AsyncFileIO: Send + Sync {
79    /// Read an entire file.
80    fn read_sync(&self, path: &std::path::Path) -> std::io::Result<Vec<u8>>;
81
82    /// Write an entire file, creating parent directories when needed.
83    fn write_sync(&self, path: &std::path::Path, data: &[u8]) -> std::io::Result<()>;
84
85    /// Read several files and preserve per-file errors.
86    fn read_batch_sync(
87        &self,
88        paths: &[&std::path::Path],
89    ) -> std::io::Result<Vec<std::io::Result<Vec<u8>>>>;
90
91    /// Write several files and preserve per-file errors.
92    fn write_batch_sync(
93        &self,
94        files: &[(&std::path::Path, &[u8])],
95    ) -> std::io::Result<Vec<std::io::Result<()>>>;
96
97    /// Return the backend name used for diagnostics and tests.
98    fn backend_name(&self) -> &'static str;
99
100    /// Return whether the backend is available on this platform.
101    fn is_available(&self) -> bool;
102}
103
104// Property tests (COMMENTED OUT - use advanced features)
105// #[cfg(test)]
106// mod io_props;
107// #[cfg(test)]
108// mod machine_props;
109// #[cfg(test)]
110// mod safe_deserialize_props;
111
112// Core exports (RKYV-based)
113pub use api::{deserialize, deserialize_batch, serialize, serialize_batch};
114pub use builder::DxMachineBuilder;
115pub use cache::{
116    MACHINE_CACHE_HEADER_LEN, MachineCacheCodec, MachineCacheError, MachineCacheKind,
117    MachineCacheMiss, MachineCachePaths, MachineCacheReceipt, MachineCacheSchema,
118    MachineCacheSource, MachineCacheWriteOptions, access_typed_machine_cache,
119    paths_for_project_cache, source_fingerprint, write_typed_machine_cache,
120};
121#[cfg(feature = "mmap")]
122pub use cache::{MappedMachineCache, open_typed_machine_cache};
123#[cfg(feature = "converters")]
124pub use cache::{json_source_to_typed_machine_cache, toml_source_to_typed_machine_cache};
125pub use format::{DxFormat, FormatMode, detect_format, parse_auto};
126pub use header::{
127    DxMachineHeader, FLAG_HAS_HEAP, FLAG_HAS_INTERN, FLAG_HAS_LENGTH_TABLE, FLAG_LITTLE_ENDIAN,
128};
129#[cfg(feature = "serde-compat")]
130pub use serde_compat::{from_bytes as from_bytes_serde, to_bytes as to_bytes_serde};
131pub use slot::{DxMachineSlot, HEAP_MARKER, INLINE_MARKER, MAX_INLINE_SIZE};
132pub use traits::{DxMachineDeserialize, DxMachineSerialize};
133pub use types::DxMachineError;
134
135// Advanced optimization exports.
136#[cfg(feature = "arena")]
137pub use optimized_rkyv::ArenaRkyv;
138#[cfg(feature = "compression")]
139pub use optimized_rkyv::CompressedRkyv;
140#[cfg(feature = "mmap")]
141pub use optimized_rkyv::MmapRkyv;
142pub use optimized_rkyv::OptimizedRkyv;
143// pub use arena::{DxArena, DxArenaPool, DxBatchBuilder};
144// pub use arena_batch::{DxArenaBatch, DxDeserialize, DxSerialize};
145pub use compress::{CompressionLevel, DxCompressed};
146// pub use direct_io::{
147//     AlignedBuffer, DirectIoConfig, DirectIoWriter, DEFAULT_ALIGNMENT, DEFAULT_DIRECT_IO_THRESHOLD,
148// };
149// pub use footer::{
150//     compute_crc16, deserialize_with_footer, extract_data, DxFooter, FooterError, FOOTER_MAGIC,
151//     FOOTER_SIZE, FOOTER_VERSION,
152// };
153// pub use inline::{DxInlineBytes, DxInlineString, MAX_INLINE_BYTES, MAX_INLINE_STRING};
154pub use intern::{InternError, InternPool, InterningDeserializer, InterningSerializer};
155#[cfg(feature = "mmap")]
156pub use mmap::{DxMmap, DxMmapBatch};
157// pub use prefetch::{prefetch, prefetch_lines, prefetch_range, PrefetchHint, PrefetchProcessor};
158// pub use quantum::{QuantumLayout, QuantumReader, QuantumType, QuantumWriter};
159// pub use simd512::runtime::{detect_simd_level, has_avx2, has_avx512, has_sse42, SimdLevel};
160
161/// DX-Machine magic bytes: 0x5A 0x44 ("ZD" little-endian)
162pub const MAGIC: [u8; 2] = [0x5A, 0x44];
163
164/// DX-Machine format version
165pub const VERSION: u8 = 0x01;
166
167/// Slot size in bytes (16 bytes for inline optimization)
168pub const SLOT_SIZE: usize = 16;
169
170#[cfg(test)]
171mod tests {
172    use super::*;
173
174    #[test]
175    fn test_magic_bytes() {
176        assert_eq!(MAGIC, [0x5A, 0x44]);
177        assert_eq!(VERSION, 0x01);
178    }
179
180    #[test]
181    fn test_slot_size() {
182        assert_eq!(SLOT_SIZE, 16);
183        assert_eq!(MAX_INLINE_SIZE, 14);
184    }
185}