Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
opti from_le_bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Oct 2, 2024
1 parent 07bac9b commit aeb9a0d
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions crates/utils/src/traits/bytes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -363,36 +363,42 @@ pub impl FromBytesImpl<
Option::Some(result)
}

fn from_le_bytes(self: Span<u8>) -> Option<T> {
fn from_le_bytes(mut self: Span<u8>) -> Option<T> {
let byte_size = ByteSize::<T>::byte_size();

if self.len() != byte_size {
return Option::None;
}

let mut result: T = Zero::zero();
let mut i = self.len();
while i != 0 {
i -= 1;
let tmp = result * 256_u16.into();
result = tmp + (*self[i]).into();
loop {
match self.pop_back() {
Option::None => { break; },
Option::Some(byte) => {
let tmp = result * 256_u16.into();
result = tmp + (*byte).into();
}
};
};
Option::Some(result)
}

fn from_le_bytes_partial(self: Span<u8>) -> Option<T> {
fn from_le_bytes_partial(mut self: Span<u8>) -> Option<T> {
let byte_size = ByteSize::<T>::byte_size();

if self.len() > byte_size {
return Option::None;
}

let mut result: T = Zero::zero();
let mut i = self.len();
while i != 0 {
i -= 1;
let tmp = result * 256_u16.into();
result = tmp + (*self[i]).into();
loop {
match self.pop_back() {
Option::None => { break; },
Option::Some(byte) => {
let tmp = result * 256_u16.into();
result = tmp + (*byte).into();
}
};
};
Option::Some(result)
}
Expand Down

0 comments on commit aeb9a0d

Please sign in to comment.