https://github.com/kkharji/redis-derive
This crate implements the FromRedisValue and ToRedisArgs Traits from mitsuhiko / redis-rs for any struct
https://github.com/kkharji/redis-derive
crates redis rust rust-proc-macro serde serde-redis
Last synced: 5 months ago
JSON representation
This crate implements the FromRedisValue and ToRedisArgs Traits from mitsuhiko / redis-rs for any struct
- Host: GitHub
- URL: https://github.com/kkharji/redis-derive
- Owner: kkharji
- License: mit
- Created: 2022-02-28T09:55:09.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-02-24T04:08:50.000Z (over 2 years ago)
- Last Synced: 2025-04-25T18:13:52.952Z (5 months ago)
- Topics: crates, redis, rust, rust-proc-macro, serde, serde-redis
- Language: Rust
- Homepage: https://docs.rs/redis-derive/
- Size: 3.86 MB
- Stars: 19
- Watchers: 3
- Forks: 4
- Open Issues: 5
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# redis-derive
This crate implements the `FromRedisValue`(redis::FromRedisValue) and `ToRedisArgs`(redis::ToRedisArgs) traits from `mitsuhiko / redis-rs`(https://github.com/mitsuhiko/redis-rs) for any struct,
this allows a seaming less type conversion between rust structs and Redis hash sets.This is more beneficial than JSON encoding the struct and storing the result in a Redis key because when saving as a Redis hash set,
sorting algorithms can be performed without having to move data out of the database.There is also the benefit of being able to retrieve just one value of the struct in the database.
Initial development was done by @Michaelvanstraten 🙏🏽.
## Usage and Examples
To use this crate at it to your dependencies and import the following to procedural macros.
```rust
use redis_derive::{FromRedisValue, ToRedisArgs};
```Now the these Marcos can be used to implement the traits `FromRedisValue`(redis::FromRedisValue) and `ToRedisArgs`(redis::ToRedisArgs) for your decorated struct.
```rust
#[derive(ToRedisArgs, FromRedisValue)]
struct MySuperCoolStruct {
first_field : String,
second_field : Option,
third_field : Vec
}```
These Procedural macros work for any struct in which every field's type also implements `ToRedisArgs`(redis::ToRedisArgs) so this would be allowed:
```rust
#[derive(ToRedisArgs, FromRedisVaule)]
struct MySuperCoolStruct {
first_field : String,
second_field : Option,
third_field : Vec
}#[derive(ToRedisArgs, FromRedisVaule)]
struct MySecondSuperCoolStruct {
fourth_field : String,
inner_struct : MySuperCoolStruct
}
```
### Complete Example
```rust
use redis::Commands;
use redis_derive::{FromRedisValue, ToRedisArgs};#[derive(FromRedisValue, ToRedisArgs, Debug)]
struct MySuperCoolStruct {
first_field : String,
second_field : Option,
third_field : Vec
}fn main() -> redis::RedisResult<()> {
let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_connection()?;let test1 = MySuperCoolStruct{
first_field : "Hello World".to_owned(),
second_field : Some(42),
third_field : vec!["abc".to_owned(), "cba".to_owned()]
};let _ = redis::cmd("HSET")
.arg("test1")
.arg(&test1)
.query(&mut con)?;let db_test1 : MySuperCoolStruct = con.hgetall("test1")?;
println!("send : {:#?}, got : {:#?}", test1, db_test1);
Ok(())
}
```License: MIT OR Apache-2.0