-
Notifications
You must be signed in to change notification settings - Fork 130
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 #445 from Snarpix/add-user-types-in-generated-code
Add annotation to specify user types in generated code
- Loading branch information
Showing
13 changed files
with
249 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod user_type; |
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 @@ | ||
use dbus::arg::{Append, IterAppend, Iter, Arg, ArgType, Get, RefArg}; | ||
use dbus::strings::Signature; | ||
use std::any; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub struct MyType | ||
{ | ||
f: (String, String), | ||
} | ||
|
||
impl MyType { | ||
pub fn new(s1: String, s2: String) -> Self { | ||
MyType{f:(s1, s2)} | ||
} | ||
} | ||
|
||
impl Append for MyType { | ||
fn append_by_ref(&self, i: &mut IterAppend) { | ||
self.f.append_by_ref(i); | ||
} | ||
} | ||
|
||
impl Arg for MyType { | ||
const ARG_TYPE: ArgType = ArgType::Struct; | ||
fn signature() -> Signature<'static> { | ||
Signature::new("(ss)".to_string()).unwrap() | ||
} | ||
} | ||
|
||
impl<'a> Get<'a> for MyType { | ||
fn get(i: &mut Iter<'a>) -> Option<MyType> { | ||
let f = <(String, String)>::get(i)?; | ||
Some(MyType{f}) | ||
} | ||
} | ||
|
||
impl RefArg for MyType { | ||
fn arg_type(&self) -> ArgType { | ||
<MyType as Arg>::ARG_TYPE | ||
} | ||
fn signature(&self) -> Signature<'static> { | ||
<MyType as Arg>::signature() | ||
} | ||
fn append(&self, i: &mut IterAppend) { | ||
<MyType as Append>::append_by_ref(self, i) | ||
} | ||
fn as_any(&self) -> &dyn any::Any { | ||
self | ||
} | ||
fn as_any_mut(&mut self) -> &mut dyn any::Any { | ||
self | ||
} | ||
fn as_i64(&self) -> Option<i64> { | ||
None | ||
} | ||
fn as_u64(&self) -> Option<u64> { | ||
None | ||
} | ||
fn as_f64(&self) -> Option<f64> { | ||
None | ||
} | ||
fn as_str(&self) -> Option<&str> { | ||
None | ||
} | ||
fn box_clone(&self) -> Box<dyn RefArg + 'static> { | ||
Box::new(self.clone()) | ||
} | ||
fn array_clone(v: &[Self]) -> Option<Box<dyn RefArg + 'static>> | ||
where | ||
Self: Sized, | ||
{ | ||
Some(Box::new(v.to_vec())) | ||
} | ||
} |
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,81 @@ | ||
mod user_types_dbus; | ||
|
||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::time::Duration; | ||
use dbus::blocking::Connection; | ||
use dbus::channel::MatchingReceiver; | ||
use dbus_crossroads::Crossroads; | ||
|
||
use codegen_tests::user_type::MyType; | ||
use user_types_dbus::*; | ||
|
||
struct MyServer { | ||
t: MyType, | ||
} | ||
|
||
impl MyServer { | ||
fn new() -> Self { | ||
MyServer{t: MyType::new("123".to_string(), "456".to_string())} | ||
} | ||
} | ||
|
||
impl user_types_cr::ComExampleMyService1InterestingInterface for MyServer { | ||
fn method1(&mut self, arg1: codegen_tests::user_type::MyType) -> Result<codegen_tests::user_type::MyType, dbus::MethodErr>{ | ||
return Ok(arg1); | ||
} | ||
fn bar(&self) -> Result<codegen_tests::user_type::MyType, dbus::MethodErr> { | ||
return Ok(self.t.clone()); | ||
} | ||
fn set_bar(&self, _value: codegen_tests::user_type::MyType) -> Result<(), dbus::MethodErr> { | ||
return Ok(()); | ||
} | ||
} | ||
|
||
|
||
#[test] | ||
fn test_cr() { | ||
let c = Connection::new_session().unwrap(); | ||
let cname = "com.example.dbustest"; | ||
c.request_name(cname, false, true, false).unwrap(); | ||
let mut cr = Crossroads::new(); | ||
let token = user_types_cr::register_com_example_my_service1_interesting_interface::<MyServer>(&mut cr); | ||
let server = MyServer::new(); | ||
cr.insert("/", &[token], server); | ||
c.start_receive(dbus::message::MatchRule::new_method_call(), Box::new(move |msg, conn| { | ||
cr.handle_message(msg, conn).unwrap(); | ||
true | ||
})); | ||
|
||
let quit = std::sync::Arc::new(AtomicBool::new(false)); | ||
let quit2 = quit.clone(); | ||
|
||
let _ = std::thread::spawn(move || { | ||
{ | ||
let c2 = dbus::blocking::Connection::new_session().unwrap(); | ||
let p = c2.with_proxy(cname, "/", std::time::Duration::from_millis(1000)); | ||
use user_types_blocking::ComExampleMyService1InterestingInterface; | ||
let arg = MyType::new("abc".to_string(), "cdf".to_string()); | ||
assert_eq!(p.method1(&arg).unwrap(), arg); | ||
} | ||
|
||
let rt = tokio::runtime::Runtime::new().unwrap(); | ||
rt.block_on(async { | ||
let (resource, conn) = dbus_tokio::connection::new_session_sync().unwrap(); | ||
let _handle = tokio::spawn(async { | ||
let err = resource.await; | ||
panic!("Lost connection to D-Bus: {}", err); | ||
}); | ||
let p = dbus::nonblock::Proxy::new(cname, "/", Duration::from_secs(2), conn); | ||
use user_types_nonblock::ComExampleMyService1InterestingInterface; | ||
let arg = MyType::new("abc".to_string(), "cdf".to_string()); | ||
assert_eq!(p.method1(&arg).await.unwrap(), arg); | ||
}); | ||
|
||
quit2.store(true, Ordering::SeqCst); | ||
}); | ||
|
||
loop { | ||
c.process(std::time::Duration::from_millis(1000)).unwrap(); | ||
if quit.load(Ordering::SeqCst) { break; } | ||
} | ||
} |
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,4 @@ | ||
pub mod user_types_blocking; | ||
pub mod user_types_client; | ||
pub mod user_types_cr; | ||
pub mod user_types_nonblock; |
3 changes: 3 additions & 0 deletions
3
dbus-codegen-tests/tests/user_types_dbus/user_types_blocking.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,3 @@ | ||
#![allow(dead_code)] | ||
#![deny(trivial_casts)] | ||
include!(concat!(env!("OUT_DIR"), "/user_types_blocking.rs")); |
3 changes: 3 additions & 0 deletions
3
dbus-codegen-tests/tests/user_types_dbus/user_types_client.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,3 @@ | ||
#![allow(dead_code)] | ||
#![deny(trivial_casts)] | ||
include!(concat!(env!("OUT_DIR"), "/user_types_client.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,3 @@ | ||
#![allow(dead_code)] | ||
#![deny(trivial_casts)] | ||
include!(concat!(env!("OUT_DIR"), "/user_types_cr.rs")); |
3 changes: 3 additions & 0 deletions
3
dbus-codegen-tests/tests/user_types_dbus/user_types_nonblock.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,3 @@ | ||
#![allow(dead_code)] | ||
#![deny(trivial_casts)] | ||
include!(concat!(env!("OUT_DIR"), "/user_types_nonblock.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
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