#redis #serde #serialization #proc-macro

macro redis-derive

This crate implements the redis::FromRedisValue and redis::ToRedisArgs traits from mitsuhiko / redis-rs for any struct

3 releases

0.1.7 Feb 14, 2023
0.1.6 Jul 16, 2022
0.1.5 Mar 1, 2022
0.1.1 Feb 28, 2022

#1308 in Encoding

Download history 430/week @ 2023-12-16 189/week @ 2023-12-23 221/week @ 2023-12-30 291/week @ 2024-01-06 273/week @ 2024-01-13 400/week @ 2024-01-20 333/week @ 2024-01-27 275/week @ 2024-02-03 308/week @ 2024-02-10 178/week @ 2024-02-17 247/week @ 2024-02-24 436/week @ 2024-03-02 236/week @ 2024-03-09 453/week @ 2024-03-16 503/week @ 2024-03-23 333/week @ 2024-03-30

1,609 downloads per month
Used in 2 crates

MIT/Apache

29KB
375 lines

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.

Usage and Examples

To use this crate at it to your dependencies and import the following to procedural macros.

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.

#[derive(ToRedisArgs, FromRedisValue)]
struct MySuperCoolStruct {
    first_field : String,
    second_field : Option<i64>,
    third_field : Vec<String>
}

These Procedural macros work for any struct in which every field's type also implements ToRedisArgs(redis::ToRedisArgs) so this would be allowed:

#[derive(ToRedisArgs, FromRedisVaule)]
struct MySuperCoolStruct {
    first_field : String,
    second_field : Option<i64>,
    third_field : Vec<String>
}

#[derive(ToRedisArgs, FromRedisVaule)]
struct MySecondSuperCoolStruct {
    fourth_field : String,
    inner_struct : MySuperCoolStruct
}

Complete Example

use redis::Commands;
use redis_derive::{FromRedisValue, ToRedisArgs};

#[derive(FromRedisValue, ToRedisArgs, Debug)]
struct MySuperCoolStruct {
    first_field : String,
    second_field : Option<i64>,
    third_field : Vec<String>
}

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(())
}

Problems and future continuations

At this point, enums can not have any fields on them.

Future Continuation

  • implementing a getter and setter for a Redis derived type, I imagine something like this
    #[derive(RedisGetter, RedisSetter)]
    struct MySuperCoolStruct {
        first_field : String,
        second_field : Option<i64>,
        third_field : Vec<String>
    }
    fn somefn() {
        let mut redis_client = /* geting some connection to redis db */;
        let first_field : String = MySuperCoolStruct::first_field::get(&redis_client, key : "MyRedisKeyForStruct");
        MySuperCoolStruct::first_field::set(&redis_client, key : "MyRedisKeyForStruct", value : String::from("test"));
    }

License: MIT OR Apache-2.0

Dependencies

~5MB
~126K SLoC