Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen: Use async keyword for not in traits functions #1482

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
49 changes: 36 additions & 13 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,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<Box_<dyn std::future::Future<Output = Result<{}, {}>> + 'static>>",
async_future.success_parameters, error_parameters
)
} else {
if in_trait {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gtk-rs/gtk4-rs#1421 (comment) FWIW. This will now create differently behaving API depending on whether it's in a trait or not

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 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 +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,
)
}

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

Expand Down Expand Up @@ -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| {{"
)?;
}

Expand Down Expand Up @@ -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)
}