diff --git a/frb_example/pure_dart/rust/src/auxiliary/custom_handler.rs b/frb_example/pure_dart/rust/src/auxiliary/custom_handler.rs index 5eb26be9de..5692197a44 100644 --- a/frb_example/pure_dart/rust/src/auxiliary/custom_handler.rs +++ b/frb_example/pure_dart/rust/src/auxiliary/custom_handler.rs @@ -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; diff --git a/frb_example/pure_dart_pde/rust/src/auxiliary/custom_handler.rs b/frb_example/pure_dart_pde/rust/src/auxiliary/custom_handler.rs index 8a566bab50..460866dd23 100644 --- a/frb_example/pure_dart_pde/rust/src/auxiliary/custom_handler.rs +++ b/frb_example/pure_dart_pde/rust/src/auxiliary/custom_handler.rs @@ -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; diff --git a/frb_rust/src/handler/mod.rs b/frb_rust/src/handler/mod.rs index d0d9b99a81..efdda17f78 100644 --- a/frb_rust/src/handler/mod.rs +++ b/frb_rust/src/handler/mod.rs @@ -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; diff --git a/frb_rust/src/lib.rs b/frb_rust/src/lib.rs index a105e0cc67..99aa57ad19 100644 --- a/frb_rust/src/lib.rs +++ b/frb_rust/src/lib.rs @@ -1,7 +1,7 @@ //! Main documentation is in mod generalized_isolate; -pub(crate) mod handler; +pub mod handler; mod misc; mod platform_types; mod rust2dart; @@ -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")] @@ -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; diff --git a/frb_rust/src/misc/panic_backtrace.rs b/frb_rust/src/misc/panic_backtrace.rs index 40cb9427bd..607bd8d923 100644 --- a/frb_rust/src/misc/panic_backtrace.rs +++ b/frb_rust/src/misc/panic_backtrace.rs @@ -7,10 +7,14 @@ thread_local! { static BACKTRACE: RefCell> = 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(); @@ -20,7 +24,7 @@ impl PanicBacktrace { })); } - pub(crate) fn catch_unwind R + UnwindSafe, R>( + pub fn catch_unwind R + UnwindSafe, R>( f: F, ) -> Result { std::panic::catch_unwind(f).map_err(|err| CatchUnwindWithBacktrace { @@ -29,12 +33,13 @@ impl PanicBacktrace { }) } - pub(crate) fn take_last() -> Option { + pub fn take_last() -> Option { 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, pub backtrace: Option, } diff --git a/website/docs/guides/custom/rust/handlers.md b/website/docs/guides/custom/rust/handlers.md index b71598fc79..0e70707592 100644 --- a/website/docs/guides/custom/rust/handlers.md +++ b/website/docs/guides/custom/rust/handlers.md @@ -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. @@ -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.