Skip to content

Commit

Permalink
Fixing code generation for Result<bool> in Kotlin (#692)
Browse files Browse the repository at this point in the history
Co-authored-by: Ellen Arteca <emarteca@google.com>
  • Loading branch information
emarteca and Ellen Arteca committed Sep 17, 2024
1 parent 599eebe commit c3bf8e3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion tool/src/kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,12 @@ return string{return_type_modifier}"#
match o {
Type::Primitive(prim) => {
let maybe_unsized_modifier = self.formatter.fmt_unsized_conversion(*prim, false);
format!("return {val_name}{return_type_modifier}{maybe_unsized_modifier}")
match prim {
PrimitiveType::Bool => {
format!("return ({val_name}{maybe_unsized_modifier}){return_type_modifier}")
}
_ => format!("return {val_name}{return_type_modifier}{maybe_unsized_modifier}"),
}
}
Type::Opaque(opaque_path) => self.gen_opaque_return_conversion(
opaque_path,
Expand Down Expand Up @@ -2022,6 +2027,10 @@ mod test {
pub fn get_u_byte_slice<'a>() -> &'a [u8] {
todo!()
}

pub fn boolean_result() -> Result<bool, ()> {
todo!()
}
}
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: tool/src/kotlin/mod.rs
assertion_line: 1828
assertion_line: 2057
expression: struct_code
---
package dev.gigapixel.somelib
Expand All @@ -15,6 +15,7 @@ internal interface MyNativeStructLib: Library {
fun MyNativeStruct_new(): MyNativeStructNative
fun MyNativeStruct_test_multi_arg_callback(f: DiplomatCallback_MyNativeStruct_test_multi_arg_callback_diplomatCallback_f_Native, x: Int): Int
fun MyNativeStruct_get_u_byte_slice(): Slice
fun MyNativeStruct_boolean_result(): ResultByteUnit
}

internal class MyNativeStructNative: Structure(), Structure.ByValue {
Expand Down Expand Up @@ -147,6 +148,16 @@ class MyNativeStruct internal constructor (
val returnVal = lib.MyNativeStruct_get_u_byte_slice();
return PrimitiveArrayTools.getUByteArray(returnVal)
}

fun booleanResult(): Res<Boolean, Unit> {

val returnVal = lib.MyNativeStruct_boolean_result();
if (returnVal.isOk == 1.toByte()) {
return (returnVal.union.ok > 0).ok()
} else {
return Err(Unit)
}
}
}

}

0 comments on commit c3bf8e3

Please sign in to comment.