From 466b0c88d95da5ef2112f8bfd7ef4f60ee40d833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 17 Mar 2020 15:53:14 +0200 Subject: [PATCH] Generate code for new from_glib_borrow signature See https://github.com/gtk-rs/glib/pull/605 --- src/codegen/function_body_chunk.rs | 25 ++++++++++++++++++++----- src/codegen/trampoline_from_glib.rs | 13 +++++++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/codegen/function_body_chunk.rs b/src/codegen/function_body_chunk.rs index 2c3d383fe..77374f9e2 100644 --- a/src/codegen/function_body_chunk.rs +++ b/src/codegen/function_body_chunk.rs @@ -414,14 +414,17 @@ impl Builder { add_chunk_for_type(env, par.typ, par, &mut body, &ty_name, nullable); if ty_name == "GString" { if *nullable { - arguments.push(Chunk::Name(format!("{}.map(|x| x.as_str())", par.name))); + arguments.push(Chunk::Name(format!( + "{}.as_ref().map(|x| x.as_str())", + par.name + ))); } else { arguments.push(Chunk::Name(format!("{}.as_str()", par.name))); } continue; } if *nullable && !is_fundamental { - arguments.push(Chunk::Name(format!("{}.as_ref()", par.name))); + arguments.push(Chunk::Name(format!("{}.as_ref().as_ref()", par.name))); continue; } arguments.push(Chunk::Name(format!( @@ -1340,12 +1343,24 @@ fn add_chunk_for_type( let type_name; if ty_name == "GString" { if *nullable { - type_name = String::from(": Option"); + if par.conversion_type == ConversionType::Borrow { + type_name = String::from(": Borrowed>"); + } else { + type_name = String::from(": Option"); + } } else { - type_name = String::from(": GString"); + if par.conversion_type == ConversionType::Borrow { + type_name = String::from(": Borrowed"); + } else { + type_name = String::from(": GString"); + } } } else if par.transfer == library::Transfer::None && *nullable { - type_name = format!(": Option<{}>", ty_name); + if par.conversion_type == ConversionType::Borrow { + type_name = format!(": Borrowed>", ty_name); + } else { + type_name = format!(": Option<{}>", ty_name); + } } else { type_name = String::from(""); } diff --git a/src/codegen/trampoline_from_glib.rs b/src/codegen/trampoline_from_glib.rs index 7840058dc..e75efc0bf 100644 --- a/src/codegen/trampoline_from_glib.rs +++ b/src/codegen/trampoline_from_glib.rs @@ -28,16 +28,21 @@ impl TrampolineFromGlib for Transformation { left = format!("{}::{}", type_name, left); } } - if need_downcast { - right = format!("{}.unsafe_cast()", right); - } - if !nullable || !is_borrow { + if !nullable { left = format!("&{}", left); + } else if nullable && is_borrow { + right = format!("{}.as_ref().as_ref()", right); } else { right = format!("{}.as_ref()", right); } + if need_downcast && is_borrow { + right = format!("{}.unsafe_cast_ref()", right); + } else if need_downcast { + right = format!("{}.unsafe_cast()", right); + } + format!("{}{}{}", left, self.name, right) } Unknown => format!("/*Unknown conversion*/{}", self.name),