Skip to content

Commit

Permalink
Add program to use std::os::wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinh Tran committed Nov 9, 2023
1 parent b78cd8a commit 464f03b
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 134 deletions.
17 changes: 0 additions & 17 deletions examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel

This file was deleted.

75 changes: 0 additions & 75 deletions examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs

This file was deleted.

15 changes: 0 additions & 15 deletions examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs

This file was deleted.

27 changes: 0 additions & 27 deletions examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs

This file was deleted.

28 changes: 28 additions & 0 deletions examples/toolchain-to-rebuild-std/hello_program/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load(
"@rules_rust//rust:defs.bzl",
"rust_binary",
)

package(default_visibility = ["//visibility:public"])

# This program uses std::os::wasi that is only available on WASI
# When running `bazel build` with `--platforms=//:aarch64-apple-darwin`
# rust toolchain is resolved to rebuild `std` for WASI specifically

# As a counterexample, try removing `target_compatible_with` attr and rebuild
# with `--platforms=//:aarch64-apple-darwin`. You'll see the following error
# --> hello_program/src/main_with_wasi.rs:4:14
# |
# 4 | use std::os::wasi::prelude::*;
# | ^^^^ could not find `wasi` in `os`
# which is expected because std is now built for aarch64-apple-darwin.
rust_binary(
name = "program_with_wasi",
srcs = [
"src/main_with_wasi.rs",
],
target_compatible_with = select({
"@rules_rust//rust/platform:wasm32-wasi": [],
"//conditions:default": ["@platforms//:incompatible"],
}),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copied from https://doc.rust-lang.org/std/os/wasi/index.html#examples

use std::fs::File;
use std::os::wasi::prelude::*;

fn main() -> std::io::Result<()> {
let f = File::create("foo.txt")?;
let fd = f.as_raw_fd();

// use fd with native WASI bindings

Ok(())
}

0 comments on commit 464f03b

Please sign in to comment.