summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi-example-geometry/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 00:47:55 +0000
commit26a029d407be480d791972afb5975cf62c9360a6 (patch)
treef435a8308119effd964b339f76abb83a57c29483 /third_party/rust/uniffi-example-geometry/src
parentInitial commit. (diff)
downloadfirefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz
firefox-26a029d407be480d791972afb5975cf62c9360a6.zip
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/uniffi-example-geometry/src')
-rw-r--r--third_party/rust/uniffi-example-geometry/src/geometry.udl15
-rw-r--r--third_party/rust/uniffi-example-geometry/src/lib.rs47
2 files changed, 62 insertions, 0 deletions
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..d710b150bb
--- /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,
+ })
+}
+
+uniffi::include_scaffolding!("geometry");