Skip to content

Commit

Permalink
Make encode more flexible
Browse files Browse the repository at this point in the history
By using `impl AsRef<[u8]>` instead of raw `&[u8]` encode may be used
with Strings and string slices directly. This is also important for
foreign types as they can implement `AsRef<[u8]>` and be directly
usable by `encode`.

Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
  • Loading branch information
wiktor-k committed Oct 16, 2024
1 parent b95ebc3 commit c3d384b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ impl fmt::Display for DecodeError {
}
}

pub fn encode(input: &[u8]) -> String {
pub fn encode(input: impl AsRef<[u8]>) -> String {
let input = input.as_ref();
let mut result = Vec::new();
let chunks = input.chunks(5);

Expand Down Expand Up @@ -92,6 +93,17 @@ mod tests {
assert_eq!(encode(b"asdasd"), "cf3seamuco".to_string());
}

#[test]
fn encode_str() {
assert_eq!(encode("asdasd"), "cf3seamuco".to_string());
}

#[test]
fn encode_string() {
let string = String::from("asdasd");
assert_eq!(encode(string), "cf3seamuco".to_string());
}

#[test]
fn simple_decode() {
assert_eq!(decode("cf3seamu"), Ok(b"asdas".to_vec()))
Expand Down

0 comments on commit c3d384b

Please sign in to comment.