Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kthakore committed Jan 24, 2021
1 parent f714448 commit 18efbbf
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
members = [
"rune",
"runefile-parser",
"runic-types"
"runic-types",
"runic-transform"
]

exclude = [
Expand Down
4 changes: 3 additions & 1 deletion rune/src/run/vm/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use log;
use std::collections::HashMap;
use runic_types::*;

mod rand;

pub trait Capability {
fn get_type() -> CAPABILITY;
fn request(&self, params: std::collections::HashMap<String, CapabilityParam>) -> Vec<u8>;
fn request(params: std::collections::HashMap<String, CapabilityParam>) -> Vec<u8>;
}


Expand Down
33 changes: 33 additions & 0 deletions rune/src/run/vm/capability/rand.rs
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![];
}
}
10 changes: 10 additions & 0 deletions runic-transform/Cargo.toml
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" }
74 changes: 74 additions & 0 deletions runic-transform/src/lib.rs
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);
}
}

0 comments on commit 18efbbf

Please sign in to comment.