-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #996 from badboy/callbacks-move
Move callbacks testing code into its own fixture crate for testing
- Loading branch information
Showing
14 changed files
with
221 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[bindings.kotlin] | ||
package_name = "uniffi.callbacks" | ||
cdylib_name = "callbacks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "callbacks" | ||
version = "0.12.0" | ||
authors = ["Firefox Sync Team <sync-team@mozilla.com>"] | ||
edition = "2018" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["staticlib", "cdylib"] | ||
name = "uniffi_callbacks" | ||
|
||
[dependencies] | ||
uniffi_macros = {path = "../../uniffi_macros"} | ||
uniffi = {path = "../../uniffi", features=["builtin-bindgen"]} | ||
thiserror = "1.0" | ||
lazy_static = "1.4" | ||
|
||
[build-dependencies] | ||
uniffi_build = {path = "../../uniffi_build", features=["builtin-bindgen"]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# A "Callbacks" test for uniffi components | ||
|
||
This is similar to the `callbacks` example, but it's intended to be contrived and to | ||
ensure we get good test coverage of all possible options. | ||
|
||
It's here so it doesn't even need to make a cursory effort to be a "good" | ||
example; it intentionally panics, asserts params are certain values, has | ||
no-op methods etc. If you're trying to get your head around uniffi then the | ||
"examples" directory will be a much better bet. | ||
|
||
This is its own crate, because the callback mechanism is not implemented for all backends yet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/callbacks.udl").unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
namespace callbacks {}; | ||
|
||
/// These objects are implemented by the foreign language and passed | ||
/// to Rust. Rust then calls methods on it when it needs to. | ||
callback interface ForeignGetters { | ||
boolean get_bool(boolean v, boolean arg2); | ||
string get_string(string v, boolean arg2); | ||
string? get_option(string? v, boolean arg2); | ||
sequence<i32> get_list(sequence<i32> v, boolean arg2); | ||
}; | ||
|
||
/// These objects are implemented in Rust, and call out to `ForeignGetters` | ||
/// to get the value. | ||
interface RustGetters { | ||
boolean get_bool(ForeignGetters callback, boolean v, boolean arg2); | ||
string get_string(ForeignGetters callback, string v, boolean arg2); | ||
string? get_option(ForeignGetters callback, string? v, boolean arg2); | ||
sequence<i32> get_list(ForeignGetters callback, sequence<i32> v, boolean arg2); | ||
}; | ||
|
||
/// These objects are implemented by the foreign language and passed | ||
/// to Rust. Rust then calls methods on it when it needs to. | ||
/// Rust developers need to declare these traits extending `Send` so | ||
/// they can be stored in Rust— i.e. not passed in as an argument to | ||
/// be used immediately. | ||
callback interface StoredForeignStringifier { | ||
string from_simple_type(i32 value); | ||
// Test if types are collected from callback interfaces. | ||
// kotlinc compile time error if not. | ||
string from_complex_type(sequence<f64?>? values); | ||
}; | ||
|
||
/// Rust object that uses the StoredForeignStringifier to produce string representations | ||
/// of passed arguments. | ||
interface RustStringifier { | ||
constructor(StoredForeignStringifier callback); | ||
string from_simple_type(i32 value); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* 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/. */ | ||
|
||
trait ForeignGetters { | ||
fn get_bool(&self, v: bool, arg2: bool) -> bool; | ||
fn get_string(&self, v: String, arg2: bool) -> String; | ||
fn get_option(&self, v: Option<String>, arg2: bool) -> Option<String>; | ||
fn get_list(&self, v: Vec<i32>, arg2: bool) -> Vec<i32>; | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct RustGetters; | ||
|
||
impl RustGetters { | ||
pub fn new() -> Self { | ||
RustGetters | ||
} | ||
fn get_bool(&self, callback: Box<dyn ForeignGetters>, v: bool, arg2: bool) -> bool { | ||
callback.get_bool(v, arg2) | ||
} | ||
fn get_string(&self, callback: Box<dyn ForeignGetters>, v: String, arg2: bool) -> String { | ||
callback.get_string(v, arg2) | ||
} | ||
fn get_option( | ||
&self, | ||
callback: Box<dyn ForeignGetters>, | ||
v: Option<String>, | ||
arg2: bool, | ||
) -> Option<String> { | ||
callback.get_option(v, arg2) | ||
} | ||
fn get_list(&self, callback: Box<dyn ForeignGetters>, v: Vec<i32>, arg2: bool) -> Vec<i32> { | ||
callback.get_list(v, arg2) | ||
} | ||
} | ||
|
||
impl Default for RustGetters { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
// Use `Send+Send` because we want to store the callback in an exposed | ||
// `Send+Sync` object. | ||
trait StoredForeignStringifier: Send + Sync + std::fmt::Debug { | ||
fn from_simple_type(&self, value: i32) -> String; | ||
fn from_complex_type(&self, values: Option<Vec<Option<f64>>>) -> String; | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct RustStringifier { | ||
callback: Box<dyn StoredForeignStringifier>, | ||
} | ||
|
||
impl RustStringifier { | ||
fn new(callback: Box<dyn StoredForeignStringifier>) -> Self { | ||
RustStringifier { callback } | ||
} | ||
|
||
fn from_simple_type(&self, value: i32) -> String { | ||
self.callback.from_simple_type(value) | ||
} | ||
} | ||
|
||
include!(concat!(env!("OUT_DIR"), "/callbacks.uniffi.rs")); |
Oops, something went wrong.