tosql_derive : Derive Macro for tosql
Automatically implements the SqlStruct trait for Rust structs, simplifying SQL serialization.
Table of Contents
Introduction
tosql_derive is a procedural macro that generates the boilerplate code required to implement the tosql::SqlStruct trait. It analyzes your struct fields and generates the appropriate serialization logic, mapping Rust types to kind2sql::Kind variants.
Features
- Automatic Derivation: Simply add
#[derive(ToSql)]to your struct. - Type Mapping: Automatically maps Rust primitive types (
u8,i32,String, etc.) to SQL types. - Variable Byte Encoding: Handles efficient encoding for
String,Bytes,u32, andu64. - Seamless Integration: Designed to work perfectly with the
tosqlandkind2sqlecosystem.
Usage
Add tosql and tosql_derive to your dependencies:
[]
= "0.1"
= "0.1"
Then derive ToSql on your struct:
use ToSql;
// Now User implements SqlStruct automatically
// You can use it with kind2sql to generate SQL inserts
Design
The macro parses the struct definition and performs the following mappings:
- Integers: Mapped to their corresponding
Kind(e.g.,u8->Kind::U8).u32andu64are treated as variable-byte encoded integers to save space. - Strings: Mapped to
Kind::String. The length is encoded using variable-byte encoding before the string data. - Bytes:
Vec<u8>,Bytes, andbytes::Bytesare mapped toKind::Bytes.
The generated dump method efficiently writes these values into a BytesMut buffer without unnecessary copying.
Tech Stack
- Rust
syn: For parsing Rust code.quote: For generating Rust code.proc-macro2: For manipulating token streams.
Directory Structure
.
├── Cargo.toml # Package manifest
├── readme/ # Documentation
│ ├── en.md # English README
│ └── zh.md # Chinese README
└── src/
└── lib.rs # Macro implementation
About
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:
tosql_derive : tosql 的派生宏
自动为 Rust 结构体实现 SqlStruct trait,简化 SQL 序列化过程。
目录
简介
tosql_derive 是一个过程宏,用于生成实现 tosql::SqlStruct trait 所需的样板代码。它分析结构体字段并生成相应的序列化逻辑,将 Rust 类型映射到 kind2sql::Kind 变体。
特性
- 自动派生:只需在结构体上添加
#[derive(ToSql)]。 - 类型映射:自动将 Rust 基本类型(
u8、i32、String等)映射到 SQL 类型。 - 变长字节编码:处理
String、Bytes、u32和u64的高效编码。 - 无缝集成:设计用于与
tosql和kind2sql生态系统完美配合。
使用演示
在依赖项中添加 tosql 和 tosql_derive:
[]
= "0.1"
= "0.1"
然后在结构体上派生 ToSql:
use ToSql;
// 现在 User 自动实现了 SqlStruct
// 你可以将其与 kind2sql 结合使用以生成 SQL 插入语句
设计思路
该宏解析结构体定义并执行以下映射:
- 整数:映射到相应的
Kind(例如u8->Kind::U8)。u32和u64被视为变长字节编码整数以节省空间。 - 字符串:映射到
Kind::String。在字符串数据之前使用变长字节编码对长度进行编码。 - 字节:
Vec<u8>、Bytes和bytes::Bytes映射到Kind::Bytes。
生成的 dump 方法将这些值高效地写入 BytesMut 缓冲区,无需不必要的复制。
技术栈
- Rust
syn:用于解析 Rust 代码。quote:用于生成 Rust 代码。proc-macro2:用于操作令牌流。
目录结构
.
├── Cargo.toml # 包配置
├── readme/ # 文档
│ ├── en.md # 英文 README
│ └── zh.md # 中文 README
└── src/
└── lib.rs # 宏实现
关于
本项目为 js0.site ⋅ 重构互联网计划 的开源组件。
我们正在以组件化的方式重新定义互联网的开发范式,欢迎关注: