Skip to content

Commit

Permalink
feature: support &[u8] and &[u8; N] into BulkString
Browse files Browse the repository at this point in the history
  • Loading branch information
luffy2025 committed Dec 2, 2024
1 parent 22f70bc commit 96ff4b1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 14 additions & 2 deletions src/resp/bulk_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ impl RespDecode for BulkString {
}
}

impl From<&[u8]> for BulkString {
fn from(data: &[u8]) -> Self {
BulkString(data.to_vec())
}
}

impl<const N: usize> From<&[u8; N]> for BulkString {
fn from(data: &[u8; N]) -> Self {
BulkString(data.to_vec())
}
}

impl Deref for BulkString {
type Target = Vec<u8>;

Expand Down Expand Up @@ -58,15 +70,15 @@ mod tests {

#[test]
fn test_bulk_string_encode() {
let bs: BulkString = BulkString::new(b"hello");
let bs: BulkString = b"hello".into();
assert_eq!(bs.encode(), b"$5\r\nhello\r\n");
}

#[test]
fn test_bulk_string_decode() -> Result<()> {
let mut buf = bytes::BytesMut::from(&b"$5\r\nhello\r\n"[..]);
let bs = BulkString::decode(&mut buf)?;
assert_eq!(bs, BulkString::new(b"hello"));
assert_eq!(bs, b"hello".into());
Ok(())
}
}
12 changes: 6 additions & 6 deletions src/resp/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ mod tests {
#[test]
fn test_resp_map() {
let mut map = RespMap::new();
map.insert(SimpleString::new("get"), BulkString::new("hello").into());
map.insert(SimpleString::new("set"), BulkString::new("world").into());
map.insert(SimpleString::new("add"), 10.into());
map.insert("get".into(), BulkString::new("hello").into());
map.insert("set".into(), BulkString::new("world").into());
map.insert("add".into(), 10.into());

assert_eq!(
map.encode(),
Expand All @@ -92,9 +92,9 @@ mod tests {
);
let map = RespMap::decode(&mut buf)?;
let mut expected = RespMap::new();
expected.insert(SimpleString::new("get"), BulkString::new("hello").into());
expected.insert(SimpleString::new("set"), BulkString::new("world").into());
expected.insert(SimpleString::new("add"), 10.into());
expected.insert("get".into(), BulkString::new("hello").into());
expected.insert("set".into(), BulkString::new("world").into());
expected.insert("add".into(), 10.into());
assert_eq!(map, expected);

Ok(())
Expand Down

0 comments on commit 96ff4b1

Please sign in to comment.