From 464f03bcee3c8113fbc9c9f36950f40117736a1b Mon Sep 17 00:00:00 2001 From: Vinh Tran Date: Thu, 9 Nov 2023 20:07:05 +0000 Subject: [PATCH] Add program to use std::os::wasi --- .../hello_lib/BUILD.bazel | 17 ----- .../hello_lib/src/greeter.rs | 75 ------------------- .../hello_lib/src/lib.rs | 15 ---- .../hello_lib/tests/greeting.rs | 27 ------- .../hello_program/BUILD.bazel | 28 +++++++ .../hello_program/src/main_with_wasi.rs | 13 ++++ 6 files changed, 41 insertions(+), 134 deletions(-) delete mode 100644 examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel delete mode 100644 examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs delete mode 100644 examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs delete mode 100644 examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs create mode 100644 examples/toolchain-to-rebuild-std/hello_program/BUILD.bazel create mode 100644 examples/toolchain-to-rebuild-std/hello_program/src/main_with_wasi.rs diff --git a/examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel b/examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel deleted file mode 100644 index 2624299c46..0000000000 --- a/examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel +++ /dev/null @@ -1,17 +0,0 @@ -load( - "@rules_rust//rust:defs.bzl", - "rust_library", -) - -package(default_visibility = ["//visibility:public"]) - -# bazel build //hello_lib:hello_lib --platforms=//:aarch64-apple-darwin -rust_library( - name = "hello_lib", - srcs = [ - "src/greeter.rs", - "src/lib.rs", - ], - crate_features = ["default"], - rustc_flags = ["--cap-lints=allow"], -) diff --git a/examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs b/examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs deleted file mode 100644 index 4a9041546e..0000000000 --- a/examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -/// Object that displays a greeting. -pub struct Greeter { - greeting: String, -} - -/// Implementation of Greeter. -impl Greeter { - /// Constructs a new `Greeter`. - /// - /// # Examples - /// - /// ``` - /// use hello_lib::greeter::Greeter; - /// - /// let greeter = Greeter::new("Hello"); - /// ``` - pub fn new(greeting: &str) -> Greeter { - Greeter { - greeting: greeting.to_string(), - } - } - - /// Returns the greeting as a string. - /// - /// # Examples - /// - /// ``` - /// use hello_lib::greeter::Greeter; - /// - /// let greeter = Greeter::new("Hello"); - /// let greeting = greeter.greeting("World"); - /// ``` - pub fn greeting(&self, thing: &str) -> String { - format!("{} {}", &self.greeting, thing) - } - - /// Prints the greeting. - /// - /// # Examples - /// - /// ``` - /// use hello_lib::greeter::Greeter; - /// - /// let greeter = Greeter::new("Hello"); - /// greeter.greet("World"); - /// ``` - pub fn greet(&self, thing: &str) { - println!("{} {}", &self.greeting, thing); - } -} - -#[cfg(test)] -mod test { - use super::Greeter; - - #[test] - fn test_greeting() { - let hello = Greeter::new("Hi"); - assert_eq!("Hi Rust", hello.greeting("Rust")); - } -} diff --git a/examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs b/examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs deleted file mode 100644 index 9dc608911f..0000000000 --- a/examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -pub mod greeter; diff --git a/examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs b/examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs deleted file mode 100644 index 6084d13650..0000000000 --- a/examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2015 The Bazel Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -extern crate hello_lib; - -// test gate is not usually required in an integration test, but the BUILD -// script is using this file to test cdylib compilation as well, and when -// compiled in that mode, greeter is an unused import. -#[cfg(test)] -use hello_lib::greeter; - -#[test] -fn test_greeting() { - let hello = greeter::Greeter::new("Hello"); - assert_eq!("Hello world", hello.greeting("world")); -} diff --git a/examples/toolchain-to-rebuild-std/hello_program/BUILD.bazel b/examples/toolchain-to-rebuild-std/hello_program/BUILD.bazel new file mode 100644 index 0000000000..1760cce0ec --- /dev/null +++ b/examples/toolchain-to-rebuild-std/hello_program/BUILD.bazel @@ -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"], + }), +) diff --git a/examples/toolchain-to-rebuild-std/hello_program/src/main_with_wasi.rs b/examples/toolchain-to-rebuild-std/hello_program/src/main_with_wasi.rs new file mode 100644 index 0000000000..541a46b375 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/hello_program/src/main_with_wasi.rs @@ -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(()) +} \ No newline at end of file