Skip to content

Commit

Permalink
Kotlin result (#728)
Browse files Browse the repository at this point in the history
* kotlin - uses results

* kotlin-result - gen example

* kotlin-result - fix example

* kotlin-result - clippy and tests

* kotlin-result - remove Res
  • Loading branch information
jcrist1 authored Nov 10, 2024
1 parent 6ccac64 commit 0650c74
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@ import org.openjdk.jmh.infra.Blackhole;

@State(Scope.Benchmark)
internal open class ICU4XFixedDecimalFormatterBench {
private val locale = ICU4XLocale.new_("en")
private val provider = ICU4XDataProvider.newStatic()
private val options = ICU4XFixedDecimalFormatterOptions.default_()
private val formatter = ICU4XFixedDecimalFormatter.tryNew(locale, provider, options).wrapErrAndThrow()
private val decimal = ICU4XFixedDecimal.new_(123)
private val locale = Locale.new_("en")
private val provider = DataProvider.newStatic()
private val options = FixedDecimalFormatterOptions.default_()
private val formatter = FixedDecimalFormatter.tryNew(locale, provider, options).getOrThrow()
private val decimal = FixedDecimal.new_(123)

@Benchmark
fun benchLocale(bh: Blackhole) {
bh.consume(ICU4XLocale.new_("en"))
bh.consume(Locale.new_("en"))
}

@Benchmark
fun benchProvider(bh: Blackhole) {
bh.consume(ICU4XDataProvider.newStatic())
bh.consume(DataProvider.newStatic())
}

@Benchmark
fun benchOptions(bh: Blackhole) {
bh.consume(ICU4XFixedDecimalFormatterOptions.default_())
bh.consume(FixedDecimalFormatterOptions.default_())
}

@Benchmark
fun benchDecimal(bh: Blackhole) {
bh.consume(ICU4XFixedDecimal.new_(123))
bh.consume(FixedDecimal.new_(123))
}

@Benchmark
fun benchFormatter(bh: Blackhole) {
bh.consume(ICU4XFixedDecimalFormatter.tryNew(locale, provider, options).wrapErrAndThrow())
bh.consume(FixedDecimalFormatter.tryNew(locale, provider, options).getOrThrow())
}

@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ class DataProvider internal constructor (

/** This exists as a regression test for https://github.com/rust-diplomat/diplomat/issues/155
*/
fun returnsResult(): Res<Unit, Unit> {
fun returnsResult(): Result<Unit> {

val returnVal = lib.icu4x_DataProvider_returns_result_mv1();
if (returnVal.isOk == 1.toByte()) {
return Unit.ok()
} else {
return Err(Unit)
return Unit.err()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ class FixedDecimal internal constructor (
*
*See the [Rust documentation for `write_to`](https://docs.rs/fixed_decimal/latest/fixed_decimal/struct.FixedDecimal.html#method.write_to) for more information.
*/
fun toString_(): Res<String, Unit> {
fun toString_(): Result<String> {
val write = DW.lib.diplomat_buffer_write_create(0)
val returnVal = lib.icu4x_FixedDecimal_to_string_mv1(handle, write);
if (returnVal.isOk == 1.toByte()) {

val returnString = DW.writeToString(write)
return returnString.ok()
} else {
return Err(Unit)
return Unit.err()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class FixedDecimalFormatter internal constructor (
*
*See the [Rust documentation for `try_new`](https://docs.rs/icu/latest/icu/decimal/struct.FixedDecimalFormatter.html#method.try_new) for more information.
*/
fun tryNew(locale: Locale, provider: DataProvider, options: FixedDecimalFormatterOptions): Res<FixedDecimalFormatter, Unit> {
fun tryNew(locale: Locale, provider: DataProvider, options: FixedDecimalFormatterOptions): Result<FixedDecimalFormatter> {

val returnVal = lib.icu4x_FixedDecimalFormatter_try_new_mv1(locale.handle, provider.handle, options.nativeStruct);
if (returnVal.isOk == 1.toByte()) {
Expand All @@ -46,7 +46,7 @@ class FixedDecimalFormatter internal constructor (
CLEANER.register(returnOpaque, FixedDecimalFormatter.FixedDecimalFormatterCleaner(handle, FixedDecimalFormatter.lib));
return returnOpaque.ok()
} else {
return Err(Unit)
return Unit.err()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,31 +320,13 @@ class Slice: Structure(), Structure.ByValue {
}
}

sealed interface Res<T, E>
class Ok<T, E>(val inner: T) : Res<T, E>
class Err<T, E>(val inner: E) : Res<T, E>


fun <T> Res<T, Throwable>.reThrow(): T {
return when (this) {
is Ok -> this.inner
is Err -> throw this.inner
}
}

fun <T, E> Res<T, E>.wrapErrAndThrow(): T {
return when (this) {
is Ok -> this.inner
is Err -> throw RuntimeException("Received error ${this.inner}")
}
}

fun <T, E> T.ok(): Res<T, E> {
return Ok(this)
internal fun <T> T.ok(): Result<T> {
return Result.success(this)
}

fun <T, E> E.err(): Res<T, E> {
return Err(this)
internal fun <T, E> E.err(): Result<T> {
return Result.failure(RuntimeException("Received error $this"))
}

internal class ResultPointerUnitUnion: Union() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ICU4XFixedDecimalFormatterTest {
val locale = Locale.new_("en")
val provider = DataProvider.newStatic()
val options = FixedDecimalFormatterOptions.default_()
val formatter = FixedDecimalFormatter.tryNew(locale, provider, options).wrapErrAndThrow()
val formatter = FixedDecimalFormatter.tryNew(locale, provider, options).getOrThrow()
val decimal: FixedDecimal = FixedDecimal.new_(123)
val formatted = formatter.formatWrite(decimal)
assertEquals(formatted, "123")
Expand Down

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

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

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

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

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

10 changes: 3 additions & 7 deletions tool/src/kotlin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,9 @@ impl<'a, 'cx> TyGenContext<'a, 'cx> {
fn gen_return_type_name(&self, result_ty: &ReturnType) -> Cow<'cx, str> {
match *result_ty {
ReturnType::Infallible(ref success) => self.gen_infallible_return_type_name(success),
ReturnType::Fallible(ref ok, ref err) => {
ReturnType::Fallible(ref ok, _) => {
let ok_type = self.gen_infallible_return_type_name(ok);
let err_type = err
.as_ref()
.map(|err| self.gen_type_name(err, None))
.unwrap_or_else(|| "Unit".into());
format!("Res<{ok_type}, {err_type}>").into()
format!("Result<{ok_type}>").into()
}
ReturnType::Nullable(ref success) => self
.formatter
Expand Down Expand Up @@ -864,7 +860,7 @@ val intermediateOption = {val_name}.option() ?: return null
use_finalizers_not_cleaners,
)
})
.unwrap_or_else(|| "return Err(Unit)".into());
.unwrap_or_else(|| "return Unit.err()".into());

#[derive(Template)]
#[template(path = "kotlin/ResultReturn.kt.jinja", escape = "none")]
Expand Down
Loading

0 comments on commit 0650c74

Please sign in to comment.