resp-rs
Zero-copy RESP2 and RESP3 protocol parser and serializer for Rust.
A high-performance Rust library for parsing and serializing the Redis Serialization Protocol (RESP), supporting both RESP2 and RESP3.
Features
- Zero-copy parsing -- uses
bytes::Bytesto slice into the input buffer without copying - RESP2 and RESP3 -- full support for both protocol versions with separate frame types
- Streaming parser -- handles partial reads and pipelining for incremental TCP data
- High performance -- 4.8-8.0 GB/s throughput in benchmarks
- Minimal dependencies -- only
bytesandthiserror - No async runtime -- pure sync parsing that works in any context
Usage
Add to your Cargo.toml:
[]
= "0.1"
Parse a RESP3 frame
use Bytes;
use resp3;
let data = from;
let = parse_frame.unwrap;
assert_eq!;
assert!;
Parse a RESP2 frame
use Bytes;
use resp2;
let data = from;
let = parse_frame.unwrap;
// frame is Array([BulkString("SET"), BulkString("key"), BulkString("value")])
Serialize a frame
use Bytes;
use ;
let frame = Array;
let wire = frame_to_bytes;
assert_eq!;
Streaming parser (incremental TCP reads)
use Bytes;
use Parser;
let mut parser = new;
// Feed partial data
parser.feed;
assert!; // incomplete
// Feed the rest
parser.feed;
let frame = parser.next_frame.unwrap.unwrap; // complete!
RESP3 types
RESP3 adds several types beyond RESP2:
use Bytes;
use ;
// Null
let = parse_frame.unwrap;
assert_eq!;
// Boolean
let = parse_frame.unwrap;
assert_eq!;
// Double
let = parse_frame.unwrap;
assert_eq!;
// Map
let data = from;
let = parse_frame.unwrap;
// frame is Map([(SimpleString("key1"), Integer(1)), (SimpleString("key2"), Integer(2))])
Supported RESP3 types
| Type | Tag | Example |
|---|---|---|
| Simple String | + |
+OK\r\n |
| Simple Error | - |
-ERR msg\r\n |
| Integer | : |
:42\r\n |
| Bulk String | $ |
$5\r\nhello\r\n |
| Null | _ |
_\r\n |
| Boolean | # |
#t\r\n |
| Double | , |
,3.14\r\n |
| Big Number | ( |
(123456\r\n |
| Blob Error | ! |
!5\r\nERROR\r\n |
| Verbatim String | = |
=15\r\ntxt:hello world\r\n |
| Array | * |
*2\r\n:1\r\n:2\r\n |
| Map | % |
%1\r\n+k\r\n+v\r\n |
| Set | ~ |
~2\r\n:1\r\n:2\r\n |
| Attribute | | |
|1\r\n+k\r\n+v\r\n |
| Push | > |
>2\r\n+msg\r\n:1\r\n |
Streaming variants (chunked strings, arrays, maps, sets, attributes, pushes) are also fully supported.
Benchmarks
Run benchmarks with:
Minimum Supported Rust Version
The MSRV is 1.85 (Rust 2024 edition).
License
Licensed under either of
at your option.
Contributing
Contributions are welcome. Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.