diff --git a/src/analysis/functions.rs b/src/analysis/functions.rs index 235e090d8..8b079192a 100644 --- a/src/analysis/functions.rs +++ b/src/analysis/functions.rs @@ -868,8 +868,9 @@ fn analyze_function( if r#async && status.need_generate() && !commented { imports.add("std::ptr"); imports.add("std::boxed::Box as Box_"); - imports.add("std::pin::Pin"); - + if in_trait { + imports.add("std::pin::Pin"); + } if let Some(ref trampoline) = trampoline { for out in &trampoline.output_params { if let Ok(rust_type) = RustType::builder(env, out.lib_par.typ) diff --git a/src/codegen/function.rs b/src/codegen/function.rs index 03ee417b7..6b8031dc9 100644 --- a/src/codegen/function.rs +++ b/src/codegen/function.rs @@ -145,7 +145,7 @@ pub fn generate( } if analysis.async_future.is_some() { - let declaration = declaration_futures(env, analysis); + let declaration = declaration_futures(env, analysis, in_trait); let suffix = if only_declaration { ";" } else { " {" }; writeln!(w)?; @@ -168,7 +168,7 @@ pub fn generate( )?; if !only_declaration { - let body = body_chunk_futures(env, analysis).unwrap(); + let body = body_chunk_futures(env, analysis, in_trait).unwrap(); for s in body.lines() { if !s.is_empty() { writeln!(w, "{}{}{}", tabs(indent + 1), comment_prefix, s)?; @@ -224,19 +224,32 @@ pub fn declaration(env: &Env, analysis: &analysis::functions::Info) -> String { ) } -pub fn declaration_futures(env: &Env, analysis: &analysis::functions::Info) -> String { +pub fn declaration_futures( + env: &Env, + analysis: &analysis::functions::Info, + in_trait: bool, +) -> String { let async_future = analysis.async_future.as_ref().unwrap(); let return_str = if let Some(ref error_parameters) = async_future.error_parameters { - format!( - " -> Pin> + 'static>>", - async_future.success_parameters, error_parameters - ) - } else { + if in_trait { + format!( + " -> Pin> + 'static>>", + async_future.success_parameters, error_parameters + ) + } else { + format!( + " -> Result<{}, {}>", + async_future.success_parameters, error_parameters + ) + } + } else if in_trait { format!( " -> Pin + 'static>>", async_future.success_parameters ) + } else { + format!(" -> {}", async_future.success_parameters) }; let mut param_str = String::with_capacity(100); @@ -267,10 +280,11 @@ pub fn declaration_futures(env: &Env, analysis: &analysis::functions::Info) -> S } let (bounds, _) = bounds(&analysis.bounds, skipped_bounds.as_ref(), true, false); + let async_sig = if in_trait { "" } else { "async " }; format!( - "fn {}{}({}){}", - async_future.name, bounds, param_str, return_str, + "{}fn {}{}({}){}", + async_sig, async_future.name, bounds, param_str, return_str, ) } @@ -387,6 +401,7 @@ pub fn body_chunk(env: &Env, analysis: &analysis::functions::Info) -> Chunk { pub fn body_chunk_futures( env: &Env, analysis: &analysis::functions::Info, + in_trait: bool, ) -> StdResult { use std::fmt::Write; @@ -436,15 +451,19 @@ pub fn body_chunk_futures( } } + if in_trait { + writeln!(body, "Box_::pin(")?; + } + if async_future.is_method { writeln!( body, - "Box_::pin({gio_future_name}::new(self, move |obj, cancellable, send| {{" + "{gio_future_name}::new(self, move |obj, cancellable, send| {{" )?; } else { writeln!( body, - "Box_::pin({gio_future_name}::new(&(), move |_obj, cancellable, send| {{" + "{gio_future_name}::new(&(), move |_obj, cancellable, send| {{" )?; } @@ -483,7 +502,11 @@ pub fn body_chunk_futures( writeln!(body, "\t\t\tsend.resolve(res);")?; writeln!(body, "\t\t}},")?; writeln!(body, "\t);")?; - writeln!(body, "}}))")?; + if in_trait { + writeln!(body, "}}))")?; + } else { + writeln!(body, "}}).await")?; + } Ok(body) }