Skip to content

Commit

Permalink
refactor: Avoid using to_string when writing cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Westerlind committed Sep 7, 2018
1 parent 065b3ed commit 4deb8f3
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,22 @@ where
cmd.extend_from_slice(b"\r\n");

{
let mut encode = |item: &[u8]| {
let mut cursor_bytes = [0; 20];
for item in args {
let bytes = match item {
Arg::Cursor => {
let n = ::itoa::write(&mut cursor_bytes[..], cursor).unwrap();
&cursor_bytes[..n]
}
Arg::Simple(val) => val,
};

cmd.push(b'$');
::itoa::write(&mut *cmd, item.len()).unwrap();
cmd.extend_from_slice(b"\r\n");
cmd.extend_from_slice(item);
::itoa::write(&mut *cmd, bytes.len()).unwrap();
cmd.extend_from_slice(b"\r\n");
};

for item in args {
match item {
Arg::Cursor => encode(cursor.to_string().as_bytes()),
Arg::Simple(val) => encode(val),
}
cmd.extend_from_slice(bytes);
cmd.extend_from_slice(b"\r\n");
}
}
}
Expand Down

0 comments on commit 4deb8f3

Please sign in to comment.