Skip to content

Commit e682a34

Browse files
committed
adding adder component written in Rust
Signed-off-by: Mark Rossett <marosset@microsoft.com>
1 parent a1dd2b6 commit e682a34

6 files changed

Lines changed: 220 additions & 0 deletions

File tree

Justfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
build-adder:
2+
cd components/adder && cargo component build

components/adder/Cargo.lock

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

components/adder/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "adder"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
wit-bindgen-rt = { version = "0.41.0", features = ["bitflags"] }
8+
9+
[lib]
10+
crate-type = ["cdylib"]
11+
12+
[package.metadata.component]
13+
package = "hyperlight-wasm-examples:calculator"
14+
15+
[package.metadata.component.target]
16+
path = "../../wit/calculator.wit"
17+
world = "adder"

components/adder/src/bindings.rs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
// Generated by `wit-bindgen` 0.41.0. DO NOT EDIT!
2+
// Options used:
3+
// * runtime_path: "wit_bindgen_rt"
4+
#[rustfmt::skip]
5+
#[allow(dead_code, clippy::all)]
6+
pub mod exports {
7+
pub mod hyperlight_wasm_examples {
8+
pub mod calculator {
9+
#[allow(dead_code, async_fn_in_trait, unused_imports, clippy::all)]
10+
pub mod add {
11+
#[used]
12+
#[doc(hidden)]
13+
static __FORCE_SECTION_REF: fn() = super::super::super::super::__link_custom_section_describing_imports;
14+
use super::super::super::super::_rt;
15+
#[doc(hidden)]
16+
#[allow(non_snake_case)]
17+
pub unsafe fn _export_add_cabi<T: Guest>(arg0: i32, arg1: i32) -> i32 {
18+
#[cfg(target_arch = "wasm32")] _rt::run_ctors_once();
19+
let result0 = T::add(arg0 as u32, arg1 as u32);
20+
_rt::as_i32(result0)
21+
}
22+
pub trait Guest {
23+
fn add(x: u32, y: u32) -> u32;
24+
}
25+
#[doc(hidden)]
26+
macro_rules! __export_hyperlight_wasm_examples_calculator_add_cabi {
27+
($ty:ident with_types_in $($path_to_types:tt)*) => {
28+
const _ : () = { #[unsafe (export_name =
29+
"hyperlight-wasm-examples:calculator/add#add")] unsafe extern "C"
30+
fn export_add(arg0 : i32, arg1 : i32,) -> i32 { unsafe {
31+
$($path_to_types)*:: _export_add_cabi::<$ty > (arg0, arg1) } } };
32+
};
33+
}
34+
#[doc(hidden)]
35+
pub(crate) use __export_hyperlight_wasm_examples_calculator_add_cabi;
36+
}
37+
}
38+
}
39+
}
40+
#[rustfmt::skip]
41+
mod _rt {
42+
#![allow(dead_code, clippy::all)]
43+
#[cfg(target_arch = "wasm32")]
44+
pub fn run_ctors_once() {
45+
wit_bindgen_rt::run_ctors_once();
46+
}
47+
pub fn as_i32<T: AsI32>(t: T) -> i32 {
48+
t.as_i32()
49+
}
50+
pub trait AsI32 {
51+
fn as_i32(self) -> i32;
52+
}
53+
impl<'a, T: Copy + AsI32> AsI32 for &'a T {
54+
fn as_i32(self) -> i32 {
55+
(*self).as_i32()
56+
}
57+
}
58+
impl AsI32 for i32 {
59+
#[inline]
60+
fn as_i32(self) -> i32 {
61+
self as i32
62+
}
63+
}
64+
impl AsI32 for u32 {
65+
#[inline]
66+
fn as_i32(self) -> i32 {
67+
self as i32
68+
}
69+
}
70+
impl AsI32 for i16 {
71+
#[inline]
72+
fn as_i32(self) -> i32 {
73+
self as i32
74+
}
75+
}
76+
impl AsI32 for u16 {
77+
#[inline]
78+
fn as_i32(self) -> i32 {
79+
self as i32
80+
}
81+
}
82+
impl AsI32 for i8 {
83+
#[inline]
84+
fn as_i32(self) -> i32 {
85+
self as i32
86+
}
87+
}
88+
impl AsI32 for u8 {
89+
#[inline]
90+
fn as_i32(self) -> i32 {
91+
self as i32
92+
}
93+
}
94+
impl AsI32 for char {
95+
#[inline]
96+
fn as_i32(self) -> i32 {
97+
self as i32
98+
}
99+
}
100+
impl AsI32 for usize {
101+
#[inline]
102+
fn as_i32(self) -> i32 {
103+
self as i32
104+
}
105+
}
106+
}
107+
/// Generates `#[unsafe(no_mangle)]` functions to export the specified type as
108+
/// the root implementation of all generated traits.
109+
///
110+
/// For more information see the documentation of `wit_bindgen::generate!`.
111+
///
112+
/// ```rust
113+
/// # macro_rules! export{ ($($t:tt)*) => (); }
114+
/// # trait Guest {}
115+
/// struct MyType;
116+
///
117+
/// impl Guest for MyType {
118+
/// // ...
119+
/// }
120+
///
121+
/// export!(MyType);
122+
/// ```
123+
#[allow(unused_macros)]
124+
#[doc(hidden)]
125+
macro_rules! __export_adder_impl {
126+
($ty:ident) => {
127+
self::export!($ty with_types_in self);
128+
};
129+
($ty:ident with_types_in $($path_to_types_root:tt)*) => {
130+
$($path_to_types_root)*::
131+
exports::hyperlight_wasm_examples::calculator::add::__export_hyperlight_wasm_examples_calculator_add_cabi!($ty
132+
with_types_in $($path_to_types_root)*::
133+
exports::hyperlight_wasm_examples::calculator::add);
134+
};
135+
}
136+
#[doc(inline)]
137+
pub(crate) use __export_adder_impl as export;
138+
#[cfg(target_arch = "wasm32")]
139+
#[unsafe(
140+
link_section = "component-type:wit-bindgen:0.41.0:hyperlight-wasm-examples:calculator:adder:encoded world"
141+
)]
142+
#[doc(hidden)]
143+
#[allow(clippy::octal_escapes)]
144+
pub static __WIT_BINDGEN_COMPONENT_TYPE: [u8; 241] = *b"\
145+
\0asm\x0d\0\x01\0\0\x19\x16wit-component-encoding\x04\0\x07v\x01A\x02\x01A\x02\x01\
146+
B\x02\x01@\x02\x01xy\x01yy\0y\x04\0\x03add\x01\0\x04\0'hyperlight-wasm-examples:\
147+
calculator/add\x05\0\x04\0)hyperlight-wasm-examples:calculator/adder\x04\0\x0b\x0b\
148+
\x01\0\x05adder\x03\0\0\0G\x09producers\x01\x0cprocessed-by\x02\x0dwit-component\
149+
\x070.227.1\x10wit-bindgen-rust\x060.41.0";
150+
#[inline(never)]
151+
#[doc(hidden)]
152+
pub fn __link_custom_section_describing_imports() {
153+
wit_bindgen_rt::maybe_link_cabi_realloc();
154+
}

components/adder/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod bindings;
2+
3+
use bindings::exports::hyperlight_wasm_examples::calculator::add::Guest;
4+
5+
struct Component;
6+
7+
impl Guest for Component {
8+
fn add(x: u32, y: u32) -> u32 {
9+
x + y
10+
}
11+
}
12+
13+
bindings::export!(Component with_types_in bindings);

wit/calculator.wit

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package hyperlight-wasm-examples:calculator;
2+
3+
interface add {
4+
add: func(x: u32, y: u32) -> u32;
5+
}
6+
7+
world adder {
8+
export add;
9+
}

0 commit comments

Comments
 (0)