forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_input.fe
More file actions
47 lines (40 loc) · 1.07 KB
/
Copy pathmemory_input.fe
File metadata and controls
47 lines (40 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use core::abi::{ByteInput, Decode}
use core::Copy
use core::num::IntDowncast
use ingot::abi::Sol
use ingot::abi::sol::decode_input
use ingot::evm::ops::mload
/// A view over an in-memory byte buffer.
pub struct MemoryBytes {
pub base: u256,
pub len: u256,
}
impl Copy for MemoryBytes {}
impl MemoryBytes {
pub fn new(base: u256, len: u256) -> Self {
MemoryBytes { base, len }
}
/// Decode Solidity ABI data from this memory view.
pub fn decode<T>(self) -> T
where T: Decode<Sol>
{
decode_input(self)
}
}
impl ByteInput for MemoryBytes {
fn len(self) -> u256 {
self.len
}
#[arithmetic(unchecked)]
fn word_at(self, _ byte_offset: u256) -> u256 {
mload(self.base + byte_offset)
}
#[arithmetic(unchecked)]
fn byte_at(self, _ byte_offset: u256) -> u8 {
let word_offset = byte_offset - (byte_offset % 32)
let word = mload(self.base + word_offset)
let idx = byte_offset - word_offset
let shift = (31 - idx) * 8
(word >> shift).downcast_truncate()
}
}