diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/uniffi-example-arithmetic | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/uniffi-example-arithmetic')
11 files changed, 227 insertions, 0 deletions
diff --git a/third_party/rust/uniffi-example-arithmetic/.cargo-checksum.json b/third_party/rust/uniffi-example-arithmetic/.cargo-checksum.json new file mode 100644 index 0000000000..21aa0972c9 --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"a4dd6685118bebe4eef690470cf112e51cc862162cf7d4c21c133d452ed7c488","build.rs":"05089b35ac197b5ff83c75872a70df023834141831d0c2051faa35b4c9df55ba","src/arithmetic.udl":"8554c6907ece627645f6b896f71430e5412bf19b0ac6becf63eb9a69868d0f7a","src/lib.rs":"92ebd9fc4d3403ab1960d2d8520c1217971147ea3aebff89228a61fcbb8b2af3","tests/bindings/test_arithmetic.kts":"e0e9347755db4e18f70b1b74c2d5a4aa328373015090ed959b46d65c2a205d92","tests/bindings/test_arithmetic.py":"3e41d69e21e96a6830197c760f3b7bddd754edc0c5515b7bd33b79cccb10f941","tests/bindings/test_arithmetic.rb":"ea0fdce0a4c7b557b427db77521da05240cd6e87d60a128ac2307fab3bbbc76d","tests/bindings/test_arithmetic.swift":"455b87d95fc690af9c35f9e43676e9c855dedddd2fc1c9e1cbaa6a02835c2d4c","tests/test_generated_bindings.rs":"09b0e79c7e769bcf5f3a8b768247a05892d408d48e0295f6737de3c8dab28479","uniffi.toml":"ad149df611a6e3a853a029d90a88a694660f6a4ebe18dcb5f9f503819761dacd"},"package":null}
\ No newline at end of file diff --git a/third_party/rust/uniffi-example-arithmetic/Cargo.toml b/third_party/rust/uniffi-example-arithmetic/Cargo.toml new file mode 100644 index 0000000000..7fe71c9b1a --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "uniffi-example-arithmetic" +edition = "2021" +version = "0.21.0" +authors = ["Firefox Sync Team <sync-team@mozilla.com>"] +license = "MPL-2.0" +publish = false + +[lib] +crate-type = ["lib", "cdylib"] +name = "arithmetical" + +[dependencies] +uniffi_macros = {path = "../../uniffi_macros"} +uniffi = {path = "../../uniffi", features=["builtin-bindgen"]} +thiserror = "1.0" + +[build-dependencies] +uniffi_build = {path = "../../uniffi_build", features=["builtin-bindgen"]} diff --git a/third_party/rust/uniffi-example-arithmetic/build.rs b/third_party/rust/uniffi-example-arithmetic/build.rs new file mode 100644 index 0000000000..b1be1ab9a1 --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/build.rs @@ -0,0 +1,7 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +fn main() { + uniffi_build::generate_scaffolding("./src/arithmetic.udl").unwrap(); +} diff --git a/third_party/rust/uniffi-example-arithmetic/src/arithmetic.udl b/third_party/rust/uniffi-example-arithmetic/src/arithmetic.udl new file mode 100644 index 0000000000..117df4834a --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/src/arithmetic.udl @@ -0,0 +1,16 @@ +[Error] +enum ArithmeticError { + "IntegerOverflow", +}; + +namespace arithmetic { + [Throws=ArithmeticError] + u64 add(u64 a, u64 b); + + [Throws=ArithmeticError] + u64 sub(u64 a, u64 b); + + u64 div(u64 dividend, u64 divisor); + + boolean equal(u64 a, u64 b); +}; diff --git a/third_party/rust/uniffi-example-arithmetic/src/lib.rs b/third_party/rust/uniffi-example-arithmetic/src/lib.rs new file mode 100644 index 0000000000..d26b90fcbb --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/src/lib.rs @@ -0,0 +1,34 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#[derive(Debug, thiserror::Error)] +pub enum ArithmeticError { + #[error("Integer overflow on an operation with {a} and {b}")] + IntegerOverflow { a: u64, b: u64 }, +} + +fn add(a: u64, b: u64) -> Result<u64> { + a.checked_add(b) + .ok_or(ArithmeticError::IntegerOverflow { a, b }) +} + +fn sub(a: u64, b: u64) -> Result<u64> { + a.checked_sub(b) + .ok_or(ArithmeticError::IntegerOverflow { a, b }) +} + +fn div(dividend: u64, divisor: u64) -> u64 { + if divisor == 0 { + panic!("Can't divide by zero"); + } + dividend / divisor +} + +fn equal(a: u64, b: u64) -> bool { + a == b +} + +type Result<T, E = ArithmeticError> = std::result::Result<T, E>; + +uniffi_macros::include_scaffolding!("arithmetic"); diff --git a/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.kts b/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.kts new file mode 100644 index 0000000000..ef11850ae2 --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.kts @@ -0,0 +1,29 @@ +import org.mozilla.uniffi.example.arithmetic.*; + +assert(add(2u, 4u) == 6uL) +assert(add(4u, 8u) == 12uL) + +try { + sub(0u, 2u) + throw RuntimeException("Should have thrown a IntegerOverflow exception!") +} catch (e: ArithmeticException) { + // It's okay! +} + +assert(sub(4u, 2u) == 2uL) +assert(sub(8u, 4u) == 4uL) + +assert(div(8u, 4u) == 2uL) + +try { + div(8u, 0u) + throw RuntimeException("Should have panicked when dividing by zero") +} catch (e: InternalException) { + // It's okay! +} + +assert(equal(2u, 2uL)) +assert(equal(4u, 4uL)) + +assert(!equal(2u, 4uL)) +assert(!equal(4u, 8uL)) diff --git a/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.py b/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.py new file mode 100644 index 0000000000..0d4e666fbf --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.py @@ -0,0 +1,37 @@ +from arithmetic import * + +try: + add(18446744073709551615, 1) + assert(not("Should have thrown a IntegerOverflow exception!")) +except ArithmeticError.IntegerOverflow: + # It's okay! + pass + +assert add(2, 4) == 6 +assert add(4, 8) == 12 + +try: + sub(0, 1) + assert(not("Should have thrown a IntegerOverflow exception!")) +except ArithmeticError.IntegerOverflow: + # It's okay! + pass + +assert sub(4, 2) == 2 +assert sub(8, 4) == 4 + +assert div(8, 4) == 2 + +try: + div(8, 0) +except InternalError: + # It's okay! + pass +else: + assert(not("Should have panicked when dividing by zero")) + +assert equal(2, 2) +assert equal(4, 4) + +assert not equal(2, 4) +assert not equal(4, 8) diff --git a/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.rb b/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.rb new file mode 100644 index 0000000000..6669eb279f --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'test/unit' +require 'arithmetic' + +include Test::Unit::Assertions + +assert_raise Arithmetic::ArithmeticError::IntegerOverflow do + Arithmetic.add 18_446_744_073_709_551_615, 1 +end + +assert_equal Arithmetic.add(2, 4), 6 +assert_equal Arithmetic.add(4, 8), 12 + +assert_raise Arithmetic::ArithmeticError::IntegerOverflow do + Arithmetic.sub 0, 1 +end + +assert_equal Arithmetic.sub(4, 2), 2 +assert_equal Arithmetic.sub(8, 4), 4 +assert_equal Arithmetic.div(8, 4), 2 + +assert_raise Arithmetic::InternalError do + Arithmetic.div 8, 0 +end + +assert Arithmetic.equal(2, 2) +assert Arithmetic.equal(4, 4) + +assert !Arithmetic.equal(2, 4) +assert !Arithmetic.equal(4, 8) diff --git a/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.swift b/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.swift new file mode 100644 index 0000000000..a8e34680e4 --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/tests/bindings/test_arithmetic.swift @@ -0,0 +1,32 @@ +import arithmetic + +do { + let _ = try add(a: 18446744073709551615, b: 1) + fatalError("Should have thrown a IntegerOverflow exception!") +} catch ArithmeticError.IntegerOverflow { + // It's okay! +} + +assert(try! add(a: 2, b: 4) == 6, "add work") +assert(try! add(a: 4, b: 8) == 12, "add work") + +do { + let _ = try sub(a: 0, b: 1) + fatalError("Should have thrown a IntegerOverflow exception!") +} catch ArithmeticError.IntegerOverflow { + // It's okay! +} + +assert(try! sub(a: 4, b: 2) == 2, "sub work") +assert(try! sub(a: 8, b: 4) == 4, "sub work") + +assert(div(dividend: 8, divisor: 4) == 2, "div works") + +// We can't test panicking in Swift because we force unwrap the error in +// `div`, which we can't catch. + +assert(equal(a: 2, b: 2), "equal works") +assert(equal(a: 4, b: 4), "equal works") + +assert(!equal(a: 2, b: 4), "non-equal works") +assert(!equal(a: 4, b: 8), "non-equal works") diff --git a/third_party/rust/uniffi-example-arithmetic/tests/test_generated_bindings.rs b/third_party/rust/uniffi-example-arithmetic/tests/test_generated_bindings.rs new file mode 100644 index 0000000000..2997acc6fc --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/tests/test_generated_bindings.rs @@ -0,0 +1,9 @@ +uniffi_macros::build_foreign_language_testcases!( + ["src/arithmetic.udl",], + [ + "tests/bindings/test_arithmetic.rb", + "tests/bindings/test_arithmetic.py", + "tests/bindings/test_arithmetic.kts", + "tests/bindings/test_arithmetic.swift", + ] +); diff --git a/third_party/rust/uniffi-example-arithmetic/uniffi.toml b/third_party/rust/uniffi-example-arithmetic/uniffi.toml new file mode 100644 index 0000000000..a9fab4c0b4 --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/uniffi.toml @@ -0,0 +1,12 @@ +[bindings.kotlin] +package_name = "org.mozilla.uniffi.example.arithmetic" +cdylib_name = "arithmetical" + +[bindings.python] +cdylib_name = "arithmetical" + +[bindings.ruby] +cdylib_name = "arithmetical" + +[bindings.swift] +cdylib_name = "arithmetical" |