summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-fixtures/custom-types/src
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:35:37 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:35:37 +0000
commita90a5cba08fdf6c0ceb95101c275108a152a3aed (patch)
tree532507288f3defd7f4dcf1af49698bcb76034855 /toolkit/components/uniffi-fixtures/custom-types/src
parentAdding debian version 126.0.1-1. (diff)
downloadfirefox-a90a5cba08fdf6c0ceb95101c275108a152a3aed.tar.xz
firefox-a90a5cba08fdf6c0ceb95101c275108a152a3aed.zip
Merging upstream version 127.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/uniffi-fixtures/custom-types/src')
-rw-r--r--toolkit/components/uniffi-fixtures/custom-types/src/custom-types.udl14
-rw-r--r--toolkit/components/uniffi-fixtures/custom-types/src/lib.rs52
2 files changed, 66 insertions, 0 deletions
diff --git a/toolkit/components/uniffi-fixtures/custom-types/src/custom-types.udl b/toolkit/components/uniffi-fixtures/custom-types/src/custom-types.udl
new file mode 100644
index 0000000000..8fef8ff41a
--- /dev/null
+++ b/toolkit/components/uniffi-fixtures/custom-types/src/custom-types.udl
@@ -0,0 +1,14 @@
+[Custom]
+typedef string Url;
+
+[Custom]
+typedef i64 Handle;
+
+dictionary CustomTypesDemo {
+ Url url;
+ Handle handle;
+};
+
+namespace custom_types {
+ CustomTypesDemo get_custom_types_demo(CustomTypesDemo? demo);
+};
diff --git a/toolkit/components/uniffi-fixtures/custom-types/src/lib.rs b/toolkit/components/uniffi-fixtures/custom-types/src/lib.rs
new file mode 100644
index 0000000000..61aea7a11a
--- /dev/null
+++ b/toolkit/components/uniffi-fixtures/custom-types/src/lib.rs
@@ -0,0 +1,52 @@
+/* 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/. */
+
+use url::Url;
+
+// Custom Handle type which trivially wraps an i64.
+pub struct Handle(pub i64);
+
+// We must implement the UniffiCustomTypeConverter trait for each custom type on the scaffolding side
+impl UniffiCustomTypeConverter for Handle {
+ // The `Builtin` type will be used to marshall values across the FFI
+ type Builtin = i64;
+
+ // Convert Builtin to our custom type
+ fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
+ Ok(Handle(val))
+ }
+
+ // Convert our custom type to Builtin
+ fn from_custom(obj: Self) -> Self::Builtin {
+ obj.0
+ }
+}
+
+// Use `url::Url` as a custom type, with `String` as the Builtin
+impl UniffiCustomTypeConverter for Url {
+ type Builtin = String;
+
+ fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
+ Ok(Url::parse(&val)?)
+ }
+
+ fn from_custom(obj: Self) -> Self::Builtin {
+ obj.to_string()
+ }
+}
+
+// And a little struct and function that ties them together.
+pub struct CustomTypesDemo {
+ url: Url,
+ handle: Handle,
+}
+
+pub fn get_custom_types_demo(v: Option<CustomTypesDemo>) -> CustomTypesDemo {
+ v.unwrap_or_else(|| CustomTypesDemo {
+ url: Url::parse("http://example.com/").unwrap(),
+ handle: Handle(123),
+ })
+}
+
+include!(concat!(env!("OUT_DIR"), "/custom-types.uniffi.rs"));