-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
212 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,54 @@ | ||
library; | ||
|
||
pub struct Buffer { | ||
buffer: raw_ptr, | ||
cap: u64, | ||
size: u64 | ||
} | ||
|
||
impl Buffer { | ||
pub fn new() -> Self { | ||
let cap = 1024; | ||
Buffer { | ||
buffer: asm(size: cap) { | ||
aloc size; | ||
hp: raw_ptr | ||
}, | ||
cap, | ||
size: 0 | ||
} | ||
} | ||
|
||
pub fn push<T>(ref mut self, val: T) { | ||
let count = __size_of::<T>(); | ||
|
||
if self.cap >= self.size + count { | ||
asm(dst: self.buffer, val: val, count: count) { | ||
mcp dst val count; | ||
}; | ||
self.size += count; | ||
} else { | ||
__revert(123456789); | ||
} | ||
|
||
} | ||
} | ||
|
||
pub trait AbiEncode { | ||
fn abi_encode(self); | ||
fn abi_encode(self, ref mut buffer: Buffer); | ||
} | ||
|
||
impl AbiEncode for u64 { | ||
fn abi_encode(self, ref mut buffer: Buffer) { | ||
buffer.push(self); | ||
} | ||
} | ||
|
||
pub fn encode<T>(value: T) | ||
pub fn encode<T>(item: T) -> Buffer | ||
where | ||
T: AbiEncode | ||
{ | ||
value.abi_encode(); | ||
let mut buffer = Buffer::new(); | ||
item.abi_encode(buffer); | ||
buffer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters