diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /third_party/rust/uniffi-example-arithmetic | |
parent | Initial commit. (diff) | |
download | firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.tar.xz firefox-esr-0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/uniffi-example-arithmetic')
11 files changed, 244 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..65c9cc95fa --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"33bc6eceebec9d3aa6e97e1481ba74f2e1949d0b9a5edd8b978be548713ee338","build.rs":"937e2a5fdb7c0e850af07a4d7676e57f62f06b842cbc0aa7291ffe58b5725caa","src/arithmetic.udl":"8554c6907ece627645f6b896f71430e5412bf19b0ac6becf63eb9a69868d0f7a","src/lib.rs":"c454193443e92d49f997c760f4131192fb66bf213bbac1710c1ebde19e765e5d","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":"26b92d6b3e648f6fadd4182cbdba4f412b73da48a789785fd98cd486b29abf05","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..bac35f5d80 --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/Cargo.toml @@ -0,0 +1,39 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2021" +name = "uniffi-example-arithmetic" +version = "0.22.0" +authors = ["Firefox Sync Team <sync-team@mozilla.com>"] +publish = false +license = "MPL-2.0" + +[lib] +name = "arithmetical" +crate-type = [ + "lib", + "cdylib", +] + +[dependencies] +thiserror = "1.0" + +[dependencies.uniffi] +path = "../../uniffi" + +[dev-dependencies.uniffi] +path = "../../uniffi" +features = ["bindgen-tests"] + +[build-dependencies.uniffi] +path = "../../uniffi" +features = ["build"] 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..fcf5fe5b5c --- /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::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..92ab8c072b --- /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::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..168e6e1d4c --- /dev/null +++ b/third_party/rust/uniffi-example-arithmetic/tests/test_generated_bindings.rs @@ -0,0 +1,6 @@ +uniffi::build_foreign_language_testcases!( + "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" |