Break into two crates

This commit is contained in:
Emil Ernerfeldt 2018-12-30 21:08:29 +01:00
parent 25d049ae35
commit bfa20be28e
14 changed files with 67 additions and 49 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
*.sublime* *.sublime*
/docs/*.d.ts
/target /target

View file

@ -1,19 +1,8 @@
[package] [workspace]
name = "emgui" members = [
version = "0.1.0" "emgui",
authors = ["Emil Ernerfeldt <emilernerfeldt@gmail.com>"] "emgui_wasm",
edition = "2018" ]
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
lazy_static = "1"
serde = "1"
serde_derive = "1"
serde_json = "1"
wasm-bindgen = "0.2"
web-sys = { version = "0.3.5", features = ['console', 'Performance', 'Window'] }
# Optimize for small code size: # Optimize for small code size:
[profile.dev] [profile.dev]

View file

@ -13,12 +13,15 @@
return cachegetUint8Memory; return cachegetUint8Memory;
} }
let WASM_VECTOR_LEN = 0;
function passStringToWasm(arg) { function passStringToWasm(arg) {
const buf = cachedTextEncoder.encode(arg); const buf = cachedTextEncoder.encode(arg);
const ptr = wasm.__wbindgen_malloc(buf.length); const ptr = wasm.__wbindgen_malloc(buf.length);
getUint8Memory().set(buf, ptr); getUint8Memory().set(buf, ptr);
return [ptr, buf.length]; WASM_VECTOR_LEN = buf.length;
return ptr;
} }
let cachedTextDecoder = new TextDecoder('utf-8'); let cachedTextDecoder = new TextDecoder('utf-8');
@ -47,7 +50,8 @@
* @returns {string} * @returns {string}
*/ */
__exports.show_gui = function(arg0) { __exports.show_gui = function(arg0) {
const [ptr0, len0] = passStringToWasm(arg0); const ptr0 = passStringToWasm(arg0);
const len0 = WASM_VECTOR_LEN;
const retptr = globalArgumentPtr(); const retptr = globalArgumentPtr();
try { try {
wasm.show_gui(retptr, ptr0, len0); wasm.show_gui(retptr, ptr0, len0);
@ -77,7 +81,7 @@
if (path_or_module instanceof WebAssembly.Module) { if (path_or_module instanceof WebAssembly.Module) {
instantiation = WebAssembly.instantiate(path_or_module, imports) instantiation = WebAssembly.instantiate(path_or_module, imports)
.then(instance => { .then(instance => {
return { instance, module: module_or_path } return { instance, module: path_or_module }
}); });
} else { } else {
const data = fetch(path_or_module); const data = fetch(path_or_module);
@ -91,7 +95,7 @@
} }
return instantiation.then(({instance}) => { return instantiation.then(({instance}) => {
wasm = init.wasm = instance.exports; wasm = init.wasm = instance.exports;
return;
}); });
}; };
self.wasm_bindgen = Object.assign(init, __exports); self.wasm_bindgen = Object.assign(init, __exports);

Binary file not shown.

12
emgui/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "emgui"
version = "0.1.0"
authors = ["Emil Ernerfeldt <emilernerfeldt@gmail.com>"]
edition = "2018"
[lib]
[dependencies]
# palette = "0.4"
serde = "1"
serde_derive = "1"

16
emgui/src/lib.rs Normal file
View file

@ -0,0 +1,16 @@
#![deny(warnings)]
extern crate serde;
#[macro_use] // TODO: get rid of this
extern crate serde_derive;
mod emgui;
mod layout;
pub mod math;
mod style;
pub mod types;
pub use crate::{
emgui::Emgui, layout::Layout, layout::LayoutOptions, style::Style, types::RawInput,
};

17
emgui_wasm/Cargo.toml Normal file
View file

@ -0,0 +1,17 @@
[package]
name = "emgui_wasm"
version = "0.1.0"
authors = ["Emil Ernerfeldt <emilernerfeldt@gmail.com>"]
edition = "2018"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
lazy_static = "1"
serde = "1"
serde_json = "1"
wasm-bindgen = "0.2"
# web-sys = { version = "0.3.5", features = ['console', 'Performance', 'Window'] }
emgui = { path = "../emgui" }

View file

@ -1,4 +1,4 @@
use crate::{layout::Layout, math::*, types::*}; use emgui::{math::*, types::*, Layout};
pub trait GuiSettings { pub trait GuiSettings {
fn show_gui(&mut self, gui: &mut Layout); fn show_gui(&mut self, gui: &mut Layout);
@ -91,7 +91,7 @@ impl GuiSettings for App {
} }
} }
impl GuiSettings for crate::layout::LayoutOptions { impl GuiSettings for emgui::LayoutOptions {
fn show_gui(&mut self, gui: &mut Layout) { fn show_gui(&mut self, gui: &mut Layout) {
if gui.button("Reset LayoutOptions").clicked { if gui.button("Reset LayoutOptions").clicked {
*self = Default::default(); *self = Default::default();
@ -110,7 +110,7 @@ impl GuiSettings for crate::layout::LayoutOptions {
} }
} }
impl GuiSettings for crate::style::Style { impl GuiSettings for emgui::Style {
fn show_gui(&mut self, gui: &mut Layout) { fn show_gui(&mut self, gui: &mut Layout) {
if gui.button("Reset Style").clicked { if gui.button("Reset Style").clicked {
*self = Default::default(); *self = Default::default();

View file

@ -1,40 +1,19 @@
#![deny(warnings)] #![deny(warnings)]
extern crate lazy_static; extern crate lazy_static;
extern crate serde;
extern crate serde_json; extern crate serde_json;
extern crate wasm_bindgen; extern crate wasm_bindgen;
extern crate web_sys; // extern crate web_sys;
#[macro_use] // TODO: get rid of this extern crate emgui;
extern crate serde_derive;
use std::sync::Mutex; use std::sync::Mutex;
use emgui::{Emgui, RawInput};
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use crate::types::*;
pub mod app; pub mod app;
pub mod emgui;
pub mod layout;
pub mod math;
pub mod style;
pub mod types;
/*
// Fast compilation, slow code:
fn foo(x: &dyn Trait);
// Fast code, slow compilation:
fn foo<T: Trait>(x: &dyn T);
// Compiles quickly in debug, fast in release:
#[dynimp(Trait)]
fn foo(x: &Trait);
*/
#[wasm_bindgen] #[wasm_bindgen]
pub fn show_gui(raw_input_json: &str) -> String { pub fn show_gui(raw_input_json: &str) -> String {
@ -43,7 +22,7 @@ pub fn show_gui(raw_input_json: &str) -> String {
lazy_static::lazy_static! { lazy_static::lazy_static! {
static ref APP: Mutex<app::App> = Default::default(); static ref APP: Mutex<app::App> = Default::default();
static ref EMGUI: Mutex<crate::emgui::Emgui> = Default::default(); static ref EMGUI: Mutex<Emgui> = Default::default();
} }
let mut emgui = EMGUI.lock().unwrap(); let mut emgui = EMGUI.lock().unwrap();