Skip to content

Rust/WASI example #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
clarkmcc opened this issue Jun 3, 2022 · 12 comments · Fixed by #676
Closed

Rust/WASI example #619

clarkmcc opened this issue Jun 3, 2022 · 12 comments · Fixed by #676
Labels
enhancement New feature or request

Comments

@clarkmcc
Copy link

clarkmcc commented Jun 3, 2022

Is your feature request related to a problem? Please describe.
I've copied the WASI example nearly exactly but I can't seem to get it to work with a Rust guest, for example, my scripts panic when I try to use time::Instant, and the .WithArgs aren't available when calling std::env::args(). Is there an example or anything special I need to do to get it working? If I were just shooting in the dark, I'd say that Rust just isn't expecting those host functions to go into the wasi_snapshot_preview1 module name, but again, I'm just spitballing.

Describe the solution you'd like
An example of using Rust and WASI.

@clarkmcc clarkmcc added the enhancement New feature or request label Jun 3, 2022
@codefromthecrypt
Copy link
Contributor

I shot around in the dark too. The main thing is TinyGo builds in support for WASI and rust doesn't. That said, I agree this is time well spent to add something however it can be achieved.

@clarkmcc
Copy link
Author

clarkmcc commented Jun 4, 2022

I'm wondering if we could piggy back off of this. https://github.com/bytecodealliance/wasi/blob/main/src/lib_generated.rs

@codefromthecrypt
Copy link
Contributor

looks fine.. seems to have no dependency on wasmtime at the moment https://crates.io/crates/wasi/0.11.0+wasi-snapshot-preview1/dependencies

If you'd like to give this a go, mainly just follow the structure of the allocation example and make a new directory with the same ("ish") code for rust that's done in tinygo?

@clarkmcc
Copy link
Author

clarkmcc commented Jun 4, 2022

Ah, silly me. Rust already has a WASI implementation and toolchain. I can still work on getting a complete example created if that would be helpful.

cargo build --target wasm32-wasi

More details are available here and https://github.com/bytecodealliance/wasi/blob/9ec04a7d8ebb1bbb9e3291503425cee1ec38a560/README.md?plain=1#L19-L21

@codefromthecrypt
Copy link
Contributor

silly me, too, then :)

I think it would be worth doing one if you have time. try to lift the same templating from the allocation example, though I think you won't actually need the allocator in order to do the cat example.

If you run out of time, you can still push an incomplete PR up and it would help vs starting from scratch!

@codefromthecrypt
Copy link
Contributor

codefromthecrypt commented Jul 6, 2022

circling back on this, I started to make a rust/wasi example, but I think it is untenable to host the binary here as it is massive. Ex. if you build the cargo-wasi's hello world, it is almost 2MB.

Ex. just this

fn main() {
    println!("Hello, World!");
}

If you do it manually with cargo build --release --target wasm32-wasi even with optimizations, it is still 260KB

That's too large a binary to check into our source. It seems this is related to a dependency on wasi-libc, which we'll test directly with C. If that changes in the future, we can think about hosting a demo here, but meanwhile I think existing docs or a separate example repo are best.

rust-lang/rust#73432

@clarkmcc
Copy link
Author

clarkmcc commented Jul 6, 2022

That works for me. The alternative is just to slap this at the top of the example main.go file and in the README instruct users to run go generate then go build. This avoids checking in the compiled wasm. But again, that's just an option.

go generate cargo build --release --target wasm32-wasi

@codefromthecrypt
Copy link
Contributor

I'll add it to the README. good idea

@codefromthecrypt
Copy link
Contributor

sorry I meant to say I'll add a how to into the README as I think primarily pointers were missing in how to find rust stuff. Rust is the most over served language in WebAssembly, most maintained, most docs, most control, most people working on it.

Basically, I think with pointers to rust projects and also to our other rust example, should be good enough, especially with WASI as it should act the same regardless of toolchain. I don't really want to special-case a generation step over this, but that doesn't mean eliminate pointers to where to go in other words.

@codefromthecrypt
Copy link
Contributor

Actually the release build using cargo wasi build --release isn't looking so bad. If this ends up <100k and still works, I'll add it in and say that this is an optimized version of cargo build --release --target wasm32-wasi

codefromthecrypt pushed a commit that referenced this issue Jul 7, 2022
This uses cargo-wasi because the default target creates almost 2MB of
wasm for a simple cat program.

Fixes #619

Signed-off-by: Adrian Cole <[email protected]>
@codefromthecrypt
Copy link
Contributor

okie dokie.. better late than never @clarkmcc! #676

@clarkmcc
Copy link
Author

clarkmcc commented Jul 7, 2022

@codefromthecrypt thanks! Sorry for not getting around to rolling an example. Things got chaotic on my side, as always 🙄.

codefromthecrypt pushed a commit that referenced this issue Jul 8, 2022
This uses cargo-wasi because the default target creates almost 2MB of
wasm for a simple cat program.

Fixes #619
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants