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

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


The following commit(s) were added to refs/heads/branch-1.11 by this push:
     new ad851035c AVRO-3558: Rust: Add a demo crate that shows usage as 
WebAssembly (#1747)
ad851035c is described below

commit ad851035c3ab0e9cfea7e3e89e1ad00ac732e7bc
Author: Martin Grigorov <[email protected]>
AuthorDate: Fri Jul 1 11:17:11 2022 +0300

    AVRO-3558: Rust: Add a demo crate that shows usage as WebAssembly (#1747)
    
    * AVRO-3558: Rust: Add a demo crate that shows usage as WebAssembly
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    * AVRO-3558 Format the code
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    * AVRO-3558: Install Firefox with sudo
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    * AVRO-3558: Test one wasm32-unknown-unknown only for the web-Assembly CI 
job
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    * AVRO-3558: Bump MSRV from 1.51.0 to 1.54.0 and return 
wasm32-unknown-unknown target
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    
    * AVRO-3558: Fix clippy errors and warnings
    
    Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
    (cherry picked from commit bf6b86d2c784ced5ec791f8249d3f2ecf18a4ee0)
---
 .github/workflows/test-lang-rust-ci.yml | 42 +++++++++++++++-
 lang/rust/Cargo.toml                    |  3 +-
 lang/rust/README.md                     |  4 ++
 lang/rust/avro/src/encode.rs            |  4 +-
 lang/rust/avro/src/ser.rs               |  4 +-
 lang/rust/avro_derive/tests/derive.rs   |  8 ++-
 lang/rust/wasm-demo/Cargo.toml          | 46 ++++++++++++++++++
 lang/rust/wasm-demo/README.md           | 28 +++++++++++
 lang/rust/wasm-demo/src/lib.rs          | 16 ++++++
 lang/rust/wasm-demo/tests/demos.rs      | 86 +++++++++++++++++++++++++++++++++
 10 files changed, 230 insertions(+), 11 deletions(-)

diff --git a/.github/workflows/test-lang-rust-ci.yml 
b/.github/workflows/test-lang-rust-ci.yml
index 5c5bc9e8c..6c3642323 100644
--- a/.github/workflows/test-lang-rust-ci.yml
+++ b/.github/workflows/test-lang-rust-ci.yml
@@ -43,7 +43,7 @@ jobs:
           - stable
           - beta
           - nightly
-          - 1.51.0  # MSRV
+          - 1.54.0  # MSRV
         target:
           - x86_64-unknown-linux-gnu
           - wasm32-unknown-unknown
@@ -196,3 +196,43 @@ 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: 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/README.md b/lang/rust/README.md
index 1442ce776..965dd51ab 100644
--- a/lang/rust/README.md
+++ b/lang/rust/README.md
@@ -642,6 +642,10 @@ let readers_schema = Schema::parse_str(r#"{"type": 
"array", "items":"int"}"#).un
 assert_eq!(false, SchemaCompatibility::can_read(&writers_schema, 
&readers_schema));
 ```
 
+## Minimal supported Rust version
+
+1.54.0
+
 ## License
 This project is licensed under [Apache License 
2.0](https://github.com/apache/avro/blob/master/LICENSE.txt).
 
diff --git a/lang/rust/avro/src/encode.rs b/lang/rust/avro/src/encode.rs
index 6524eb933..afb3e2964 100644
--- a/lang/rust/avro/src/encode.rs
+++ b/lang/rust/avro/src/encode.rs
@@ -106,9 +106,9 @@ pub(crate) fn encode_internal<S: Borrow<Schema>>(
             buffer.extend_from_slice(&slice);
         }
         Value::Uuid(uuid) => encode_bytes(
-            #[allow(unknown_lints)] // for Rust 1.51.0
-            #[allow(clippy::unnecessary_to_owned)]
             // we need the call .to_string() to properly convert ASCII to UTF-8
+            #[allow(unknown_lints)] // for Rust 1.54.0
+            #[allow(clippy::unnecessary_to_owned)]
             &uuid.to_string(),
             buffer,
         ),
diff --git a/lang/rust/avro/src/ser.rs b/lang/rust/avro/src/ser.rs
index ccd0a0a90..c85c8c4d3 100644
--- a/lang/rust/avro/src/ser.rs
+++ b/lang/rust/avro/src/ser.rs
@@ -285,7 +285,7 @@ impl<'b> ser::Serializer for &'b mut Serializer {
     }
 }
 
-impl<'a> ser::SerializeSeq for SeqSerializer {
+impl ser::SerializeSeq for SeqSerializer {
     type Ok = Value;
     type Error = Error;
 
@@ -303,7 +303,7 @@ impl<'a> ser::SerializeSeq for SeqSerializer {
     }
 }
 
-impl<'a> ser::SerializeTuple for SeqSerializer {
+impl ser::SerializeTuple for SeqSerializer {
     type Ok = Value;
     type Error = Error;
 
diff --git a/lang/rust/avro_derive/tests/derive.rs 
b/lang/rust/avro_derive/tests/derive.rs
index 163f9567a..86dd66b65 100644
--- a/lang/rust/avro_derive/tests/derive.rs
+++ b/lang/rust/avro_derive/tests/derive.rs
@@ -823,10 +823,8 @@ mod test_derive {
     }}
 
     #[derive(Debug, Serialize, Deserialize, AvroSchema)]
-    #[allow(unknown_lints)] // Rust 1.51.0 (MSRV) does not support 
#[allow(clippy::box_collection)]
-    #[allow(clippy::box_collection)]
     struct TestSmartPointers<'a> {
-        a: Box<String>,
+        a: String,
         b: Mutex<Vec<i64>>,
         c: Cow<'a, i32>,
     }
@@ -859,13 +857,13 @@ mod test_derive {
         let schema = Schema::parse_str(schema).unwrap();
         assert_eq!(schema, TestSmartPointers::get_schema());
         let test = TestSmartPointers {
-            a: Box::new("hey".into()),
+            a: "hey".into(),
             b: Mutex::new(vec![42]),
             c: Cow::Owned(32),
         };
         // test serde with manual equality for mutex
         let test = serde(test);
-        assert_eq!(Box::new("hey".into()), test.a);
+        assert_eq!("hey", test.a);
         assert_eq!(vec![42], *test.b.borrow().lock().unwrap());
         assert_eq!(Cow::Owned::<i32>(32), test.c);
     }
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 <[email protected]>"]
+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..b248758bc
--- /dev/null
+++ b/lang/rust/wasm-demo/src/lib.rs
@@ -0,0 +1,16 @@
+// 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..89be21f72
--- /dev/null
+++ b/lang/rust/wasm-demo/tests/demos.rs
@@ -0,0 +1,86 @@
+// 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),
+        }
+    }
+}

Reply via email to