TITLE: Basic Serialization/Deserialization with serde_molecule in Rust DESCRIPTION: Demonstrates fundamental `serde_molecule` usage for custom structs. Shows deriving `Serialize`/`Deserialize`, then using `to_vec` for serialization and `from_slice` for deserialization, asserting data integrity. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_0 LANGUAGE: rust CODE: ``` use serde::{Serialize, Deserialize}; use serde_molecule::{to_vec, from_slice}; #[derive(Serialize, Deserialize)] pub struct Table1 { pub f1: u8, pub f2: u16, } let t1 = Table1{f1: 0, f2: 0}; // serialize let bytes = to_vec(&t1, false).unwrap(); // deserialize let t2: Table1 = from_slice(&bytes, false).unwrap(); assert_eq!(t1.f1, t2.f1); ``` ---------------------------------------- TITLE: Basic Serialization and Deserialization with Serde Molecule DESCRIPTION: This snippet demonstrates the fundamental usage of `serde_molecule` for serializing and deserializing a custom Rust struct. It shows how to derive `Serialize` and `Deserialize` traits and use `to_vec` and `from_slice` functions for data conversion. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_0 LANGUAGE: Rust CODE: ``` use serde::{Serialize, Deserialize}; use serde_molecule::{to_vec, from_slice}; #[derive(Serialize, Deserialize)] pub struct Table1 { pub f1: u8, pub f2: u16, } let t1 = Table1{f1: 0, f2: 0}; // serialize let bytes = to_vec(&t1, false).unwrap(); // deserialize let t2: Table1 = from_slice(&bytes, false).unwrap(); assert_eq!(t1.f1, t2.f1); ``` ---------------------------------------- TITLE: Adding serde_molecule and Serde Dependencies to Cargo.toml DESCRIPTION: Illustrates declaring `serde` and `serde_molecule` as dependencies in `Cargo.toml`. Specifies enabling the `derive` feature for `serde`, essential for automatic trait implementation. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_1 LANGUAGE: toml CODE: ``` serde = { version = "???", features = ["derive"] } serde_molecule = { version = "???" } ``` ---------------------------------------- TITLE: Adding Serde Molecule Dependencies to Cargo.toml DESCRIPTION: This TOML snippet illustrates how to add `serde` and `serde_molecule` as dependencies in a Rust project's `Cargo.toml` file, enabling the use of the serialization framework and the Molecule implementation. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_1 LANGUAGE: TOML CODE: ``` serde = { version = "???", features = ["derive"] } serde_molecule = { version = "???" } ``` ---------------------------------------- TITLE: Replace Rust Tuples with Structs for Serde Molecule DESCRIPTION: Demonstrates how to replace a Rust tuple type with a struct when using `serde_molecule` for serialization and deserialization. This approach is recommended due to Serde's limited support for general tuples, ensuring better compatibility and avoiding potential issues. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_8 LANGUAGE: rust CODE: ``` use serde::{Deserialize, Serialize}; // Instead of this: // type MyTuple = (u8, String); // Use this: #[derive(Serialize, Deserialize)] struct MyStruct { field1: u8, field2: String, } ``` ---------------------------------------- TITLE: Using Structs Instead of Tuples for Serde Molecule Serialization in Rust DESCRIPTION: This Rust code snippet demonstrates the recommended approach of using a `struct` instead of a general tuple for data serialization with `serde_molecule`. Due to Serde's internal implementation, general tuples have limited support, while tuple structs and tuple variants are mapped to Molecule tables. Using a struct ensures proper serialization behavior. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_8 LANGUAGE: rust CODE: ``` use serde::{Deserialize, Serialize}; // Instead of this: // type MyTuple = (u8, String); // Use this: #[derive(Serialize, Deserialize)] struct MyStruct { field1: u8, field2: String, } ``` ---------------------------------------- TITLE: Rust to Molecule Type Mapping Reference DESCRIPTION: This table provides a comprehensive mapping between common Rust data types and their corresponding Molecule serialization types, indicating whether they are fixed-size. It serves as a reference for understanding how Rust data structures are represented in the Molecule format. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_2 LANGUAGE: APIDOC CODE: ``` | Rust Type | Molecule Type | Fixed Size | | --------- | ------------- | ---------- | | i8,u8 | byte | yes | | i16, u16, i32, u32, i64, u64, i128, u128 | array | yes | | f32 | array([byte; 4]) | yes | | f64 | array([byte; 8]) | yes | | [u8; N] | array([byte; N]) | yes | | [T; N] | array([T; N]) | yes | | Vec | fixvec | no | | struct | table | no | | #[serde(with = "struct_serde")] | struct | yes | | #[serde(with = "dynvec_serde")] | dynvec | no | | Option | option | no | | enum | union | no | | String | fixvec | no | | BTreeMap | dynvec | no | | HashMap | dynvec | no | | BinaryHeap | fixvec | no | | LinkedList | fixvec | no | | VecDeque | fixvec | no | | HashSet | fixvec | no | | Tuple Struct|table | no | | Tuple Variant|table | no | | Struct Variant|table| no | | Tuple | not supported | N/A| ``` ---------------------------------------- TITLE: Rust to Molecule Type Mapping Reference DESCRIPTION: Provides a comprehensive mapping between Rust data types and their corresponding Molecule serialization types. Includes whether the Molecule type has a fixed size, serving as a reference for `serde_molecule` translations. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_2 LANGUAGE: APIDOC CODE: ``` | Rust Type | Molecule Type | Fixed Size | | --------- | ------------- | ---------- | | i8,u8 | byte | yes | | i16, u16, i32, u32, i64, u64, i128, u128 | array | yes | | f32 | array([byte; 4]) | yes | | f64 | array([byte; 8]) | yes | | [u8; N] | array([byte; N]) | yes | | [T; N] | array([T; N]) | yes | | Vec | fixvec | no | | struct | table | no | | #[serde(with = "struct_serde")] | struct | yes | | #[serde(with = "dynvec_serde")] | dynvec | no | | Option | option | no | | enum | union | no | | String | fixvec | no | | BTreeMap | dynvec | no | | HashMap | dynvec | no | | BinaryHeap | fixvec | no | | LinkedList | fixvec | no | | VecDeque | fixvec | no | | HashSet | fixvec | no | | Tuple Struct|table | no | | Tuple Variant|table | no | | Struct Variant|table| no | | Tuple | not supported | N/A| ``` ---------------------------------------- TITLE: Serializing Dynamic Vectors with dynvec_serde in Rust DESCRIPTION: Demonstrates using `#[serde(with = "dynvec_serde")]` for `Vec`-like containers with non-fixed-size elements. This annotation ensures correct serialization as `dynvec`, overriding the default `fixvec` behavior. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_3 LANGUAGE: rust CODE: ``` use serde_molecule::dynvec_serde; #[derive(Serialize, Deserialize)] struct RawTransaction { // ... #[serde(with = "dynvec_serde")] pub outputs: Vec, } ``` ---------------------------------------- TITLE: Serializing Dynamic-Sized Vectors with dynvec_serde DESCRIPTION: This Rust snippet demonstrates how to use the `#[serde(with = "dynvec_serde")]` attribute to correctly serialize `Vec`-like containers whose elements are not of a fixed size into Molecule's `dynvec` type. This is crucial for handling variable-length data structures. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_3 LANGUAGE: Rust CODE: ``` use serde_molecule::dynvec_serde; #[derive(Serialize, Deserialize)] struct RawTransaction { // ... #[serde(with = "dynvec_serde")] pub outputs: Vec, } ``` ---------------------------------------- TITLE: Molecule Schema for Map Types DESCRIPTION: This snippet defines the Molecule schema for representing Rust map types like `BTreeMap` and `HashMap`. It shows how maps are structured as a `vector` of `MapEntry` tables, each containing a `key` and `value`, though `HashMap` is not recommended due to arbitrary order. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_5 LANGUAGE: APIDOC CODE: ``` table MapEntry { key: KEY_TYPE, value: VALUE_TYPE, } vector Map ; ``` ---------------------------------------- TITLE: Serializing Molecule Structs with struct_serde in Rust DESCRIPTION: Illustrates `#[serde(with = "struct_serde")]` to explicitly mark a field as a Molecule `struct`. This is crucial for correct serialization when a field corresponds to a fixed-size Molecule struct, as fields default to `table`s. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_4 LANGUAGE: rust CODE: ``` use serde_molecule::struct_serde; #[derive(Serialize, Deserialize)] pub struct CellInput { pub since: u64, #[serde(with = "struct_serde")] pub previous_output: OutPoint, } ``` ---------------------------------------- TITLE: Molecule Schema for Rust Map Types DESCRIPTION: Defines the Molecule schema for serializing Rust map types like `BTreeMap` and `HashMap`. Maps are represented as a `vector` of `MapEntry` tables, each containing `key` and `value` fields. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_5 LANGUAGE: APIDOC CODE: ``` table MapEntry { key: KEY_TYPE, value: VALUE_TYPE, } vector Map ; ``` ---------------------------------------- TITLE: Serializing Fixed-Size Structs with struct_serde DESCRIPTION: This Rust snippet illustrates the use of the `#[serde(with = "struct_serde")]` attribute to explicitly mark a field as a Molecule `struct`. This is necessary when a field represents a fixed-size Molecule struct rather than the default `table` type, ensuring correct serialization behavior. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_4 LANGUAGE: Rust CODE: ``` use serde_molecule::struct_serde; #[derive(Serialize, Deserialize)] pub struct CellInput { pub since: u64, #[serde(with = "struct_serde")] pub previous_output: OutPoint, } ``` ---------------------------------------- TITLE: Handling Large Arrays with big_array_serde DESCRIPTION: This Rust snippet shows how to use the `#[serde(with = "big_array_serde")]` attribute to serialize Rust arrays larger than 32 elements, overcoming a limitation in the standard Serde framework. This enables proper serialization of large fixed-size byte arrays into Molecule. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_7 LANGUAGE: Rust CODE: ``` use serde::{Deserialize, Serialize}; use serde_molecule::big_array_serde; #[derive(Serialize, Deserialize)] struct BigArray { f1: u8, #[serde(with = "big_array_serde")] f2: [u8; 33], #[serde(with = "big_array_serde")] f3: [u8; 64], } ``` ---------------------------------------- TITLE: Handling Large Arrays with big_array_serde in Rust DESCRIPTION: Demonstrates `#[serde(with = "big_array_serde")]` for serializing arrays larger than 32 elements. This annotation addresses a standard Serde limitation, enabling `serde_molecule` to correctly handle large fixed-size arrays. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_7 LANGUAGE: rust CODE: ``` use serde::{Deserialize, Serialize}; use serde_molecule::big_array_serde; #[derive(Serialize, Deserialize)] struct BigArray { f1: u8, #[serde(with = "big_array_serde")] f2: [u8; 33], #[serde(with = "big_array_serde")] f3: [u8; 64], } ``` ---------------------------------------- TITLE: Configuring Cargo.toml for no_std Support DESCRIPTION: This TOML snippet provides the necessary `Cargo.toml` configuration to enable `serde_molecule` and `serde` for `no_std` environments. It demonstrates how to disable default features and enable specific features like `alloc` for `serde_molecule` and `derive` for `serde`. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/README.md#_snippet_6 LANGUAGE: TOML CODE: ``` serde_molecule = { version = "x.x.x", default-features = false, features = ["alloc"] } serde = { version = "x.x.x", default-features = false, features = ["derive"] } ``` ---------------------------------------- TITLE: Configuring Cargo.toml for no_std Support in serde_molecule DESCRIPTION: Shows `Cargo.toml` configuration for `no_std` support in `serde_molecule` and `serde`. Involves disabling default features and enabling `alloc` for `serde_molecule` and `derive` for `serde`. SOURCE: https://github.com/xujiandong/serde_molecule/blob/main/serde_molecule/README.md#_snippet_6 LANGUAGE: toml CODE: ``` serde_molecule = { version = "x.x.x", default-features = false, features = ["alloc"] } serde = { version = "x.x.x", default-features = false, features = ["derive"] } ```