Squashed 'third_party/rules_rust/' content from commit bf59038ca

git-subtree-dir: third_party/rules_rust
git-subtree-split: bf59038cac11798cbaef9f3bf965bad8182b97fa
Signed-off-by: Brian Silverman <bsilver16384@gmail.com>
Change-Id: I5a20e403203d670df467ea97dde9a4ac40339a8d
diff --git a/examples/wasm/BUILD.bazel b/examples/wasm/BUILD.bazel
new file mode 100644
index 0000000..e499c31
--- /dev/null
+++ b/examples/wasm/BUILD.bazel
@@ -0,0 +1,79 @@
+# Copyright 2020 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.
+
+# buildifier: disable=module-docstring
+load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_test")
+load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_shared_library")
+load("@rules_rust//wasm_bindgen:wasm_bindgen.bzl", "rust_wasm_bindgen")
+
+package(default_visibility = ["//visibility:public"])
+
+rust_binary(
+    name = "hello_world_bin_wasm",
+    srcs = ["main.rs"],
+    edition = "2018",
+    deps = [
+        "@rules_rust//wasm_bindgen/raze:wasm_bindgen",
+    ],
+)
+
+rust_shared_library(
+    name = "hello_world_lib_wasm",
+    srcs = ["main.rs"],
+    edition = "2018",
+    deps = [
+        "@rules_rust//wasm_bindgen/raze:wasm_bindgen",
+    ],
+)
+
+rust_wasm_bindgen(
+    name = "hello_world_bundler_wasm_bindgen",
+    wasm_file = ":hello_world_bin_wasm",
+)
+
+rust_wasm_bindgen(
+    name = "hello_world_web_wasm_bindgen",
+    target = "web",
+    wasm_file = ":hello_world_lib_wasm",
+)
+
+rust_wasm_bindgen(
+    name = "hello_world_deno_wasm_bindgen",
+    target = "deno",
+    wasm_file = ":hello_world_lib_wasm",
+)
+
+rust_wasm_bindgen(
+    name = "hello_world_nomodules_wasm_bindgen",
+    target = "no-modules",
+    wasm_file = ":hello_world_lib_wasm",
+)
+
+rust_wasm_bindgen(
+    name = "hello_world_nodejs_wasm_bindgen",
+    target = "nodejs",
+    wasm_file = ":hello_world_lib_wasm",
+)
+
+nodejs_test(
+    name = "hello_world_wasm_test",
+    data = [
+        ":hello_world_bundler_wasm_bindgen",
+        ":hello_world_deno_wasm_bindgen",
+        ":hello_world_nodejs_wasm_bindgen",
+        ":hello_world_nomodules_wasm_bindgen",
+        ":hello_world_web_wasm_bindgen",
+    ],
+    entry_point = "hello_world_wasm_test.js",
+)
diff --git a/examples/wasm/hello_world_wasm_test.js b/examples/wasm/hello_world_wasm_test.js
new file mode 100644
index 0000000..8712335
--- /dev/null
+++ b/examples/wasm/hello_world_wasm_test.js
@@ -0,0 +1,23 @@
+"use strict";
+const fs = require('fs');
+const path = require('path');
+const assert = require('assert');
+
+const main = async function (typ) {
+  const wasm_file = path.join(__dirname, 'hello_world_'+typ+'_wasm_bindgen_bg.wasm');
+  assert.ok(fs.existsSync(wasm_file));
+
+  const buf = fs.readFileSync(wasm_file);
+  assert.ok(buf);
+
+  const res = await WebAssembly.instantiate(buf);
+  assert.ok(res);
+  assert.strictEqual(res.instance.exports.double(2), 4);
+};
+
+["bundler", "web", "deno", "nomodules", "nodejs"].forEach((typ) => {
+  main(typ).catch(function (err) {
+    console.error(err);
+    process.exit(1);
+  });
+})
diff --git a/examples/wasm/main.rs b/examples/wasm/main.rs
new file mode 100644
index 0000000..8703ad3
--- /dev/null
+++ b/examples/wasm/main.rs
@@ -0,0 +1,11 @@
+use wasm_bindgen::prelude::*;
+
+#[wasm_bindgen]
+pub fn double(i: i32) -> i32 {
+    i * 2
+}
+
+#[allow(dead_code)]
+fn main() {
+    println!("Hello {}", double(2));
+}