stream_resp
StreamRESP is a RESP (Redis Serialization Protocol) parser fully compliant with RESP3, implemented using a finite state machine (FSM) approach. Designed for streaming scenarios.
- Full RESP3 support: All RESP3 types are supported.
- Optional explicit positive integer sign: Enable the
explicit-positive-signfeature to support parsing integers with an explicit+sign (e.g.,:+123\r\n).
Documentation
Installation
To use stream_resp in your project, add the following to your Cargo.toml:
[]
= "1"
Features
Enabling jemalloc
If you want to enable jemalloc for better memory allocation performance, you can enable the jemalloc feature in your Cargo.toml:
[]
= { = "1", = ["jemalloc"] }
Enabling Explicit Positive Integer Sign (+)
By default, the parser strictly adheres to the RESP specification for integers (:<number>\r\n). However, some clients or protocols might use an explicit plus sign (:+<number>\r\n). To enable parsing of integers with an explicit +, enable the integer-plus-sign feature:
[]
= { = "1", = ["explicit-positive-sign"] }
You can also enable multiple features:
[]
= { = "1", = ["jemalloc", "explicit-positive-sign"] }
Usage
Here are some examples demonstrating how to use the stream_resp parser.
Example 1: Basic Usages
Creating RESP Values using From
use Cow;
use RespValue;
// From String/&str (becomes SimpleString)
let simple_str: RespValue = "OK".into;
assert_eq!;
let simple_string: RespValue = Stringfrom.into;
assert_eq!;
// From i64
let integer: RespValue = 123.into;
assert_eq!;
// From Option<String> (becomes BulkString)
let bulk_some: RespValue = Some.into;
assert_eq!;
let bulk_none: RespValue = None::.into;
assert_eq!;
// From Vec<RespValue> (becomes Array)
let array: RespValue = vec!.into;
assert_eq!;
// Other From implementations exist for bool, f64, Vec<(RespValue, RespValue)> (Map), etc.
Representing Redis Commands
use Cow;
use RespValue;
// Representing the Redis command: SET mykey "Hello"
let command = Array;
// Get the RESP byte representation
let expected_bytes = b"*3\r\n$3\r\nSET\r\n$5\r\nmykey\r\n$5\r\nHello\r\n";
assert_eq!;
println!;
println!;
Converting RESP Values using Into
use Cow;
use RespValue;
let simple_string = SimpleString;
let ok_str: String = simple_string.into;
assert_eq!;
let integer = Integer;
let num: i64 = integer.into;
assert_eq!;
Example 2: Streaming RESP Messages over TCP
use ;
use ;
use ;
use RespValue;
Example 3: Parsing Complete RESP Messages
use Cow;
use ;
use RespValue;
Example 4: Parsing Incomplete RESP Messages in Chunks
use Cow;
use ;
use RespValue;
Benchmarks
The project includes performance benchmarks for the RESP parser. To run the benchmarks:
# Run all benchmarks
# Run a specific benchmark group
# Run a specific benchmark test
The benchmark results will be displayed in your terminal. Detailed HTML reports will be generated in the target/criterion directory, which you can open in a web browser to see more detailed performance information.
Viewing Benchmark Results
After running the benchmarks, you can find detailed reports in:
target/criterion/report/index.html
The benchmarks measure parsing performance across various RESP data types and scenarios including:
- Simple strings, errors, integers
- Bulk strings (empty, null, large)
- Arrays (simple, nested, large)
- Real-world commands
- Batched commands
- Chunked parsing
Benchmark results will be displayed in the terminal and detailed HTML reports are generated in the
target/criterion directory.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for details.