Skip to content

Commit

Permalink
Merge pull request #2 from taubyte/1-required-changes-for-lit-impleme…
Browse files Browse the repository at this point in the history
…ntation

add bytes slice codec
  • Loading branch information
tafseer-khan authored Mar 27, 2023
2 parents 2870d03 + dbb70e8 commit 1d5a65e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/http/client/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub fn getHttpResponseHeaderSize(
assert_eq!(key, test::HEADER_KEY);

let v = vec![test::HEADER_VALUE.to_string()];
let header = codec::byte_slice::from_string_slice(v);
let header = codec::string_slice::from(v);
utils::write_usize(size, header.len());

Errno::ErrorNone.error()
Expand All @@ -260,7 +260,7 @@ pub fn getHttpResponseHeader(
assert_eq!(key, test::HEADER_KEY);

let v = vec![test::HEADER_VALUE.to_string()];
let header = codec::byte_slice::from_string_slice(v);
let header = codec::string_slice::from(v);
utils::write_bytes_vec(buf_ptr, header);

Errno::ErrorNone.error()
Expand Down
4 changes: 2 additions & 2 deletions src/storage/file/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub mod mock {
let name = utils::read_string(name_ptr, name_size);

let to_write_versions: Vec<String> = test::VERSIONS.iter().map(|v| v.to_string()).collect();
let to_write = codec::byte_slice::from_string_slice(to_write_versions);
let to_write = codec::string_slice::from(to_write_versions);

if storage_id != new_test::STORAGE_ID {
Errno::ErrorCap.error()
Expand All @@ -117,7 +117,7 @@ pub mod mock {
let name = utils::read_string(name_ptr, name_size);

let to_write_versions: Vec<String> = test::VERSIONS.iter().map(|v| v.to_string()).collect();
let to_write = codec::byte_slice::from_string_slice(to_write_versions);
let to_write = codec::string_slice::from(to_write_versions);

if storage_id != new_test::STORAGE_ID {
Errno::ErrorCap.error()
Expand Down
9 changes: 0 additions & 9 deletions src/utils/codec/byte_slice.rs

This file was deleted.

25 changes: 25 additions & 0 deletions src/utils/codec/bytes_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub fn to(buf:Vec<u8>) -> Vec<Vec<u8>> {
let mut result: Vec<Vec<u8>> = Vec::new();
let mut idx = 0;
while idx < buf.len(){
if idx+2 >= buf.len(){
break
}
let size = u16::from_le_bytes([buf[idx], buf[idx+1]]) as usize;
idx += 2;
if idx+size > buf.len(){
break
}
result.push(buf[idx..idx+size].to_vec());
idx += size;
}
result
}
pub fn from(vec: Vec<Vec<u8>>) -> Vec<u8> {
let mut result: Vec<u8> = Vec::new();
for mut s in vec {
result.append(&mut s.len().to_le_bytes().to_vec());
result.append(&mut s);
}
result
}
9 changes: 9 additions & 0 deletions src/utils/codec/string_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ pub fn to(buf: Vec<u8>) -> Vec<String> {

result
}

pub fn from(vec: Vec<String>) -> Vec<u8> {
let mut result: Vec<u8> = Vec::new();
for s in vec {
result.append(&mut s.into_bytes());
result.push(0);
}
result
}
2 changes: 1 addition & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod codec {
pub mod byte_slice;
pub mod bytes_slice;
pub mod cid;
pub mod string_slice;
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/test/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn write_bytes_vec(ptr: *mut u8, to_write: Vec<u8>) {

pub fn write_string_slice(ptr: *mut u8, to_write: &[&str]) {
let v: Vec<String> = to_write.iter().map(|s| s.to_string()).collect();
let bytes_slice = codec::byte_slice::from_string_slice(v);
let bytes_slice = codec::string_slice::from(v);

unsafe {
let buf = std::slice::from_raw_parts_mut(ptr, bytes_slice.len());
Expand All @@ -47,7 +47,7 @@ pub fn write_string_slice(ptr: *mut u8, to_write: &[&str]) {

pub fn write_string_slice_size(ptr: *mut usize, to_write: &[&str]) {
let v: Vec<String> = to_write.iter().map(|s| s.to_string()).collect();
let bytes_slice = codec::byte_slice::from_string_slice(v);
let bytes_slice = codec::string_slice::from(v);
write_usize(ptr, bytes_slice.len())
}

Expand Down

0 comments on commit 1d5a65e

Please sign in to comment.