-
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.
Allow Rust traits to be exposed as an interface.
- Loading branch information
Showing
38 changed files
with
578 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
[package] | ||
name = "uniffi-example-traits" | ||
edition = "2021" | ||
version = "0.22.0" | ||
authors = ["Firefox Sync Team <sync-team@mozilla.com>"] | ||
license = "MPL-2.0" | ||
publish = false | ||
|
||
[lib] | ||
crate-type = ["lib", "cdylib"] | ||
name = "uniffi_traits" | ||
|
||
[dependencies] | ||
uniffi = {path = "../../uniffi"} | ||
thiserror = "1.0" | ||
|
||
[build-dependencies] | ||
uniffi = {path = "../../uniffi", features = ["build"] } | ||
|
||
[dev-dependencies] | ||
uniffi = {path = "../../uniffi", features = ["bindgen-tests"] } | ||
|
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::generate_scaffolding("./src/traits.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,36 @@ | ||
/* 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 std::sync::Arc; | ||
|
||
// namespace functions. | ||
fn get_buttons() -> Vec<Arc<dyn Button>> { | ||
vec![Arc::new(StopButton {}), Arc::new(GoButton {})] | ||
} | ||
|
||
fn press(button: Arc<dyn Button>) -> Arc<dyn Button> { | ||
button | ||
} | ||
|
||
pub trait Button: Send + Sync { | ||
fn name(&self) -> String; | ||
} | ||
|
||
struct GoButton {} | ||
|
||
impl Button for GoButton { | ||
fn name(&self) -> String { | ||
"go".to_string() | ||
} | ||
} | ||
|
||
struct StopButton {} | ||
|
||
impl Button for StopButton { | ||
fn name(&self) -> String { | ||
"stop".to_string() | ||
} | ||
} | ||
|
||
include!(concat!(env!("OUT_DIR"), "/traits.uniffi.rs")); |
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,12 @@ | ||
namespace traits { | ||
// Get all the buttons we can press. | ||
sequence<Button> get_buttons(); | ||
// press a button and return it. | ||
Button press(Button button); | ||
}; | ||
|
||
// This is a trait in Rust. | ||
[Trait] | ||
interface Button { | ||
string name(); | ||
}; |
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 @@ | ||
from traits import * | ||
|
||
for button in get_buttons(): | ||
if button.name() in ["go", "stop"]: | ||
press(button) | ||
else: | ||
print("unknown button", button) |
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 @@ | ||
uniffi::build_foreign_language_testcases!("tests/bindings/test_traits.py",); |
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,9 @@ | ||
[bindings.kotlin] | ||
package_name = "uniffi.traits" | ||
cdylib_name = "uniffi_traits" | ||
|
||
[bindings.swift] | ||
cdylib_name = "uniffi_traits" | ||
|
||
[bindings.python] | ||
cdylib_name = "uniffi_traits" |
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,74 @@ | ||
/* 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 std::sync::{Arc, Mutex}; | ||
|
||
// namespace functions. | ||
pub fn get_traits() -> Vec<Arc<dyn TestTrait>> { | ||
vec![ | ||
Arc::new(Trait1 { | ||
..Default::default() | ||
}), | ||
Arc::new(Trait2 {}), | ||
] | ||
} | ||
|
||
pub trait TestTrait: Send + Sync + std::fmt::Debug { | ||
fn name(&self) -> String; | ||
|
||
fn number(self: Arc<Self>) -> u64; | ||
|
||
fn strong_count(self: Arc<Self>) -> u64 { | ||
Arc::strong_count(&self) as u64 | ||
} | ||
|
||
fn take_other(&self, other: Option<Arc<dyn TestTrait>>); | ||
|
||
fn get_other(&self) -> Option<Arc<dyn TestTrait>>; | ||
} | ||
|
||
#[derive(Debug, Default)] | ||
pub(crate) struct Trait1 { | ||
// A reference to another trait. | ||
other: Mutex<Option<Arc<dyn TestTrait>>>, | ||
} | ||
|
||
impl TestTrait for Trait1 { | ||
fn name(&self) -> String { | ||
"trait 1".to_string() | ||
} | ||
|
||
fn number(self: Arc<Self>) -> u64 { | ||
1_u64 | ||
} | ||
|
||
fn take_other(&self, other: Option<Arc<dyn TestTrait>>) { | ||
*self.other.lock().unwrap() = other.map(|arc| Arc::clone(&arc)) | ||
} | ||
|
||
fn get_other(&self) -> Option<Arc<dyn TestTrait>> { | ||
(*self.other.lock().unwrap()).as_ref().map(Arc::clone) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct Trait2 {} | ||
impl TestTrait for Trait2 { | ||
fn name(&self) -> String { | ||
"trait 2".to_string() | ||
} | ||
|
||
fn number(self: Arc<Self>) -> u64 { | ||
2_u64 | ||
} | ||
|
||
// Don't bother implementing these here - the test on the struct above is ok. | ||
fn take_other(&self, _other: Option<Arc<dyn TestTrait>>) { | ||
unimplemented!(); | ||
} | ||
|
||
fn get_other(&self) -> Option<Arc<dyn TestTrait>> { | ||
unimplemented!() | ||
} | ||
} |
Oops, something went wrong.