Ty::to_gvalue_setter() - New function to generate g_value_set_*() names

parent a7d4777e
use proc_macro2::{Ident, TokenStream, TokenTree};
use proc_macro2::{Ident, TokenStream};
use hir::{Property, Ty};
use ident_ext::IdentExt;
......@@ -68,51 +68,11 @@ pub fn property_getter<'ast>(properties: &[Property<'ast>]) -> TokenStream {
let type_ = prop.type_;
let body = prop.getter;
// FIXME: (type conversions) The new quote/syn don't have into_tokens() anymore.
// What the following code does is to stringify the prop.type_ and then match on
// the stringified type name. Instead, we need to do something similar to what
// the signals code does. See where hir/mod.rs:extract_inputs() calls
// hir::Ty::extract_from_type(syn::Type). We should do that, and if it succeeds
// (it means we know how to represent the type as a GType), then take further
// action.
//
// Instead of deciding on the value's type by hand here, to then call g_value_set_foo(),
// maybe we can do that with one of glib-rs's built-in conversions?
let type_token_tree: TokenTree = prop.type_.into_tokens().into_iter().nth(0).unwrap();
let tname = match type_token_tree {
TokenTree::Ident(i) => i.to_string(),
_ => String::new(),
};
let g_value_set = match &tname[..] {
"i32" => {
quote! {gobject_ffi::g_value_set_int(value, ret);}
}
"i64" => {
quote! {gobject_ffi::g_value_set_int64(value, ret);}
}
"u32" => {
quote! {gobject_ffi::g_value_set_uint(value, ret);}
}
"u64" => {
quote! {gobject_ffi::g_value_set_uint64(value, ret);}
}
"f32" => {
quote! {gobject_ffi::g_value_set_float(value, ret);}
}
"f64" => {
quote! {gobject_ffi::g_value_set_double(value, ret);}
}
"bool" => {
quote! {gobject_ffi::g_value_set_bool(value, ret);}
}
"String" => {
quote! {gobject_ffi::g_value_set_string(value, ret.to_glib_none().0);}
}
_ => {
quote! {gobject_ffi::g_value_set_object(value, ret.to_glib_none().0);}
}
};
// FIXME: Don't unwrap() here; instead, store a hir::Ty in the property instead of
// a syn::Type, i.e. something that we already checked that can be represented by
// Glib.
let ty = Ty::extract_from_type(&prop.type_).unwrap();
let g_value_set = ty.to_gvalue_setter();
quote! {
#index => {
......
......@@ -568,6 +568,32 @@ impl<'ast> Ty<'ast> {
Ty::Owned(_) => unimplemented!(),
}
}
pub fn to_gvalue_setter(&self) -> TokenStream {
match *self {
Ty::Unit => unreachable!("there is no g_value_set_*() for the Unit type"),
Ty::Char(_) => quote! { gobject_ffi::g_value_set_uint }, // <char as ToGlib>::GlibType = u32
Ty::Bool(_) => quote! { gobject_ffi::g_value_set_bool },
Ty::Borrowed(_) => unimplemented!(),
Ty::Integer(ref ident) => match ident.to_owned_string().as_str() {
"i8" | "u8" => quote! { gobject_ffi::g_value_set_char },
"i16" => unimplemented!("should we promote i16 to i32?"),
"i32" => quote! { gobject_ffi::g_value_set_int },
"i64" => quote! { gobject_ffi::g_value_set_int64 },
"isize" => unimplemented!(),
"u16" => unimplemented!("should we promote u16 to u32?"),
"u32" => quote! { gobject_ffi::g_value_set_uint },
"u64" => quote! { gobject_ffi::g_value_set_uint64 },
"usize" => unimplemented!(),
_ => unreachable!(),
},
Ty::Owned(_) => unimplemented!(),
}
}
}
impl<'ast> Property<'ast> {
......
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