File tree 2 files changed +39
-0
lines changed 2 files changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -1242,6 +1242,16 @@ impl<T: FromRedisValue> FromRedisValue for Option<T> {
1242
1242
}
1243
1243
}
1244
1244
1245
+ #[ cfg( feature = "bytes" ) ]
1246
+ impl FromRedisValue for bytes:: Bytes {
1247
+ fn from_redis_value ( v : & Value ) -> RedisResult < Self > {
1248
+ match v {
1249
+ Value :: Data ( bytes_vec) => Ok ( bytes:: Bytes :: copy_from_slice ( bytes_vec. as_ref ( ) ) ) ,
1250
+ _ => invalid_type_error ! ( v, "Not binary data" ) ,
1251
+ }
1252
+ }
1253
+ }
1254
+
1245
1255
/// A shortcut function to invoke `FromRedisValue::from_redis_value`
1246
1256
/// to make the API slightly nicer.
1247
1257
pub fn from_redis_value < T : FromRedisValue > ( v : & Value ) -> RedisResult < T > {
Original file line number Diff line number Diff line change @@ -163,6 +163,35 @@ fn test_bool() {
163
163
assert_eq ! ( v, Ok ( true ) ) ;
164
164
}
165
165
166
+ #[ cfg( feature = "bytes" ) ]
167
+ #[ test]
168
+ fn test_bytes ( ) {
169
+ use bytes:: Bytes ;
170
+ use redis:: { ErrorKind , FromRedisValue , RedisResult , Value } ;
171
+
172
+ let content: & [ u8 ] = b"\x01 \x02 \x03 \x04 " ;
173
+ let content_vec: Vec < u8 > = Vec :: from ( content) ;
174
+ let content_bytes = Bytes :: from_static ( content) ;
175
+
176
+ let v: RedisResult < Bytes > = FromRedisValue :: from_redis_value ( & Value :: Data ( content_vec) ) ;
177
+ assert_eq ! ( v, Ok ( content_bytes) ) ;
178
+
179
+ let v: RedisResult < Bytes > = FromRedisValue :: from_redis_value ( & Value :: Status ( "garbage" . into ( ) ) ) ;
180
+ assert_eq ! ( v. unwrap_err( ) . kind( ) , ErrorKind :: TypeError ) ;
181
+
182
+ let v: RedisResult < Bytes > = FromRedisValue :: from_redis_value ( & Value :: Okay ) ;
183
+ assert_eq ! ( v. unwrap_err( ) . kind( ) , ErrorKind :: TypeError ) ;
184
+
185
+ let v: RedisResult < Bytes > = FromRedisValue :: from_redis_value ( & Value :: Nil ) ;
186
+ assert_eq ! ( v. unwrap_err( ) . kind( ) , ErrorKind :: TypeError ) ;
187
+
188
+ let v: RedisResult < Bytes > = FromRedisValue :: from_redis_value ( & Value :: Int ( 0 ) ) ;
189
+ assert_eq ! ( v. unwrap_err( ) . kind( ) , ErrorKind :: TypeError ) ;
190
+
191
+ let v: RedisResult < Bytes > = FromRedisValue :: from_redis_value ( & Value :: Int ( 42 ) ) ;
192
+ assert_eq ! ( v. unwrap_err( ) . kind( ) , ErrorKind :: TypeError ) ;
193
+ }
194
+
166
195
#[ test]
167
196
fn test_types_to_redis_args ( ) {
168
197
use redis:: ToRedisArgs ;
You can’t perform that action at this time.
0 commit comments