Skip to content

Commit

Permalink
Merge pull request gtk-rs#602 from EPashkin/async_exctraction
Browse files Browse the repository at this point in the history
Async exctraction
  • Loading branch information
EPashkin authored May 16, 2018
2 parents 47ba509 + 305c6a9 commit 1825132
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 59 deletions.
50 changes: 33 additions & 17 deletions src/analysis/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use std::collections::vec_deque::VecDeque;
use std::slice::Iter;
use std::vec::Vec;

use consts::TYPE_PARAMETERS_START;
use env::Env;
use analysis::imports::Imports;
use analysis::function_parameters::{async_param_to_remove, CParameter};
use analysis::functions::{find_function, find_index_to_ignore, finish_function_name};
use analysis::function_parameters::{CParameter, async_param_to_remove};
use analysis::imports::Imports;
use analysis::out_parameters::use_function_return_for_result;
use analysis::rust_type::{bounds_rust_type, rust_type};
use consts::TYPE_PARAMETERS_START;
use env::Env;
use library::{Function, Fundamental, Nullable, ParameterDirection, Type, TypeId};
use traits::IntoString;

Expand Down Expand Up @@ -97,22 +97,29 @@ impl Bound {
}
}

#[derive(Debug)]
pub struct CallbackInfo {
pub callback_type: String,
pub success_parameters: String,
pub error_parameters: String,
pub bound_name: char,
}

impl Bounds {
pub fn add_for_parameter(
&mut self,
env: &Env,
func: &Function,
par: &CParameter,
async: bool,
) -> (Option<String>, Option<(String, String, String, char)>) {
) -> (Option<String>, Option<CallbackInfo>) {
let type_name = bounds_rust_type(env, par.typ);
let mut type_string =
if async && async_param_to_remove(&par.name) {
return (None, None);
} else {
type_name.into_string()
};
let mut trampoline_info = None;
let mut type_string = if async && async_param_to_remove(&par.name) {
return (None, None);
} else {
type_name.into_string()
};
let mut callback_info = None;
let mut ret = None;
if !par.instance_parameter && par.direction != ParameterDirection::Out {
if let Some(bound_type) = Bounds::type_for(env, par.typ, par.nullable) {
Expand All @@ -123,13 +130,22 @@ impl Bounds {
if let Some(function) = find_function(env, &finish_func_name) {
let mut out_parameters = find_out_parameters(env, function);
if use_function_return_for_result(env, function.ret.typ) {
out_parameters.insert(0, rust_type(env, function.ret.typ).into_string());
out_parameters
.insert(0, rust_type(env, function.ret.typ).into_string());
}
let parameters = format_out_parameters(&out_parameters);
let error_type = find_error_type(env, function);
type_string = format!("FnOnce(Result<{}, {}>) + Send + 'static", parameters, error_type);
let bounds_name = *self.unused.front().unwrap();
trampoline_info = Some((type_string.clone(), parameters, error_type, bounds_name));
type_string = format!(
"FnOnce(Result<{}, {}>) + Send + 'static",
parameters, error_type
);
let bound_name = *self.unused.front().unwrap();
callback_info = Some(CallbackInfo {
callback_type: type_string.clone(),
success_parameters: parameters,
error_parameters: error_type,
bound_name,
});
}
}
if !self.add_parameter(&par.name, &type_string, bound_type, async) {
Expand All @@ -140,7 +156,7 @@ impl Bounds {
}
}
}
(ret, trampoline_info)
(ret, callback_info)
}

pub fn type_for(env: &Env, type_id: TypeId, nullable: Nullable) -> Option<BoundType> {
Expand Down
108 changes: 66 additions & 42 deletions src/analysis/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
use std::collections::HashMap;
use std::vec::Vec;

use analysis::bounds::Bounds;
use analysis::bounds::{Bounds, CallbackInfo};
use analysis::function_parameters::{self, Parameters, Transformation, TransformationType};
use analysis::out_parameters::use_function_return_for_result;
use analysis::imports::Imports;
use analysis::out_parameters;
use analysis::out_parameters::use_function_return_for_result;
use analysis::ref_mode::RefMode;
use analysis::return_value;
use analysis::rust_type::*;
Expand All @@ -22,9 +22,9 @@ use config;
use env::Env;
use library::{self, Function, FunctionKind, Nullable, Parameter, ParameterScope, Type};
use nameutil;
use std::borrow::Borrow;
use traits::*;
use version::Version;
use std::borrow::Borrow;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Visibility {
Expand Down Expand Up @@ -197,48 +197,18 @@ fn analyze_function(
if let Ok(s) = used_rust_type(env, par.typ) {
used_types.push(s);
}
let (to_glib_extra, type_string) = bounds.add_for_parameter(env, func, par, async);
let (to_glib_extra, callback_info) = bounds.add_for_parameter(env, func, par, async);
if let Some(to_glib_extra) = to_glib_extra {
to_glib_extras.insert(pos, to_glib_extra);
}
if let Some((callback_type, success_parameters, error_parameters, bound_name)) = type_string {
// Checks for /*Ignored*/ or other error comments
if callback_type.find("/*").is_some() {
commented = true;
}
let func_name = func.c_identifier.as_ref().unwrap();
let finish_func_name = finish_function_name(func_name);
let mut output_params = vec![];
let mut ffi_ret = None;
if let Some(function) = find_function(env, &finish_func_name) {
if use_function_return_for_result(env, function.ret.typ) {
ffi_ret = Some(function.ret.clone());
}

output_params.extend(function.parameters.clone());
for param in &mut output_params {
if nameutil::needs_mangling(&param.name) {
param.name = nameutil::mangle_keywords(&*param.name).into_owned();
}
}
}
trampoline = Some(AsyncTrampoline {
is_method: func.kind == FunctionKind::Method,
name: format!("{}_trampoline", func.name),
finish_func_name,
callback_type,
bound_name,
output_params,
ffi_ret,
});

async_future = Some(AsyncFuture {
is_method: func.kind == FunctionKind::Method,
name: format!("{}_future", func.name),
success_parameters,
error_parameters,
});
}
analyze_async(
env,
func,
callback_info,
&mut commented,
&mut trampoline,
&mut async_future,
);
let type_error =
!(async && *env.library.type_(par.typ) == Type::Fundamental(library::Fundamental::Pointer)) &&
parameter_rust_type(env, par.typ, par.direction, Nullable(false), RefMode::None)
Expand Down Expand Up @@ -376,6 +346,60 @@ pub fn is_carray_with_direct_elements(env: &Env, typ: library::TypeId) -> bool {
}
}

fn analyze_async(
env: &Env,
func: &library::Function,
callback_info: Option<CallbackInfo>,
commented: &mut bool,
trampoline: &mut Option<AsyncTrampoline>,
async_future: &mut Option<AsyncFuture>,
) {
if let Some(CallbackInfo {
callback_type,
success_parameters,
error_parameters,
bound_name,
}) = callback_info
{
// Checks for /*Ignored*/ or other error comments
if callback_type.find("/*").is_some() {
*commented = true;
}
let func_name = func.c_identifier.as_ref().unwrap();
let finish_func_name = finish_function_name(func_name);
let mut output_params = vec![];
let mut ffi_ret = None;
if let Some(function) = find_function(env, &finish_func_name) {
if use_function_return_for_result(env, function.ret.typ) {
ffi_ret = Some(function.ret.clone());
}

output_params.extend(function.parameters.clone());
for param in &mut output_params {
if nameutil::needs_mangling(&param.name) {
param.name = nameutil::mangle_keywords(&*param.name).into_owned();
}
}
}
*trampoline = Some(AsyncTrampoline {
is_method: func.kind == FunctionKind::Method,
name: format!("{}_trampoline", func.name),
finish_func_name,
callback_type,
bound_name,
output_params,
ffi_ret,
});

*async_future = Some(AsyncFuture {
is_method: func.kind == FunctionKind::Method,
name: format!("{}_future", func.name),
success_parameters,
error_parameters,
});
}
}

pub fn find_function<'a>(env: &'a Env, c_identifier: &str) -> Option<&'a Function> {
let find = |functions: &'a [Function]| -> Option<&'a Function> {
for function in functions {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![cfg_attr(feature = "cargo-clippy", allow(match_same_arms))]
#![cfg_attr(feature = "cargo-clippy", allow(let_and_return))]
#![cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
#![cfg_attr(feature = "cargo-clippy", allow(write_literal))]

extern crate git2;
#[macro_use]
Expand Down

0 comments on commit 1825132

Please sign in to comment.