Skip to content

Commit

Permalink
codegen: Use async keyword for not in traits functions
Browse files Browse the repository at this point in the history
Keep the Pin<Box<T>> only for in trait async funcitons.
Reduces the generated code a little bit :)
  • Loading branch information
bilelmoussaoui committed Jul 9, 2023
1 parent c88b692 commit e469948
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/analysis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
57 changes: 41 additions & 16 deletions src/codegen/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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)?;
Expand Down Expand Up @@ -224,19 +224,34 @@ 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<Box_<dyn std::future::Future<Output = Result<{}, {}>> + 'static>>",
async_future.success_parameters, error_parameters
)
if in_trait {
format!(
" -> Pin<Box_<dyn std::future::Future<Output = Result<{}, {}>> + 'static>>",
async_future.success_parameters, error_parameters
)
} else {
format!(
" -> Result<{}, {}>",
async_future.success_parameters, error_parameters
)
}
} else {
format!(
" -> Pin<Box_<dyn std::future::Future<Output = {}> + 'static>>",
async_future.success_parameters
)
if in_trait {
format!(
" -> Pin<Box_<dyn std::future::Future<Output = {}> + 'static>>",
async_future.success_parameters
)
} else {
format!(" -> {}", async_future.success_parameters)
}
};

let mut param_str = String::with_capacity(100);
Expand Down Expand Up @@ -267,10 +282,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,
)
}

Expand Down Expand Up @@ -387,6 +403,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<String, fmt::Error> {
use std::fmt::Write;

Expand Down Expand Up @@ -436,15 +453,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| {{"
)?;
}

Expand Down Expand Up @@ -483,7 +504,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)
}

0 comments on commit e469948

Please sign in to comment.