Skip to content

Commit e971667

Browse files
committed
Port Rust to wasm
- Building adapter crate - Requires wasm-bindgen ^0.2.70 due to bug in toolchain [1] - Requires local installation of sarcaustech/unifac [2] with no parallelization (Rayon). Used with commit 4711916 [1] rustwasm/wasm-pack#886 [2] https://github.com/sarcaustech/unifac
0 parents  commit e971667

File tree

3 files changed

+319
-0
lines changed

3 files changed

+319
-0
lines changed

.gitignore

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
pkg/
2+
.idea/
3+
*.iml
4+
5+
# Created by https://www.toptal.com/developers/gitignore/api/windows,macos,linux,visualstudiocode,vim,intellij,rust
6+
# Edit at https://www.toptal.com/developers/gitignore?templates=windows,macos,linux,visualstudiocode,vim,intellij,rust
7+
8+
### Intellij ###
9+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
10+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
11+
12+
# User-specific stuff
13+
.idea/**/workspace.xml
14+
.idea/**/tasks.xml
15+
.idea/**/usage.statistics.xml
16+
.idea/**/dictionaries
17+
.idea/**/shelf
18+
19+
# Generated files
20+
.idea/**/contentModel.xml
21+
22+
# Sensitive or high-churn files
23+
.idea/**/dataSources/
24+
.idea/**/dataSources.ids
25+
.idea/**/dataSources.local.xml
26+
.idea/**/sqlDataSources.xml
27+
.idea/**/dynamic.xml
28+
.idea/**/uiDesigner.xml
29+
.idea/**/dbnavigator.xml
30+
31+
# Gradle
32+
.idea/**/gradle.xml
33+
.idea/**/libraries
34+
35+
# Gradle and Maven with auto-import
36+
# When using Gradle or Maven with auto-import, you should exclude module files,
37+
# since they will be recreated, and may cause churn. Uncomment if using
38+
# auto-import.
39+
# .idea/artifacts
40+
# .idea/compiler.xml
41+
# .idea/jarRepositories.xml
42+
# .idea/modules.xml
43+
# .idea/*.iml
44+
# .idea/modules
45+
# *.iml
46+
# *.ipr
47+
48+
# CMake
49+
cmake-build-*/
50+
51+
# Mongo Explorer plugin
52+
.idea/**/mongoSettings.xml
53+
54+
# File-based project format
55+
*.iws
56+
57+
# IntelliJ
58+
out/
59+
60+
# mpeltonen/sbt-idea plugin
61+
.idea_modules/
62+
63+
# JIRA plugin
64+
atlassian-ide-plugin.xml
65+
66+
# Cursive Clojure plugin
67+
.idea/replstate.xml
68+
69+
# Crashlytics plugin (for Android Studio and IntelliJ)
70+
com_crashlytics_export_strings.xml
71+
crashlytics.properties
72+
crashlytics-build.properties
73+
fabric.properties
74+
75+
# Editor-based Rest Client
76+
.idea/httpRequests
77+
78+
# Android studio 3.1+ serialized cache file
79+
.idea/caches/build_file_checksums.ser
80+
81+
### Intellij Patch ###
82+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
83+
84+
# *.iml
85+
# modules.xml
86+
# .idea/misc.xml
87+
# *.ipr
88+
89+
# Sonarlint plugin
90+
# https://plugins.jetbrains.com/plugin/7973-sonarlint
91+
.idea/**/sonarlint/
92+
93+
# SonarQube Plugin
94+
# https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
95+
.idea/**/sonarIssues.xml
96+
97+
# Markdown Navigator plugin
98+
# https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
99+
.idea/**/markdown-navigator.xml
100+
.idea/**/markdown-navigator-enh.xml
101+
.idea/**/markdown-navigator/
102+
103+
# Cache file creation bug
104+
# See https://youtrack.jetbrains.com/issue/JBR-2257
105+
.idea/$CACHE_FILE$
106+
107+
# CodeStream plugin
108+
# https://plugins.jetbrains.com/plugin/12206-codestream
109+
.idea/codestream.xml
110+
111+
### Linux ###
112+
*~
113+
114+
# temporary files which can be created if a process still has a handle open of a deleted file
115+
.fuse_hidden*
116+
117+
# KDE directory preferences
118+
.directory
119+
120+
# Linux trash folder which might appear on any partition or disk
121+
.Trash-*
122+
123+
# .nfs files are created when an open file is removed but is still being accessed
124+
.nfs*
125+
126+
### macOS ###
127+
# General
128+
.DS_Store
129+
.AppleDouble
130+
.LSOverride
131+
132+
# Icon must end with two \r
133+
Icon
134+
135+
136+
# Thumbnails
137+
._*
138+
139+
# Files that might appear in the root of a volume
140+
.DocumentRevisions-V100
141+
.fseventsd
142+
.Spotlight-V100
143+
.TemporaryItems
144+
.Trashes
145+
.VolumeIcon.icns
146+
.com.apple.timemachine.donotpresent
147+
148+
# Directories potentially created on remote AFP share
149+
.AppleDB
150+
.AppleDesktop
151+
Network Trash Folder
152+
Temporary Items
153+
.apdisk
154+
155+
### Rust ###
156+
# Generated by Cargo
157+
# will have compiled files and executables
158+
/target/
159+
160+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
161+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
162+
Cargo.lock
163+
164+
### Vim ###
165+
# Swap
166+
[._]*.s[a-v][a-z]
167+
!*.svg # comment out if you don't need vector files
168+
[._]*.sw[a-p]
169+
[._]s[a-rt-v][a-z]
170+
[._]ss[a-gi-z]
171+
[._]sw[a-p]
172+
173+
# Session
174+
Session.vim
175+
Sessionx.vim
176+
177+
# Temporary
178+
.netrwhist
179+
# Auto-generated tag files
180+
tags
181+
# Persistent undo
182+
[._]*.un~
183+
184+
### VisualStudioCode ###
185+
.vscode/*
186+
!.vscode/tasks.json
187+
!.vscode/launch.json
188+
*.code-workspace
189+
190+
### VisualStudioCode Patch ###
191+
# Ignore all local history of files
192+
.history
193+
.ionide
194+
195+
### Windows ###
196+
# Windows thumbnail cache files
197+
Thumbs.db
198+
Thumbs.db:encryptable
199+
ehthumbs.db
200+
ehthumbs_vista.db
201+
202+
# Dump file
203+
*.stackdump
204+
205+
# Folder config file
206+
[Dd]esktop.ini
207+
208+
# Recycle Bin used on file shares
209+
$RECYCLE.BIN/
210+
211+
# Windows Installer files
212+
*.cab
213+
*.msi
214+
*.msix
215+
*.msm
216+
*.msp
217+
218+
# Windows shortcuts
219+
*.lnk
220+
221+
# End of https://www.toptal.com/developers/gitignore/api/windows,macos,linux,visualstudiocode,vim,intellij,rust

Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "unifac-wasm"
3+
version = "0.1.0"
4+
authors = ["Mikel Muennekhoff <[email protected]>", "Peter Heringer <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[lib]
10+
crate-type = ["cdylib", "rlib"]
11+
12+
[dependencies]
13+
unifac = { path = "../unifac" } #"0.1.1"
14+
wasm-bindgen = "0.2.70"
15+
js-sys = "0.3.46"

src/lib.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
#[derive(Debug, Clone)]
5+
pub struct Substance {
6+
pub fraction: f64,
7+
pub gamma: Option<f64>,
8+
functional_groups: Vec<unifac::FunctionalGroup>,
9+
}
10+
11+
#[wasm_bindgen]
12+
#[derive(Debug, Clone)]
13+
pub struct Mixture {
14+
pub temperature: f64,
15+
substances: Vec<unifac::Substance>,
16+
}
17+
18+
#[wasm_bindgen]
19+
impl Mixture {
20+
#[wasm_bindgen(constructor)]
21+
pub fn new(temperature: f64) -> Self {
22+
Self {
23+
temperature,
24+
substances: Vec::new(),
25+
}
26+
}
27+
28+
#[wasm_bindgen]
29+
pub fn add_substance(&mut self, substance: Substance) -> Self {
30+
let s = unifac::Substance::from(substance.fraction, substance.functional_groups);
31+
self.substances.push(s);
32+
self.clone()
33+
}
34+
35+
#[wasm_bindgen]
36+
pub fn print(&self) -> js_sys::JsString {
37+
js_sys::JsString::from(format!("{:?}", self)).clone()
38+
}
39+
40+
#[wasm_bindgen]
41+
pub fn calc(&self) -> js_sys::Array {
42+
match unifac::calc(self.substances.clone(), self.temperature) {
43+
Ok(res) => {
44+
let arr = js_sys::Array::new();
45+
for s in res {
46+
arr.push(&JsValue::from_f64(s.gamma.unwrap()));
47+
}
48+
arr
49+
}
50+
Err(_) => js_sys::Array::new(),
51+
}
52+
}
53+
}
54+
55+
#[wasm_bindgen]
56+
impl Substance {
57+
#[wasm_bindgen(constructor)]
58+
pub fn new(fraction: f64) -> Self {
59+
Substance {
60+
fraction,
61+
functional_groups: Vec::new(),
62+
gamma: None,
63+
}
64+
}
65+
66+
#[wasm_bindgen]
67+
pub fn add_functional_group(&mut self, id: u8, nu: f64) -> Self {
68+
let fg = unifac::FunctionalGroup::from(id, nu).expect("FG add_functional_group");
69+
self.functional_groups.push(fg);
70+
self.clone()
71+
}
72+
73+
#[wasm_bindgen]
74+
pub fn print(&self) -> js_sys::JsString {
75+
let s: Vec<String> = self
76+
.functional_groups
77+
.iter()
78+
.map(|fg| format!("{} ({} times)", fg.id, fg.nu))
79+
.collect();
80+
let joined = s.join(" ");
81+
js_sys::JsString::from(joined).clone()
82+
}
83+
}

0 commit comments

Comments
 (0)