-
Notifications
You must be signed in to change notification settings - Fork 15
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
5 changed files
with
122 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,8 @@ | |
members = [ | ||
"rune", | ||
"runefile-parser", | ||
"runic-types" | ||
"runic-types", | ||
"runic-transform" | ||
] | ||
|
||
exclude = [ | ||
|
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,33 @@ | ||
use log; | ||
|
||
use crate::run::vm::capability::*; | ||
use runic_types::*; | ||
pub struct RandCapability { | ||
|
||
} | ||
|
||
|
||
|
||
impl Capability for RandCapability { | ||
fn get_type() -> CAPABILITY { | ||
return CAPABILITY::RAND; | ||
} | ||
|
||
fn request(params: std::collections::HashMap<String, CapabilityParam>) -> Vec<u8> { | ||
|
||
|
||
let number_of_samples: u32 = match params.get(&String::from("n")) { | ||
Some(number_of_samples) => { | ||
let int_value = transform::<u32>( (*number_of_samples.value).to_vec(), number_of_samples.value_type); | ||
if int_value.len() > 0usize { | ||
int_value[0] | ||
} else { | ||
1 | ||
} | ||
}, | ||
_ => 1 | ||
}; | ||
|
||
return vec![]; | ||
} | ||
} |
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,10 @@ | ||
[package] | ||
name = "runic-transform" | ||
version = "0.1.0" | ||
authors = ["Kartik Thakore <kartik@thakore.ai>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
runic-types = { path = "../runic-types" } |
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,74 @@ | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::boxed::Box; | ||
use runic_types::*; | ||
use alloc::vec::Vec; | ||
|
||
pub trait Transformable<INPUT_BUFFER_TYPE, OUTPUT_BUFFER_TYPE> { | ||
fn to_buffer(input: &[INPUT_BUFFER_TYPE], input_size: usize) -> Vec<u8>; | ||
fn from_buffer(input: Vec<u8>, buffer_size: usize) -> Vec<INPUT_BUFFER_TYPE>; | ||
} | ||
|
||
pub struct Transform<InputType, OutputType> { | ||
_input: Option<InputType>, | ||
_output: Option<OutputType> | ||
} | ||
|
||
// Transformable<input_buffer_type, output_buffer_type> for &[Input Type] | ||
impl Transformable<f32, i32> for Transform<f32, i32> { | ||
// Should return a Vec<i32> ?? | ||
fn to_buffer(_input: &[f32], input_size: usize) -> Vec<u8> { | ||
// Transformed to &[f32] then to &[u8] | ||
|
||
return Vec::with_capacity(input_size * 4 as usize); | ||
} | ||
|
||
fn from_buffer(_input: Vec<u8>, buffer_size: usize) -> Vec<f32> { | ||
|
||
return Vec::from([0.0]); | ||
} | ||
} | ||
|
||
// Transformable<input_buffer_type, output_buffer_type> for &[Input Type] | ||
impl Transformable<f32, f32> for Transform<f32, f32> { | ||
// Should return a Vec<i32> ?? | ||
fn to_buffer(input: &[f32], input_size: usize) -> Vec<u8> { | ||
// Transformed to &[f32] then to &[u8] | ||
let layout = alloc::alloc::Layout::from_size_align(input_size * 4, 1).unwrap(); | ||
let mut out: Vec<u8> = Vec::with_capacity(input_size*4); | ||
|
||
for input_idx in 0..input_size { | ||
let input = input[input_idx]; | ||
let input = input.to_be_bytes(); | ||
out[input_idx + 0] = input[0]; | ||
out[input_idx + 1] = input[1]; | ||
out[input_idx + 2] = input[2]; | ||
out[input_idx + 3] = input[3]; | ||
} | ||
return out; | ||
} | ||
|
||
fn from_buffer(_input: Vec<u8>, buffer_size: usize) -> Vec<f32> { | ||
|
||
return Vec::with_capacity( (buffer_size / 4) as usize); | ||
} | ||
} | ||
|
||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::*; | ||
#[test] | ||
fn can_transform_same_types() { | ||
|
||
let raw: Vec<f32> = Vec::from([0.0]); | ||
let buffer: Vec<u8> = Transform::<f32, f32>::to_buffer(raw, raw.len()); | ||
let transform: Vec<f32> = Transform::<f32, f32>::from_buffer(buffer); | ||
|
||
|
||
println!("{:?}", buffer); | ||
} | ||
} |