Skip to content

Commit

Permalink
refactoring/c++: simplify c_generate_args_with_types
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushistov committed Jun 22, 2019
1 parent 761058d commit 379e1ab
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
15 changes: 9 additions & 6 deletions macroslib/src/cpp/cpp_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use smol_str::SmolStr;
use syn::spanned::Spanned;

use crate::{
cpp::{fmt_write_err_map, map_any_err_to_our_err, CppForeignMethodSignature, MergeCTypesFlags},
cpp::{
fmt_write_err_map, map_any_err_to_our_err, CppForeignMethodSignature, MergeCTypesFlags,
WRITE_TO_MEM_FAILED_MSG,
},
error::{panic_on_syn_error, DiagnosticError},
file_cache::FileWriteCache,
source_registry::SourceId,
Expand Down Expand Up @@ -35,18 +38,18 @@ pub(in crate::cpp) fn doc_comments_to_c_comments(
pub(in crate::cpp) fn c_generate_args_with_types(
f_method: &CppForeignMethodSignature,
append_comma_if_not_empty: bool,
) -> Result<String, String> {
) -> String {
let mut buf = String::new();
for (i, f_type_info) in f_method.input.iter().enumerate() {
if i > 0 {
write!(&mut buf, ", ").map_err(fmt_write_err_map)?;
buf.push_str(", ");
}
write!(&mut buf, "{} a_{}", f_type_info.as_ref().name, i).map_err(fmt_write_err_map)?;
write!(&mut buf, "{} a_{}", f_type_info.as_ref().name, i).expect(WRITE_TO_MEM_FAILED_MSG);
}
if !buf.is_empty() && append_comma_if_not_empty {
write!(&mut buf, ", ").map_err(fmt_write_err_map)?;
buf.push_str(", ");
}
Ok(buf)
buf
}

pub(in crate::cpp) fn c_class_type(class: &ForeignerClassInfo) -> String {
Expand Down
6 changes: 2 additions & 4 deletions macroslib/src/cpp/fclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
cpp::{
c_func_name, cpp_code, map_type::map_type, n_arguments_list, rust_generate_args_with_types,
CppContext, CppForeignMethodSignature, CppForeignTypeInfo, MethodContext,
WRITE_TO_MEM_FAILED_MSG,
},
error::{panic_on_syn_error, DiagnosticError, Result},
file_cache::FileWriteCache,
Expand All @@ -26,8 +27,6 @@ use crate::{
types::{ForeignerClassInfo, MethodAccess, MethodVariant, SelfTypeVariant},
};

static WRITE_TO_MEM_FAILED_MSG: &str = "Write to memory buffer failed, no free mem?";

pub(in crate::cpp) fn generate(
ctx: &mut CppContext,
class: &ForeignerClassInfo,
Expand Down Expand Up @@ -166,8 +165,7 @@ May be you need to use `private constructor = empty;` syntax?",
.write_all(cpp_code::doc_comments_to_c_comments(&method.doc_comments, false).as_bytes())
.expect(WRITE_TO_MEM_FAILED_MSG);
let c_func_name = c_func_name(class, method);
let c_args_with_types = cpp_code::c_generate_args_with_types(f_method, false)
.map_err(|err| DiagnosticError::new(class.src_id, class.span(), err))?;
let c_args_with_types = cpp_code::c_generate_args_with_types(f_method, false);
let comma_c_args_with_types = if c_args_with_types.is_empty() {
String::new()
} else {
Expand Down
6 changes: 3 additions & 3 deletions macroslib/src/cpp/finterface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ struct C_{interface_name} {{
"#,
method_name = method.name,
doc_comments = cpp_code::doc_comments_to_c_comments(&method.doc_comments, false),
single_args_with_types = cpp_code::c_generate_args_with_types(f_method, true)?,
single_args_with_types = cpp_code::c_generate_args_with_types(f_method, true),
c_ret_type = c_ret_type,
)
.map_err(&map_write_err)?;
Expand Down Expand Up @@ -374,7 +374,7 @@ struct C_{interface_name} {{
}}
"#,
method_name = method.name,
single_args_with_types = cpp_code::c_generate_args_with_types(f_method, true)?,
single_args_with_types = cpp_code::c_generate_args_with_types(f_method, true),
input_args = cpp_code::cpp_generate_args_to_call_c(f_method)?,
interface_name = interface.name,
)
Expand All @@ -392,7 +392,7 @@ struct C_{interface_name} {{
}}
"#,
method_name = method.name,
single_args_with_types = cpp_code::c_generate_args_with_types(f_method, true)?,
single_args_with_types = cpp_code::c_generate_args_with_types(f_method, true),
input_args = cpp_code::cpp_generate_args_to_call_c(f_method)?,
interface_name = interface.name,
c_ret_type = c_ret_type,
Expand Down
2 changes: 2 additions & 0 deletions macroslib/src/cpp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ use crate::{
CppConfig, CppOptional, CppStrView, CppVariant, LanguageGenerator, SourceCode, TypeMap,
};

static WRITE_TO_MEM_FAILED_MSG: &str = "Write to memory buffer failed, no free mem?";

#[derive(Debug)]
struct CppConverter {
typename: SmolStr,
Expand Down

0 comments on commit 379e1ab

Please sign in to comment.