| Crates.io | tosql_derive |
| lib.rs | tosql_derive |
| version | 0.1.14 |
| created_at | 2025-11-23 13:58:37.192982+00 |
| updated_at | 2025-11-25 11:49:13.857197+00 |
| description | Derive macro for ToSqlTrait trait / ToSqlTrait trait 派生宏 |
| homepage | https://github.com/js0-site/rust/tree/main/tosql_derive |
| repository | https://github.com/js0-site/rust.git |
| max_upload_size | |
| id | 1946594 |
| size | 41,895 |
Automatically implements the ToSqlTrait trait for Rust structs, simplifying SQL serialization.
tosql_derive is a procedural macro that generates the boilerplate code required to implement the tosql::ToSqlTrait trait. It analyzes your struct fields and generates the appropriate serialization logic, mapping Rust types to kind2sql::Kind variants.
#[derive(ToSql)] to your struct.u8, i32, String, etc.) to SQL types.String, Bytes, u32, and u64.tosql and kind2sql ecosystem.Add tosql and tosql_derive to your dependencies:
[dependencies]
tosql = "0.1"
tosql_derive = "0.1"
Then derive ToSql on your struct:
use tosql_derive::ToSql;
#[derive(ToSql)]
struct User {
id: u64,
username: String,
is_active: u8,
}
// Now User implements ToSqlTrait automatically
// You can use it with kind2sql to generate SQL inserts
The macro parses the struct definition and performs the following mappings:
Kind (e.g., u8 -> Kind::U8). u32 and u64 are treated as variable-byte encoded integers to save space.Kind::String. The length is encoded using variable-byte encoding before the string data.Vec<u8>, Bytes, and bytes::Bytes are mapped to Kind::Bytes.The generated dump method efficiently writes these values into a BytesMut buffer without unnecessary copying.
syn: For parsing Rust code.quote: For generating Rust code.proc-macro2: For manipulating token streams..
├── Cargo.toml # Package manifest
├── readme/ # Documentation
│ ├── en.md # English README
│ └── zh.md # Chinese README
└── src/
└── lib.rs # Macro implementation
This project is an open-source component of js0.site ⋅ Refactoring the Internet Plan.
We are redefining the development paradigm of the Internet in a componentized way. Welcome to follow us:
自动为 Rust 结构体实现 ToSqlTrait trait,简化 SQL 序列化过程。
tosql_derive 是一个过程宏,用于生成实现 tosql::ToSqlTrait trait 所需的样板代码。它分析结构体字段并生成相应的序列化逻辑,将 Rust 类型映射到 kind2sql::Kind 变体。
#[derive(ToSql)]。u8、i32、String 等)映射到 SQL 类型。String、Bytes、u32 和 u64 的高效编码。tosql 和 kind2sql 生态系统完美配合。在依赖项中添加 tosql 和 tosql_derive:
[dependencies]
tosql = "0.1"
tosql_derive = "0.1"
然后在结构体上派生 ToSql:
use tosql_derive::ToSql;
#[derive(ToSql)]
struct User {
id: u64,
username: String,
is_active: u8,
}
// 现在 User 自动实现了 ToSqlTrait
// 你可以将其与 kind2sql 结合使用以生成 SQL 插入语句
该宏解析结构体定义并执行以下映射:
Kind(例如 u8 -> Kind::U8)。u32 和 u64 被视为变长字节编码整数以节省空间。Kind::String。在字符串数据之前使用变长字节编码对长度进行编码。Vec<u8>、Bytes 和 bytes::Bytes 映射到 Kind::Bytes。生成的 dump 方法将这些值高效地写入 BytesMut 缓冲区,无需不必要的复制。
syn:用于解析 Rust 代码。quote:用于生成 Rust 代码。proc-macro2:用于操作令牌流。.
├── Cargo.toml # 包配置
├── readme/ # 文档
│ ├── en.md # 英文 README
│ └── zh.md # 中文 README
└── src/
└── lib.rs # 宏实现
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: