You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/06/30 13:20:17 UTC

[avro] 01/01: AVRO-3558: Rust: Add a demo crate that shows usage as WebAssembly

This is an automated email from the ASF dual-hosted git repository.

mgrigorov pushed a commit to branch avro-3558-demo-wasm
in repository https://gitbox.apache.org/repos/asf/avro.git

commit 2d31b2b38a8e73aea70b61a1b01a4362eac2b07d
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
AuthorDate: Thu Jun 30 16:19:43 2022 +0300

    AVRO-3558: Rust: Add a demo crate that shows usage as WebAssembly
    
    Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
 .github/workflows/test-lang-rust-ci.yml | 43 +++++++++++++++++
 lang/rust/Cargo.toml                    |  3 +-
 lang/rust/wasm-demo/Cargo.toml          | 46 ++++++++++++++++++
 lang/rust/wasm-demo/README.md           | 28 +++++++++++
 lang/rust/wasm-demo/src/lib.rs          | 17 +++++++
 lang/rust/wasm-demo/tests/demos.rs      | 82 +++++++++++++++++++++++++++++++++
 6 files changed, 218 insertions(+), 1 deletion(-)

diff --git a/.github/workflows/test-lang-rust-ci.yml b/.github/workflows/test-lang-rust-ci.yml
index 5c5bc9e8c..9796f1118 100644
--- a/.github/workflows/test-lang-rust-ci.yml
+++ b/.github/workflows/test-lang-rust-ci.yml
@@ -196,3 +196,46 @@ jobs:
       - name: Perl reads interop files created by Java and Rust
         working-directory: lang/perl
         run: ./build.sh interop-data-test
+
+  web-assembly:
+    runs-on: ubuntu-latest
+
+    steps:
+      - name: Checkout
+        uses: actions/checkout@v2
+
+      - name: Rust Toolchain
+        uses: actions-rs/toolchain@v1
+        with:
+          profile: minimal
+          toolchain: stable
+          override: true
+          target: wasm32-unknown-unknown
+
+      - name: Cache Cargo
+        uses: actions/cache@v2
+        with:
+          # these represent dependencies downloaded by cargo
+          # and thus do not depend on the OS, arch nor rust version.
+          path: ~/.cargo
+          key: cargo-cache1-
+
+      - name: Cache Rust dependencies
+        uses: actions/cache@v2
+        with:
+          # these represent compiled steps of both dependencies and avro
+          # and thus are specific for a particular OS, arch and rust version.
+          path: ~/target
+          key: ${{ runner.os }}-target-cache1-stable-
+
+      - name: Install wasm-pack
+        run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
+
+      - name: Install Firefox
+        run: apt install firefox
+
+      - name: Build the Web Assembly demo app
+        run: wasm-pack build wasm-demo
+
+      - name: Test the Web Assembly demo app
+        run: RUST_BACKTRACE=1 wasm-pack test --headless --firefox wasm-demo
diff --git a/lang/rust/Cargo.toml b/lang/rust/Cargo.toml
index bcda788a7..689a83e51 100644
--- a/lang/rust/Cargo.toml
+++ b/lang/rust/Cargo.toml
@@ -19,5 +19,6 @@
 members = [
     "avro_test_helper",
     "avro_derive",
-    "avro"
+    "avro",
+    "wasm-demo"
 ]
diff --git a/lang/rust/wasm-demo/Cargo.toml b/lang/rust/wasm-demo/Cargo.toml
new file mode 100644
index 000000000..9fa891300
--- /dev/null
+++ b/lang/rust/wasm-demo/Cargo.toml
@@ -0,0 +1,46 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you 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.
+
+[package]
+name = "hello-wasm"
+version = "0.1.0"
+authors = ["Apache Avro team <de...@avro.apache.org>"]
+description = "A demo project for testing apache_avro in WebAssembly"
+license = "Apache-2.0"
+readme = "README.md"
+repository = "https://github.com/apache/avro"
+edition = "2018"
+keywords = ["avro", "data", "serialization", "wasm", "web assembly"]
+categories = ["encoding"]
+documentation = "https://docs.rs/apache-avro"
+
+
+[lib]
+crate-type = ["cdylib", "rlib"]
+
+[dependencies]
+apache-avro = { path = "../avro" }
+serde = { default-features = false, version = "1.0.137", features = ["derive"] }
+wasm-bindgen = "0.2.63"
+
+[dev-dependencies]
+console_error_panic_hook = { version = "0.1.6" }
+wasm-bindgen-test = "0.3.13"
+
+[profile.release]
+# Tell `rustc` to optimize for small code size.
+opt-level = "s"
diff --git a/lang/rust/wasm-demo/README.md b/lang/rust/wasm-demo/README.md
new file mode 100644
index 000000000..87d63641b
--- /dev/null
+++ b/lang/rust/wasm-demo/README.md
@@ -0,0 +1,28 @@
+<!---
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you 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.
+-->
+
+# About
+
+An application that is used to test `apache_avro` crate as a web assembly.
+
+The project is created with `wasm-pack new wasm-demo` command and simplified to not use unrelated technologies (like Wee and a panic hook).
+
+# Code
+
+See [tests](./tests/demos.rs)
diff --git a/lang/rust/wasm-demo/src/lib.rs b/lang/rust/wasm-demo/src/lib.rs
new file mode 100644
index 000000000..5cd17fb5a
--- /dev/null
+++ b/lang/rust/wasm-demo/src/lib.rs
@@ -0,0 +1,17 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
diff --git a/lang/rust/wasm-demo/tests/demos.rs b/lang/rust/wasm-demo/tests/demos.rs
new file mode 100644
index 000000000..08d9d21ec
--- /dev/null
+++ b/lang/rust/wasm-demo/tests/demos.rs
@@ -0,0 +1,82 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you 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.
+
+#![cfg(target_arch = "wasm32")]
+
+extern crate wasm_bindgen_test;
+
+use std::io::BufWriter;
+use wasm_bindgen_test::*;
+
+use apache_avro::{from_value, to_value, types::Record, Codec, Reader, Schema, Writer};
+use serde::{Deserialize, Serialize};
+
+wasm_bindgen_test_configure!(run_in_browser);
+
+#[derive(Deserialize, Serialize, Debug, PartialEq)]
+pub struct MyRecord {
+    b: String,
+    a: i64,
+}
+
+#[wasm_bindgen_test]
+fn serialization_roundtrip() {
+    console_error_panic_hook::set_once();
+
+    let record = MyRecord {
+        b: "hello".to_string(),
+        a: 1,
+    };
+
+    let serialized = to_value(&record).unwrap();
+    let deserialized = from_value::<MyRecord>(&serialized).unwrap();
+    assert_eq!(deserialized, record);
+}
+
+#[wasm_bindgen_test]
+fn write_read() {
+    console_error_panic_hook::set_once();
+
+    let schema_str = r#"
+    {
+      "type": "record",
+      "name": "my_record",
+      "fields": [
+        {"name": "a", "type": "long"},
+        {"name": "b", "type": "string"}
+      ]
+    }"#;
+    let schema = Schema::parse_str(schema_str).unwrap();
+
+    let mut record = Record::new(&schema).unwrap();
+    record.put("a", 12_i32);
+    record.put("b", "hello".to_owned());
+
+    let mut writer = Writer::with_codec(&schema, BufWriter::new(Vec::with_capacity(200)), Codec::Null);
+    writer.append(record).unwrap();
+    writer.flush().unwrap();
+    let bytes = writer.into_inner().unwrap().into_inner().unwrap();
+
+    let reader = Reader::new(&bytes[..]).unwrap();
+
+    for value in reader {
+        match value {
+            Ok(record) => println!("Successfully read {:?}", record),
+            Err(err) => panic!("An error occurred while reading: {:?}", err),
+        }
+    }
+}