Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export more objects to allow custom thread pool without using flutter_rust_bridge::for_generated namespace #2228

Merged
merged 20 commits into from
Aug 7, 2024
Merged
2 changes: 1 addition & 1 deletion frb_example/pure_dart/rust/src/auxiliary/custom_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#![allow(unused_variables)]
#![allow(dead_code)]

use flutter_rust_bridge::for_generated::*;
use flutter_rust_bridge::handler::*;
use flutter_rust_bridge::*;
use std::future::Future;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#![allow(unused_variables)]
#![allow(dead_code)]

use flutter_rust_bridge::for_generated::*;
use flutter_rust_bridge::handler::*;
use flutter_rust_bridge::*;
use std::future::Future;

Expand Down
9 changes: 9 additions & 0 deletions frb_rust/src/handler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ pub(crate) mod executor;
#[allow(clippy::module_inception)]
pub(crate) mod handler;
pub(crate) mod implementation;

pub use error::Error as HandlerError;
pub use error_listener::ErrorListener;
pub use executor::Executor;
pub use handler::{FfiCallMode, TaskInfo};
pub use handler::{TaskContext, TaskRetFutTrait};
pub use implementation::error_listener::NoOpErrorListener;
pub use implementation::executor::SimpleExecutor;
pub use implementation::handler::SimpleHandler;
10 changes: 8 additions & 2 deletions frb_rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Main documentation is in <https://github.com/fzyzcjy/flutter_rust_bridge>

mod generalized_isolate;
pub(crate) mod handler;
pub mod handler;
mod misc;
mod platform_types;
mod rust2dart;
Expand All @@ -28,7 +28,9 @@ pub(crate) mod rust_opaque;
pub(crate) mod stream;
pub(crate) mod web_transfer;

pub use crate::codec::sse::Dart2RustMessageSse;
pub use crate::codec::sse::SseCodec;
pub use crate::codec::{BaseCodec, Rust2DartMessageTrait};
#[cfg(all(feature = "rust-async", feature = "dart-opaque"))]
pub use crate::dart_fn::DartFnFuture;
#[cfg(feature = "dart-opaque")]
Expand All @@ -38,15 +40,19 @@ pub use crate::handler::handler::Handler;
pub use crate::handler::implementation::handler::DefaultHandler;
pub use crate::misc::dart_dynamic::DartDynamic;
pub use crate::misc::into_into_dart::IntoIntoDart;
pub use crate::misc::panic_backtrace::{CatchUnwindWithBacktrace, PanicBacktrace};
#[cfg(feature = "user-utils")]
pub use crate::misc::user_utils::setup_default_user_utils;
pub use crate::platform_types::DartAbi;
pub use crate::rust2dart::sender::Rust2DartSendError;
#[cfg(all(feature = "rust-async", feature = "thread-pool"))]
pub use crate::rust_async::spawn_blocking_with;
#[cfg(feature = "rust-async")]
pub use crate::rust_async::{spawn, spawn_local, BaseAsyncRuntime, JoinHandle};
pub use crate::rust_async::{spawn, spawn_local, BaseAsyncRuntime, JoinHandle, SimpleAsyncRuntime};
#[cfg(feature = "rust-async")]
pub use crate::rust_auto_opaque::RustAutoOpaqueNom;
#[allow(deprecated)]
pub use crate::rust_opaque::{DartSafe, RustOpaqueNom};
#[cfg(feature = "thread-pool")]
pub use crate::thread_pool::{BaseThreadPool, SimpleThreadPool};
pub use flutter_rust_bridge_macros::frb;
15 changes: 10 additions & 5 deletions frb_rust/src/misc/panic_backtrace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ thread_local! {
static BACKTRACE: RefCell<Option<Backtrace>> = const { RefCell::new(None) };
}

pub(crate) struct PanicBacktrace;
/// Utility for tracking panic backtrace.
///
/// This is originally used internally, and only exposed because it is needed outside flutter_rust_bridge.
/// Therefore, the API may not follow semantics versioning.
pub struct PanicBacktrace;

impl PanicBacktrace {
pub(crate) fn setup() {
pub fn setup() {
let old_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |arg| {
let trace = Backtrace::capture();
Expand All @@ -20,7 +24,7 @@ impl PanicBacktrace {
}));
}

pub(crate) fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(
pub fn catch_unwind<F: FnOnce() -> R + UnwindSafe, R>(
f: F,
) -> Result<R, CatchUnwindWithBacktrace> {
std::panic::catch_unwind(f).map_err(|err| CatchUnwindWithBacktrace {
Expand All @@ -29,12 +33,13 @@ impl PanicBacktrace {
})
}

pub(crate) fn take_last() -> Option<Backtrace> {
pub fn take_last() -> Option<Backtrace> {
BACKTRACE.with(|b| b.borrow_mut().take())
}
}

pub(crate) struct CatchUnwindWithBacktrace {
/// Similar to the output of catch_unwind, but with extra backtrace
pub struct CatchUnwindWithBacktrace {
pub err: Box<dyn Any + Send + 'static>,
pub backtrace: Option<Backtrace>,
}
Expand Down
5 changes: 5 additions & 0 deletions website/docs/guides/custom/rust/handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ If your instance is detected, the generator will not generate one, but will use

As for how to write a custom handler, often copy-paste-modify the code
in `flutter_rust_bridge::frb_generated_default_handler!()` is a good idea.
You may need to import types from `flutter_rust_bridge::` as well as `flutter_rust_bridge::handler::`.

The handler is the central entrypoint to handle calls between Rust and Dart,
therefore please visit the API of the `Handler` trait for more details.
Expand Down Expand Up @@ -37,3 +38,7 @@ and it causes confusion and bugs.
Similar things may happen if you are using multiple isolates in your Dart program,
or when you hot-restart the Dart side.

## Remarks

The test is
in https://github.com/fzyzcjy/flutter_rust_bridge/blob/master/frb_example/pure_dart/rust/src/api/custom_handler.rs.
Loading