-
Notifications
You must be signed in to change notification settings - Fork 23
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
13 changed files
with
184 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "molecule-std" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
molecule = { path = "../../bindings/rust" } | ||
|
||
[build-dependencies] | ||
codegen = { package ="molecule-codegen", path = "../../tools/codegen" } |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use codegen::{Compiler, Language}; | ||
|
||
fn compile_schema(schema: &str) { | ||
let mut compiler = Compiler::new(); | ||
compiler | ||
.input_schema_file(schema) | ||
.generate_code(Language::Rust) | ||
.output_dir_set_default() | ||
.expand_primitive_types() | ||
.run() | ||
.unwrap(); | ||
println!("cargo:rerun-if-changed={}", schema); | ||
} | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=primitive_types.mol"); | ||
compile_schema("primitive_types.mol"); | ||
let out_dir = ::std::env::var("OUT_DIR").unwrap(); | ||
println!("{}", out_dir); | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
array uint64 [byte; 8]; |
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
pub mod prelude; | ||
pub mod primitive_types; | ||
|
||
extern crate alloc; | ||
|
||
pub use alloc::{borrow::ToOwned, vec, vec::Vec}; | ||
use core::{clone::Clone, default::Default, fmt}; | ||
use molecule::prelude::*; | ||
pub use molecule::{ | ||
bytes, | ||
bytes::Bytes, | ||
error, | ||
error::VerificationResult, | ||
hex_string, io, | ||
primitive::{Byte, ByteReader}, | ||
verification_error, | ||
}; | ||
|
||
use primitive_types::*; | ||
|
||
pub trait Unpack<T> { | ||
/// Unpack binary data into rust types. | ||
fn unpack(&self) -> T; | ||
} | ||
|
||
/// A syntactic sugar to convert a rust type into binary data. | ||
pub trait Pack<T: Entity> { | ||
/// Packs a rust type into binary data. | ||
fn pack(&self) -> T; | ||
} | ||
|
||
impl Pack<Uint64> for u64 { | ||
fn pack(&self) -> Uint64 { | ||
Uint64::new_unchecked(Bytes::from(self.to_le_bytes().to_vec())) | ||
} | ||
} | ||
|
||
impl<'r> Unpack<u64> for Uint64Reader<'r> { | ||
fn unpack(&self) -> u64 { | ||
let mut b = [0u8; 8]; | ||
b.copy_from_slice(self.as_slice()); | ||
u64::from_le_bytes(b) | ||
} | ||
} | ||
|
||
macro_rules! impl_conversion_for_entity_unpack { | ||
($original:ty, $entity:ident) => { | ||
impl Unpack<$original> for $entity { | ||
fn unpack(&self) -> $original { | ||
self.as_reader().unpack() | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_conversion_for_entity_unpack!(u64, Uint64); |
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
include!(concat!(env!("OUT_DIR"), "/primitive_types.rs")); | ||
|
||
extern crate alloc; | ||
pub use alloc::{borrow::ToOwned, vec, vec::Vec}; | ||
pub use molecule::{ | ||
bytes, | ||
bytes::Bytes, | ||
error, | ||
error::VerificationResult, | ||
hex_string, io, prelude, | ||
prelude::*, | ||
primitive::{Byte, ByteReader}, | ||
verification_error, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
include!(concat!(env!("OUT_DIR"), "/primitive_types.rs")); |
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
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