redismodule-rs icon indicating copy to clipboard operation
redismodule-rs copied to clipboard

How to use binary/bytes/stringbuffer type

Open thomas9911 opened this issue 2 years ago • 6 comments

I'm making a module that wants to read/write non-utf8 bytes to Redis. However the RedisKeyWritable read / write function only accept &str and also the higher level call function only accepts strings.

Is there a way to do this currently (without using the raw C bindings) ?

Currently i worked around this by creating my own type and using the set_value / get_value functions.

thomas9911 avatar Oct 24 '22 09:10 thomas9911

@thomas9911 you're right it seems like a left over from the original API. The original API didn't work with byte safe RedisString but with String/str. We should update the API to support RedisString (PR is welcome)

gkorland avatar Oct 25 '22 11:10 gkorland

Since I also bumped into this missing functionality the other day I sketched a quick draft that just adds byte-slice-accepting versions of the original methods.

Although that might work, perhaps a more generic interface should be implemented (tho even then something like T: Into<Vec<u8>> cannot replace &str in the existing api, since &str accepts &&str and T: Into<Vec<u8>> does not)

tBuLi12 avatar Oct 25 '22 20:10 tBuLi12

I was also working on it 😅

thomas9911 avatar Oct 25 '22 20:10 thomas9911

Oh damn, sorry - well, mine was just a sketch and I see your version accepts RedisStrings rather than byte slices (as it should) so nvm that 😅

Idk about breaking the public API, but keeping the default read & write with plain old strings as arguments does not seem like good long-term choice either...

tBuLi12 avatar Oct 25 '22 21:10 tBuLi12

Maybe we could add a trait that can convert rust types into RedisString and let these functions accept this (so &str will keep working) :thinking:

thomas9911 avatar Oct 26 '22 12:10 thomas9911

Well we could make the api accept T: Into<RedisString> (at least in places where the underlying string is cloned anyway), and also add an impl Into<RedisString> for &str, but I'm not sure if that keeps it completely backwards compatible

tBuLi12 avatar Oct 26 '22 15:10 tBuLi12