Finish challenge 14.

This commit is contained in:
2022-06-25 18:28:38 -04:00
parent 52222e90e9
commit f6b4c98826
3 changed files with 121 additions and 31 deletions

View File

@@ -6,6 +6,10 @@ use std::fmt::Write; // need to import this trait
pub struct Bytes(pub Vec<u8>);
impl Bytes {
pub fn empty() -> Bytes {
Bytes(vec![])
}
pub fn from_utf8(s: &str) -> Bytes {
Bytes(s.as_bytes().iter().map(|c| c.clone()).collect())
}
@@ -16,6 +20,10 @@ impl Bytes {
String::from(std::str::from_utf8(&v).unwrap())
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn get_block(&self, block_index: usize, block_size: usize) -> Bytes {
Bytes(self.0[(block_index * block_size)..(block_index + 1) * block_size].to_vec())
}