Skip to content

Conversation

@rlch
Copy link

@rlch rlch commented Dec 16, 2025

Hey @fzyzcjy

This PR adds support for serializing Result<T, E> to the equivalent exposed by oxidized. I think the package's API has stabilized, obviously idiomatic w.r.t. Rust conventions; it's well-tested and doesn't pull in a crap-ton of API's like a lot of the sibling packages.

Thanks again for this package, I hope to contribute more over the coming years; what you've done is magnificent and IMO the best addition to the Flutter ecosystem thus far.

Changes

Fixes #2683

Procedure and Checklist

In order to quickly iterate and avoid slowing down development pace by making CI pass, only the following simplified steps are needed, and I (fzyzcjy) will handle the rest of CI / moving the tests currently (will have more automation in the future).

  • Implement the feature / fix the bug.
  • Add tests in frb_example/dart_minimal.
  • Make dart_minimal's CI green.

Utility commands

  • Run codegen: cargo run --manifest-path /path/to/flutter_rust_bridge/frb_codegen/Cargo.toml -- generate
  • Run tests: ./frb_internal test-dart-native --package frb_example/dart_minimal

When oxidized package is detected in pubspec.yaml dependencies:
- Fallible Rust functions return Future<Result<T, E>> instead of Future<T>
- Errors are returned as Err(...) instead of throwing exceptions
- Panics still throw PanicException (bugs, not expected errors)

Runtime (frb_dart):
- Add decodeAsResult() to SimpleDecoder for Result-based decoding
- Add decodeObjectAsResult/decodeWireSyncTypeAsResult to BaseCodec
- Add executeNormalAsResult/executeSyncAsResult to BaseHandler
- Export Result, Ok, Err from flutter_rust_bridge_for_generated_common.dart

Codegen (frb_codegen):
- Add has_dependency() to DartRepository
- Add detect_oxidized_dependency() in generator_parser.rs
- Add use_oxidized field to GeneratorApiDartInternalConfig
- Wrap fallible function return types in Result<T, E>
- Use executeNormalAsResult/executeSyncAsResult for fallible functions

Tested with dart_minimal example:
- Async functions: Future<Result<T, E>>
- Sync functions: Result<T, E>
@codecov
Copy link

codecov bot commented Dec 16, 2025

Codecov Report

❌ Patch coverage is 0% with 253 lines in your changes missing coverage. Please review.
✅ Project coverage is 1.87%. Comparing base (67bfaec) to head (50cbd85).

Files with missing lines Patch % Lines
...gen/src/library/codegen/parser/mir/parser/ty/ty.rs 0.00% 104 Missing ⚠️
...src/library/codegen/parser/mir/parser/ty/result.rs 0.00% 73 Missing ⚠️
.../config/internal_config_parser/generator_parser.rs 0.00% 10 Missing ⚠️
...flat/transformer/resolve_type_alias_transformer.rs 0.00% 10 Missing ⚠️
...egen/generator/api_dart/spec_generator/function.rs 0.00% 9 Missing ⚠️
...tor/wire/dart/spec_generator/misc/api_impl_body.rs 0.00% 8 Missing ⚠️
...gen/src/library/codegen/ir/hir/misc/serializers.rs 0.00% 7 Missing ⚠️
...en/src/library/codegen/parser/hir/flat/exporter.rs 0.00% 7 Missing ⚠️
...degen/parser/hir/flat/parser/syn_item/item_type.rs 0.00% 7 Missing ⚠️
...gen/src/library/utils/dart_repository/dart_repo.rs 0.00% 7 Missing ⚠️
... and 6 more
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #2950       +/-   ##
==========================================
- Coverage   98.57%   1.87%   -96.71%     
==========================================
  Files         464     357      -107     
  Lines       19202   14152     -5050     
==========================================
- Hits        18928     265    -18663     
- Misses        274   13887    +13613     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

rlch added 2 commits December 16, 2025 16:49
  When a type alias like \`type Result<T> = std::result::Result<T, MyError>\`
  is used, FRB now correctly extracts the error type from the alias definition
  instead of falling back to AnyhowException.

  - Store generic type aliases separately in HirFlatTypeAlias
  - Look up generic Result aliases when parsing function return types
  - Substitute type parameters to resolve the actual error type
- Add example error types and functions using type aliases
- Update generated Dart/Rust bindings with new test cases
@fzyzcjy
Copy link
Owner

fzyzcjy commented Dec 16, 2025

You are welcome and thanks for the great job!

Copy link
Owner

@fzyzcjy fzyzcjy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nits (I will review more ASAP when having time)

@rlch
Copy link
Author

rlch commented Dec 20, 2025

Thanks @fzyzcjy ! Addressed changes.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Dec 23, 2025

Looks great! I will review ASAP, but have a tight ddl now thus may not have time very recently :(

@rlch
Copy link
Author

rlch commented Jan 25, 2026

Hey @fzyzcjy would you be able to review this when you get some time? Got like 3 projects using this fork and it's getting a little annoying lol.

No rush, I understand you're busy. Just thought I'd remind!

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 25, 2026

Hi, happy to see your work is being used by multi projects, and I also think this is a great feature! I am really sorry for being super busy w/ a very emergent and important ddl (or indeed, multiple overlapped deadlines, that's why I have been non resting for a long time) :( I will review as soon as the current ddl ends, potentially it will not be overlapped with another emergent ddl.

By the way, if you (or maybe Cursor / Claude Code etc) put the tests in pure_dart etc instead of the dart_minimal, then I do not need to manually do the moving / refactoring / checking, then I will potentially just merge it with a lightweight review, since it is does not change current code a lot, and can be refactored and further reviewed later.

@rlch
Copy link
Author

rlch commented Jan 26, 2026

No worries at all mate. Sorry to hear you're so busy, best of luck with it!

I'll handle the tests, lmk if I've done anything wrong when I get around to it.

Again no stress at all, take your time.

@fzyzcjy
Copy link
Owner

fzyzcjy commented Jan 26, 2026

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature: Rust Results in Dart code

2 participants