Commit 0a932459 authored by Alex Crichton's avatar Alex Crichton

Don't generate `-> ()` in `instance_ext`

Be sure to special case `Unit` return types to avoid generating these
unnecessary tokens!

Closes #41
parent 7be6b13a
use proc_macro2::TokenStream;
use quote::ToTokens;
use hir::{Method, Slot, VirtualMethod};
use hir::{Method, Slot, Ty, VirtualMethod};
use super::signals;
use hir::names::Names;
......@@ -32,9 +33,9 @@ pub fn slot_trait_fns<'ast>(slots: &[Slot<'ast>]) -> Vec<TokenStream> {
| Slot::VirtualMethod(VirtualMethod { ref sig, .. }) => {
let name = &sig.name;
let inputs = &sig.inputs;
let output = &sig.output;
let output = ToReturnType(&sig.output);
Some(quote! {
fn #name(#(#inputs),*) -> #output;
fn #name(#(#inputs),*) #output;
})
}
......@@ -42,9 +43,9 @@ pub fn slot_trait_fns<'ast>(slots: &[Slot<'ast>]) -> Vec<TokenStream> {
let connect_signalname = signals::connect_signalname(signal);
let sig = &signal.sig;
let input_types = signal.sig.input_arg_types();
let output = &sig.output;
let output = ToReturnType(&sig.output);
Some(quote! {
fn #connect_signalname<F: Fn(&Self, #input_types) -> #output + 'static>(&self, f: F) ->
fn #connect_signalname<F: Fn(&Self, #input_types) #output + 'static>(&self, f: F) ->
glib::SignalHandlerId;
})
}
......@@ -77,9 +78,9 @@ pub fn slot_trait_impls<'ast>(names: &Names, slots: &[Slot<'ast>]) -> Vec<TokenS
};
let output_from = sig.ret_from_glib_fn(&value);
let inputs = &sig.inputs;
let output = &sig.output;
let output = ToReturnType(&sig.output);
Some(quote! {
fn #name(#(#inputs),*) -> #output {
fn #name(#(#inputs),*) #output {
#output_from
}
})
......@@ -91,14 +92,14 @@ pub fn slot_trait_impls<'ast>(names: &Names, slots: &[Slot<'ast>]) -> Vec<TokenS
let sig = &signal.sig;
let signalname_str = format!("{}", sig.name);
let input_types = signal.sig.input_arg_types();
let output = &sig.output;
let output = ToReturnType(&sig.output);
Some(quote! {
fn #connect_signalname<F: Fn(&Self, #input_types) -> #output + 'static>(&self, f: F) ->
fn #connect_signalname<F: Fn(&Self, #input_types) #output + 'static>(&self, f: F) ->
glib::SignalHandlerId
{
unsafe {
let f: Box<Box<Fn(&Self, #input_types) -> #output + 'static>> =
let f: Box<Box<Fn(&Self, #input_types) #output + 'static>> =
Box::new(Box::new(f));
glib::signal::connect(self.to_glib_none().0,
......@@ -112,3 +113,14 @@ pub fn slot_trait_impls<'ast>(names: &Names, slots: &[Slot<'ast>]) -> Vec<TokenS
})
.collect()
}
struct ToReturnType<'a>(&'a Ty<'a>);
impl<'a> ToTokens for ToReturnType<'a> {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self.0 {
Ty::Unit => {}
other => (quote! { -> #other }).to_tokens(tokens),
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment