diff --git a/packages/data_structures/src/byte_reader.cairo b/packages/data_structures/src/byte_reader.cairo index cc613367..3ad8a56b 100644 --- a/packages/data_structures/src/byte_reader.cairo +++ b/packages/data_structures/src/byte_reader.cairo @@ -171,8 +171,13 @@ pub trait ByteReader { } // use core::ops::index::IndexView; -// impl ByteReaderImpl, +Len, +IndexView> of ByteReader { -impl ByteReaderImpl, +Len, +IndexView> of ByteReader { +impl ByteReaderImpl< + T, + +Drop, + +Len, + impl IndexViewImpl: core::ops::index::IndexView, + +Into +> of ByteReader { #[inline] fn reader(self: @T) -> ByteReaderState { ByteReaderState { data: self, index: 0 } @@ -186,8 +191,8 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead #[inline] fn word_u16(self: @T, offset: usize) -> Option { if self.remaining(offset, 2) { - let b1 = *self[offset]; - let b2 = *self[offset + 1]; + let b1 = *self[offset].into(); + let b2 = *self[offset + 1].into(); Option::Some(b1.into() * one_shift_left_bytes_u128(1).try_into().unwrap() + b2.into()) } else { Option::None @@ -197,8 +202,8 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead #[inline] fn word_u16_le(self: @T, offset: usize) -> Option { if self.remaining(offset, 2) { - let b1 = *self[offset]; - let b2 = *self[offset + 1]; + let b1 = *self[offset].into(); + let b2 = *self[offset + 1].into(); Option::Some(b1.into() + b2.into() * one_shift_left_bytes_u128(1).try_into().unwrap()) } else { Option::None @@ -208,10 +213,10 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead #[inline] fn word_u32(self: @T, offset: usize) -> Option { if self.remaining(offset, 4) { - let b1 = *self[offset]; - let b2 = *self[offset + 1]; - let b3 = *self[offset + 2]; - let b4 = *self[offset + 3]; + let b1 = *self[offset].into(); + let b2 = *self[offset + 1].into(); + let b3 = *self[offset + 2].into(); + let b4 = *self[offset + 3].into(); Option::Some( b1.into() * one_shift_left_bytes_u128(3).try_into().unwrap() + b2.into() * one_shift_left_bytes_u128(2).try_into().unwrap() @@ -226,10 +231,10 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead #[inline] fn word_u32_le(self: @T, offset: usize) -> Option { if self.remaining(offset, 4) { - let b1 = *self[offset]; - let b2 = *self[offset + 1]; - let b3 = *self[offset + 2]; - let b4 = *self[offset + 3]; + let b1 = *self[offset].into(); + let b2 = *self[offset + 1].into(); + let b3 = *self[offset + 2].into(); + let b4 = *self[offset + 3].into(); Option::Some( b1.into() + b2.into() * one_shift_left_bytes_u128(1).try_into().unwrap() @@ -244,14 +249,14 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead #[inline] fn word_u64(self: @T, offset: usize) -> Option { if self.remaining(offset, 8) { - let b1 = *self[offset]; - let b2 = *self[offset + 1]; - let b3 = *self[offset + 2]; - let b4 = *self[offset + 3]; - let b5 = *self[offset + 4]; - let b6 = *self[offset + 5]; - let b7 = *self[offset + 6]; - let b8 = *self[offset + 7]; + let b1 = *self[offset].into(); + let b2 = *self[offset + 1].into(); + let b3 = *self[offset + 2].into(); + let b4 = *self[offset + 3].into(); + let b5 = *self[offset + 4].into(); + let b6 = *self[offset + 5].into(); + let b7 = *self[offset + 6].into(); + let b8 = *self[offset + 7].into(); Option::Some( b1.into() * one_shift_left_bytes_u128(7).try_into().unwrap() + b2.into() * one_shift_left_bytes_u128(6).try_into().unwrap() @@ -270,14 +275,14 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead #[inline] fn word_u64_le(self: @T, offset: usize) -> Option { if self.remaining(offset, 8) { - let b1 = *self[offset]; - let b2 = *self[offset + 1]; - let b3 = *self[offset + 2]; - let b4 = *self[offset + 3]; - let b5 = *self[offset + 4]; - let b6 = *self[offset + 5]; - let b7 = *self[offset + 6]; - let b8 = *self[offset + 7]; + let b1 = *self[offset].into(); + let b2 = *self[offset + 1].into(); + let b3 = *self[offset + 2].into(); + let b4 = *self[offset + 3].into(); + let b5 = *self[offset + 4].into(); + let b6 = *self[offset + 5].into(); + let b7 = *self[offset + 6].into(); + let b8 = *self[offset + 7].into(); Option::Some( b1.into() + b2.into() * one_shift_left_bytes_u128(1).try_into().unwrap() @@ -296,22 +301,22 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead #[inline] fn word_u128(self: @T, offset: usize) -> Option { if self.remaining(offset, 16) { - let b01 = *self[offset]; - let b02 = *self[offset + 1]; - let b03 = *self[offset + 2]; - let b04 = *self[offset + 3]; - let b05 = *self[offset + 4]; - let b06 = *self[offset + 5]; - let b07 = *self[offset + 6]; - let b08 = *self[offset + 7]; - let b09 = *self[offset + 8]; - let b10 = *self[offset + 9]; - let b11 = *self[offset + 10]; - let b12 = *self[offset + 11]; - let b13 = *self[offset + 12]; - let b14 = *self[offset + 13]; - let b15 = *self[offset + 14]; - let b16 = *self[offset + 15]; + let b01 = *self[offset].into(); + let b02 = *self[offset + 1].into(); + let b03 = *self[offset + 2].into(); + let b04 = *self[offset + 3].into(); + let b05 = *self[offset + 4].into(); + let b06 = *self[offset + 5].into(); + let b07 = *self[offset + 6].into(); + let b08 = *self[offset + 7].into(); + let b09 = *self[offset + 8].into(); + let b10 = *self[offset + 9].into(); + let b11 = *self[offset + 10].into(); + let b12 = *self[offset + 11].into(); + let b13 = *self[offset + 12].into(); + let b14 = *self[offset + 13].into(); + let b15 = *self[offset + 14].into(); + let b16 = *self[offset + 15].into(); Option::Some( b01.into() * one_shift_left_bytes_u128(15).try_into().unwrap() + b02.into() * one_shift_left_bytes_u128(14).try_into().unwrap() @@ -338,22 +343,22 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead #[inline] fn word_u128_le(self: @T, offset: usize) -> Option { if self.remaining(offset, 16) { - let b01 = *self[offset]; - let b02 = *self[offset + 1]; - let b03 = *self[offset + 2]; - let b04 = *self[offset + 3]; - let b05 = *self[offset + 4]; - let b06 = *self[offset + 5]; - let b07 = *self[offset + 6]; - let b08 = *self[offset + 7]; - let b09 = *self[offset + 8]; - let b10 = *self[offset + 9]; - let b11 = *self[offset + 10]; - let b12 = *self[offset + 11]; - let b13 = *self[offset + 12]; - let b14 = *self[offset + 13]; - let b15 = *self[offset + 14]; - let b16 = *self[offset + 15]; + let b01 = *self[offset].into(); + let b02 = *self[offset + 1].into(); + let b03 = *self[offset + 2].into(); + let b04 = *self[offset + 3].into(); + let b05 = *self[offset + 4].into(); + let b06 = *self[offset + 5].into(); + let b07 = *self[offset + 6].into(); + let b08 = *self[offset + 7].into(); + let b09 = *self[offset + 8].into(); + let b10 = *self[offset + 9].into(); + let b11 = *self[offset + 10].into(); + let b12 = *self[offset + 11].into(); + let b13 = *self[offset + 12].into(); + let b14 = *self[offset + 13].into(); + let b15 = *self[offset + 14].into(); + let b16 = *self[offset + 15].into(); Option::Some( b01.into() + b02.into() * one_shift_left_bytes_u128(01).try_into().unwrap() @@ -379,7 +384,7 @@ impl ByteReaderImpl, +Len, +IndexView> of ByteRead fn read_u8(ref self: ByteReaderState) -> Option { if self.data.remaining(self.index, 1) { - let result = *self.data[self.index]; + let result = *self.data[self.index].into(); self.index += 1; Option::Some(result) } else { @@ -574,22 +579,10 @@ impl ByteReaderLenImpl> of Len> { } } -impl ByteArrayIndexViewAsSnapshotImpl of IndexView { +impl IntoU8Impl of Into { #[inline(always)] - fn index(self: @ByteArray, index: usize) -> @u8 { - @self.at(index).expect('Index out of bounds') - } -} - -// use core::ops::index::IndexView; -impl ByteReaderIndexViewImpl< - T, impl TIndexView: core::ops::index::IndexView, +Into -> of core::ops::index::IndexView, usize> { - type Target = @u8; - - #[inline(always)] - fn index(self: @ByteReaderState, index: usize) -> @u8 { - @TIndexView::index(*self.data, index).into() + fn into(self: u8) -> @u8 { + @self } }