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-geometry | |
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-geometry')
11 files changed, 144 insertions, 0 deletions
diff --git a/third_party/rust/uniffi-example-geometry/.cargo-checksum.json b/third_party/rust/uniffi-example-geometry/.cargo-checksum.json new file mode 100644 index 0000000000..640bee2540 --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.toml":"ae167d6d92c007dac4db511c1b9ea2e4c7a7d6edb8f2fa95463b877825a3395a","build.rs":"94ca4e70e538a2159cb77157c227d487973756373c4b27ea5cea05a6698cb60b","src/geometry.udl":"7da7a6ec080c7117ec3c25206e23f9ed436e60b1a26fba34f991547680443550","src/lib.rs":"be9c624f691a8d1ebe3be6dbbcde44033759b8321a3a298453018dd69b9c14c7","tests/bindings/test_geometry.kts":"e537185e3c699df1c0468525700e8a38f9a504b2a663c38442146b951e38e9a7","tests/bindings/test_geometry.py":"3ea483b8a4fbe13aefa6641177ae149f75f734bc32bf0da533b97c1abf3dc317","tests/bindings/test_geometry.rb":"17c2fe8a7b477419a6646983dd88f1b07a0304b58a568c03e9bfa640d5b2df5c","tests/bindings/test_geometry.swift":"a61fec6bfe16020809e20e4da372748c24366767138c5672a0bfff85c4b62d78","tests/test_generated_bindings.rs":"8ba67396105b96cc554f78078c7a8c6e8ce86ecc868d97a069a5f60fd87c1a36","uniffi.toml":"5b28f45d3c2581a52cf886a502f034778a002815b66994e5da2081a5c9f5284b"},"package":null}
\ No newline at end of file diff --git a/third_party/rust/uniffi-example-geometry/Cargo.toml b/third_party/rust/uniffi-example-geometry/Cargo.toml new file mode 100644 index 0000000000..ac84351ddb --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "uniffi-example-geometry" +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 = "uniffi_geometry" + +[dependencies] +uniffi_macros = {path = "../../uniffi_macros"} +uniffi = {path = "../../uniffi", features=["builtin-bindgen"]} + +[build-dependencies] +uniffi_build = {path = "../../uniffi_build", features=["builtin-bindgen"]} diff --git a/third_party/rust/uniffi-example-geometry/build.rs b/third_party/rust/uniffi-example-geometry/build.rs new file mode 100644 index 0000000000..716ccd4908 --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/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/geometry.udl").unwrap(); +} diff --git a/third_party/rust/uniffi-example-geometry/src/geometry.udl b/third_party/rust/uniffi-example-geometry/src/geometry.udl new file mode 100644 index 0000000000..af60d429bf --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/src/geometry.udl @@ -0,0 +1,15 @@ + +namespace geometry { + double gradient(Line ln); + Point? intersection(Line ln1, Line ln2); +}; + +dictionary Point { + double coord_x; + double coord_y; +}; + +dictionary Line { + Point start; + Point end; +}; diff --git a/third_party/rust/uniffi-example-geometry/src/lib.rs b/third_party/rust/uniffi-example-geometry/src/lib.rs new file mode 100644 index 0000000000..0e6add54f2 --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/src/lib.rs @@ -0,0 +1,47 @@ +/* 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/. */ + +// https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp +// Silence, clippy! +const EPSILON: f64 = 0.0001f64; + +#[derive(Debug, Clone)] +pub struct Point { + coord_x: f64, + coord_y: f64, +} + +#[derive(Debug, Clone)] +pub struct Line { + start: Point, + end: Point, +} + +pub fn gradient(ln: Line) -> f64 { + let rise = ln.end.coord_y - ln.start.coord_y; + let run = ln.end.coord_x - ln.start.coord_x; + rise / run +} + +pub fn intersection(ln1: Line, ln2: Line) -> Option<Point> { + // TODO: yuck, should be able to take &Line as argument here + // and have rust figure it out with a bunch of annotations... + let g1 = gradient(ln1.clone()); + let z1 = ln1.start.coord_y - g1 * ln1.start.coord_x; + let g2 = gradient(ln2.clone()); + let z2 = ln2.start.coord_y - g2 * ln2.start.coord_x; + // Parallel lines do not intersect. + if (g1 - g2).abs() < EPSILON { + return None; + } + // Otherwise, they intersect at this fancy calculation that + // I found on wikipedia. + let x = (z2 - z1) / (g1 - g2); + Some(Point { + coord_x: x, + coord_y: g1 * x + z1, + }) +} + +include!(concat!(env!("OUT_DIR"), "/geometry.uniffi.rs")); diff --git a/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.kts b/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.kts new file mode 100644 index 0000000000..77bb9932ec --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.kts @@ -0,0 +1,10 @@ +import uniffi.geometry.*; + +val ln1 = Line(Point(0.0,0.0), Point(1.0,2.0)) +val ln2 = Line(Point(1.0,1.0), Point(2.0,2.0)) + +assert( gradient(ln1) == 2.0 ) +assert( gradient(ln2) == 1.0 ) + +assert( intersection(ln1, ln2) == Point(0.0, 0.0) ) +assert( intersection(ln1, ln1) == null ) diff --git a/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.py b/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.py new file mode 100644 index 0000000000..a40e2af6ec --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.py @@ -0,0 +1,10 @@ +from geometry import * + +ln1 = Line(Point(0,0), Point(1,2)) +ln2 = Line(Point(1,1), Point(2,2)) + +assert gradient(ln1) == 2 +assert gradient(ln2) == 1 + +assert intersection(ln1, ln2) == Point(0, 0) +assert intersection(ln1, ln1) is None diff --git a/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.rb b/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.rb new file mode 100644 index 0000000000..8b1280d823 --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require 'test/unit' +require 'geometry' + +include Test::Unit::Assertions +include Geometry + +ln1 = Line.new(Point.new(0.0, 0.0), Point.new(1.0, 2.0)) +ln2 = Line.new(Point.new(1.0, 1.0), Point.new(2.0, 2.0)) + +assert_equal Geometry.gradient(ln1), 2 +assert_equal Geometry.gradient(ln2), 1 + +assert_equal Geometry.intersection(ln1, ln2), Point.new(0, 0) +assert Geometry.intersection(ln1, ln1).nil? diff --git a/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.swift b/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.swift new file mode 100644 index 0000000000..58bd65607f --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/tests/bindings/test_geometry.swift @@ -0,0 +1,10 @@ +import geometry + +let ln1 = Line(start: Point(coordX: 0, coordY: 0), end: Point(coordX: 1, coordY: 2)) +let ln2 = Line(start: Point(coordX: 1, coordY: 1), end: Point(coordX: 2, coordY: 2)) + +assert(gradient(ln: ln1) == 2.0) +assert(gradient(ln: ln2) == 1.0) + +assert(intersection(ln1: ln1, ln2: ln2) == Point(coordX: 0, coordY: 0)) +assert(intersection(ln1: ln1, ln2: ln1) == nil) diff --git a/third_party/rust/uniffi-example-geometry/tests/test_generated_bindings.rs b/third_party/rust/uniffi-example-geometry/tests/test_generated_bindings.rs new file mode 100644 index 0000000000..0c712ff7c0 --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/tests/test_generated_bindings.rs @@ -0,0 +1,9 @@ +uniffi_macros::build_foreign_language_testcases!( + ["src/geometry.udl",], + [ + "tests/bindings/test_geometry.py", + "tests/bindings/test_geometry.rb", + "tests/bindings/test_geometry.kts", + "tests/bindings/test_geometry.swift", + ] +); diff --git a/third_party/rust/uniffi-example-geometry/uniffi.toml b/third_party/rust/uniffi-example-geometry/uniffi.toml new file mode 100644 index 0000000000..969379365a --- /dev/null +++ b/third_party/rust/uniffi-example-geometry/uniffi.toml @@ -0,0 +1 @@ +[bindings.swift] |