Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: fix #36 by type-casting errors
Browse files Browse the repository at this point in the history
Produces code like this:
```
func (foreignCallbackCallbackInterfaceCallback) InvokeDoSomething(callback Callback, args []byte, outBuf *C.RustBuffer) uniffiCallbackResult {
	reader := bytes.NewReader(args)
	callback.DoSomething(FfiConverterTypeBoobyTrapErrorINSTANCE.Read(reader).(*BoobyTrapError))

	return uniffiCallbackResultSuccess
}
func (foreignCallbackCallbackInterfaceCallback) InvokeDoSomethingElse(callback Callback, args []byte, outBuf *C.RustBuffer) uniffiCallbackResult {
	reader := bytes.NewReader(args)
	callback.DoSomethingElse(FfiConverterTypeSomeEnumINSTANCE.Read(reader))

	return uniffiCallbackResultSuccess
}
```
Note the type-cast on `Read(reader)` for errors.

Signed-off-by: Kegan Dougal <[email protected]>
kegsay committed Mar 15, 2024
1 parent 2d69843 commit fda6ef5
Showing 5 changed files with 33 additions and 4 deletions.
13 changes: 11 additions & 2 deletions bindgen/src/gen_go/mod.rs
Original file line number Diff line number Diff line change
@@ -448,9 +448,18 @@ pub mod filters {
// we want to pass around the `error` interface and let the caller type cast, but in
// some cases (e.g when writing nested errors) we need to work with concrete error types
// which involve type casting from `error` to `ConcreteError`.
pub fn error_type_cast(type_: &impl AsType) -> Result<String, askama::Error> {
pub fn error_type_cast(
type_: &impl AsType,
is_name_used_as_error: bool,
) -> Result<String, askama::Error> {
let result = match type_.as_type() {
Type::Enum { .. } => format!(".(*{})", oracle().find(type_).type_label()),
Type::Enum { name: n, .. } => {
if is_name_used_as_error {
format!(".(*{})", oracle().find(type_).type_label())
} else {
String::from("")
}
}
_ => String::from(""),
};
Ok(result)
2 changes: 1 addition & 1 deletion bindgen/templates/CallbackInterfaceTemplate.go
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ func ({{ foreign_callback }}) {{ method_name }} (callback {{ type_name }}, args
{%- endmatch -%}
callback.{{ meth.name()|fn_name }}(
{%- for arg in meth.arguments() -%}
{{ arg|read_fn }}(reader)
{{ arg|read_fn }}(reader){{arg|error_type_cast(ci.is_name_used_as_error(arg|type_name))}}
{%- if !loop.last %}, {% endif -%}
{%- endfor -%}
);
2 changes: 1 addition & 1 deletion bindgen/templates/ErrorTemplate.go
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ func (c {{ e|ffi_converter_name }}) Read(reader io.Reader) error {
case {{ loop.index }}:
return &{{ type_name|class_name }}{&{{ type_name|class_name }}{{ variant.name()|class_name }}{
{%- for field in variant.fields() %}
{{ field.name()|error_field_name }}: {{ field|read_fn }}(reader){{field|error_type_cast}},
{{ field.name()|error_field_name }}: {{ field|read_fn }}(reader){{field|error_type_cast(true)}},
{%- endfor %}
}}
{%- endfor %}
10 changes: 10 additions & 0 deletions fixtures/errors/src/errors.udl
Original file line number Diff line number Diff line change
@@ -46,11 +46,21 @@ interface ComplexError {
Option(i32? id_a, i32? id_b);
};

enum SomeEnum {
"One",
"Two",
};

dictionary Vec2 {
double x;
double y;
};

callback interface Callback {
void do_something(BoobyTrapError error);
void do_something_else(SomeEnum e);
};

namespace errors {

[Throws=BoobyTrapError]
10 changes: 10 additions & 0 deletions fixtures/errors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -45,6 +45,11 @@ pub enum ComplexError {
},
}

pub enum SomeEnum {
One,
Two,
}

#[derive(Debug, thiserror::Error)]
pub enum NestedError {
#[error(transparent)]
@@ -182,4 +187,9 @@ fn error_timestamp() -> Result<std::time::SystemTime, BoobyTrapError> {
Err(BoobyTrapError::IceSlip)
}

pub trait Callback {
fn do_something(&self, error: BoobyTrapError);
fn do_something_else(&self, a: SomeEnum);
}

include!(concat!(env!("OUT_DIR"), "/errors.uniffi.rs"));

0 comments on commit fda6ef5

Please sign in to comment.