Skip to content

Commit

Permalink
Review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Kegan Dougal <[email protected]>
  • Loading branch information
kegsay committed Mar 14, 2024
1 parent 0479091 commit 1a0cb44
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
22 changes: 5 additions & 17 deletions bindgen/src/gen_go/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,31 +465,19 @@ pub mod filters {
}

/// FFI type name to be used to reference cgo types
pub fn ffi_type_name<T: Clone + Into<FfiType>>(type_: &T) -> Result<String, askama::Error> {
pub fn ffi_type_name(type_: &Type) -> Result<String, askama::Error> {
let ffi_type: FfiType = type_.clone().into();
let result = match ffi_type {
FfiType::RustArcPtr(_) => "unsafe.Pointer".into(),
FfiType::RustBuffer(_) => "RustBufferI".into(),
FfiType::RustBuffer(_) => match type_ {
Type::External { namespace, .. } => format!("{}.RustBufferI", namespace),
_ => "RustBufferI".into(),
},
_ => format!("C.{}", oracle().ffi_type_label(&ffi_type)),
};
Ok(result)
}

// Return the Go package name for the given type, if it has one, followed by a '.'
// Returns the empty string if the type has no package name associated with it e.g primitive types.
//
// for multi-package bindings, it may be required to specify which
// RustBufferI should be used when creating completeFunc. Failure to
// specify the right package here will result in compilation errors.
pub fn maybe_namespace(type_: &impl AsType) -> Result<String, askama::Error> {
let type_label = oracle().find(type_).type_label();
if let Some((package_name, _)) = type_label.split_once(".") {
Ok(format!("{}.", package_name))
} else {
Ok(String::from(""))
}
}

/// FFI type name to be used to reference cgo types. Such that they exactly match to the cgo bindings and can be used with `//export`.
pub fn ffi_type_name_cgo_safe<T: Clone + Into<FfiType>>(
type_: &T,
Expand Down
2 changes: 1 addition & 1 deletion bindgen/templates/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
{% macro return_type_decl_async(func) %}
{%- match func.return_type() -%}
{%- when Some with (return_type) -%}
{{return_type|maybe_namespace}}{{ return_type|ffi_type_name }}
{{ return_type|ffi_type_name }}
{%- when None -%}
{%- endmatch %}
{%- endmacro %}
Expand Down

0 comments on commit 1a0cb44

Please sign in to comment.