Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interface: Allow marking as sync so signals can be emitted from any t… #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions core/interface.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{
util::{self, Errors},
TypeBase, TypeDefinition, TypeMode,
Concurrency, TypeBase, TypeDefinition, TypeMode,
};
use darling::{util::PathList, FromMeta};
use darling::{util::{PathList, Flag}, FromMeta};
use heck::ToUpperCamelCase;
use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote, ToTokens};
Expand All @@ -19,6 +19,7 @@ struct Attrs {
pub parent_trait: Option<syn::TypePath>,
pub wrapper: Option<bool>,
pub requires: PathList,
pub sync: Flag,
}

#[derive(Debug)]
Expand Down Expand Up @@ -51,9 +52,13 @@ impl InterfaceDefinition {
) -> Self {
let attrs = opts.0;

let inner =
let mut inner =
TypeDefinition::parse(module, TypeBase::Interface, attrs.name, crate_path, errors);

if attrs.sync.is_some() {
inner.concurrency = Concurrency::SendSync;
}

let name = inner.name.clone();
Self {
inner,
Expand Down
35 changes: 27 additions & 8 deletions tests/interface.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[gobject::interface]
#[gobject::interface(sync)]
mod iface {
use std::marker::PhantomData;
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -30,34 +30,36 @@ mod iface {

#[gobject::class(final, implements(Dummy))]
mod implement {
use std::cell::Cell;
use std::sync::Mutex;

#[derive(Default)]
pub struct Implementor {
#[property(get, set, override_iface = "super::Dummy")]
my_prop: Cell<u64>,
my_prop: Mutex<u64>,
#[property(get, set, explicit_notify, lax_validation,
builder(minimum = -10, maximum = 10))]
my_auto_prop: Cell<i64>,
my_auto_prop: Mutex<i64>,
}
impl super::DummyImpl for Implementor {}
}

#[gobject::class(final, implements(Dummy))]
mod implement2 {
use super::{DummyExt, DummyImplExt};
use std::sync::Mutex;

use std::cell::Cell;
#[derive(Default)]
pub struct Implementor2 {
#[property(get, set, override_iface = "super::Dummy")]
my_prop: Cell<u64>,
my_prop: Mutex<u64>,
}
impl Implementor2 {
#[signal(override)]
fn my_sig(&self, hello: u64) {
self.parent_my_sig(55);
assert_eq!(self.my_prop.get(), 55);
self.my_prop.set(hello + 22);
assert_eq!(*self.my_prop.lock().unwrap(), 55);
let mut v = self.my_prop.lock().unwrap();
*v = hello + 22;
}
}
impl super::DummyImpl for Implementor2 {
Expand All @@ -72,17 +74,34 @@ mod implement2 {

#[test]
fn interface() {
use std::sync::Arc;
use std::sync::Mutex;

let obj = glib::Object::new::<Implementor>(&[]).unwrap();
obj.set_my_prop(4000);
obj.set_my_auto_prop(-5);
obj.emit_my_sig(123);
assert_eq!(obj.my_prop(), 123);
assert_eq!(obj.my_virt(&obj), 223);
assert_eq!(obj.my_virt2(1), 124);

let obj = glib::Object::new::<Implementor2>(&[]).unwrap();
obj.emit_my_sig(133);
assert_eq!(obj.my_prop(), 155);
assert_eq!(obj.my_virt(&obj), 610);
assert_eq!(obj.dosomething(), "155");
assert_eq!(obj.my_virt2(1), 157);

let called_signals: Arc<Mutex<Vec<String>>> = Default::default();
obj.connect_my_sig(glib::clone!(@strong called_signals => move |_obj: &Implementor2, v: u64| {
assert_eq!(v, 0);
called_signals.lock().unwrap().push("my".to_owned());
}));
std::thread::spawn(glib::clone!(@strong obj => move || {
obj.emit_my_sig(0);
}))
.join()
.unwrap();

assert_eq!(*called_signals.lock().unwrap(), &["my"]);
}