Skip to content

Commit

Permalink
Merge pull request #1574 from fzyzcjy/feat/1543
Browse files Browse the repository at this point in the history
No need to let user write `AssertUnwindSafe` everywhere
  • Loading branch information
fzyzcjy authored Jan 1, 2024
2 parents fcc5740 + 5671cf3 commit c0c7d24
Show file tree
Hide file tree
Showing 53 changed files with 1,063 additions and 1,287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ flutter_rust_bridge::frb_generated_default_handler!();
// Section: wire_funcs

fn wire_greet_impl(
name: impl CstDecode<String> + core::panic::UnwindSafe,
name: impl CstDecode<String>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl WireRustCodecEntrypointTrait<'_> for CstWireRustCodecEntrypoint {
TargetOrCommon::Common => ExternFuncParam {
name: name.clone(),
rust_type: format!(
"impl CstDecode<{}> + core::panic::UnwindSafe",
"impl CstDecode<{}>",
WireRustCodecCstGenerator::new(
field.ty.clone(),
context.as_wire_rust_codec_cst_context()
Expand Down
4 changes: 2 additions & 2 deletions frb_example/dart_minimal/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ flutter_rust_bridge::frb_generated_default_handler!();

fn wire_minimal_adder_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
a: impl CstDecode<i32> + core::panic::UnwindSafe,
b: impl CstDecode<i32> + core::panic::UnwindSafe,
a: impl CstDecode<i32>,
b: impl CstDecode<i32>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand Down
2 changes: 1 addition & 1 deletion frb_example/flutter_via_create/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ flutter_rust_bridge::frb_generated_default_handler!();
// Section: wire_funcs

fn wire_greet_impl(
name: impl CstDecode<String> + core::panic::UnwindSafe,
name: impl CstDecode<String>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ flutter_rust_bridge::frb_generated_default_handler!();
// Section: wire_funcs

fn wire_greet_impl(
name: impl CstDecode<String> + core::panic::UnwindSafe,
name: impl CstDecode<String>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand Down
35 changes: 17 additions & 18 deletions frb_example/gallery/rust/src/api/polars.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,72 @@
use crate::ignore_me::polars_related;
use flutter_rust_bridge::frb;
use polars_lazy::prelude::*;
use std::panic::AssertUnwindSafe;

// This demo is a minimal version of https://github.com/Desdaemon/polars_dart
// Refer to that repository for more details (though may have not migrated to V2 yet)

#[frb(opaque)]
pub struct DataFrame(AssertUnwindSafe<polars_core::prelude::DataFrame>);
pub struct DataFrame(polars_core::prelude::DataFrame);

impl DataFrame {
fn new(inner: polars_core::prelude::DataFrame) -> DataFrame {
Self(AssertUnwindSafe(inner))
Self(inner)
}

#[frb(sync)]
pub fn lazy(self) -> LazyFrame {
LazyFrame::new(self.0 .0.lazy())
LazyFrame::new(self.0.lazy())
}

#[frb(sync)]
pub fn get_column_names(&self) -> Vec<String> {
(self.0 .0.get_column_names().into_iter())
(self.0.get_column_names().into_iter())
.map(|x| x.to_owned())
.collect()
}

// TODO: Can use `DartDynamic` instead
pub fn get_column(&self, name: String) -> anyhow::Result<Vec<String>> {
Ok((self.0 .0.column(&name)?.iter())
Ok((self.0.column(&name)?.iter())
.map(|value| value.to_string())
.collect())
}
}

#[frb(opaque)]
pub struct LazyFrame(AssertUnwindSafe<polars_lazy::prelude::LazyFrame>);
pub struct LazyFrame(polars_lazy::prelude::LazyFrame);

impl LazyFrame {
fn new(inner: polars_lazy::prelude::LazyFrame) -> LazyFrame {
Self(AssertUnwindSafe(inner))
Self(inner)
}

#[frb(sync)]
pub fn filter(self, predicate: Expr) -> LazyFrame {
Self::new(self.0 .0.filter(predicate.0 .0))
Self::new(self.0.filter(predicate.0))
}

#[frb(sync)]
pub fn group_by(self, expr: Expr) -> LazyGroupBy {
LazyGroupBy::new(self.0 .0.group_by(vec![expr.0 .0]))
LazyGroupBy::new(self.0.group_by(vec![expr.0]))
}

pub fn collect(self) -> DataFrame {
DataFrame::new(self.0 .0.collect().unwrap())
DataFrame::new(self.0.collect().unwrap())
}
}

#[frb(opaque)]
pub struct LazyGroupBy(AssertUnwindSafe<polars_lazy::prelude::LazyGroupBy>);
pub struct LazyGroupBy(polars_lazy::prelude::LazyGroupBy);

impl LazyGroupBy {
fn new(inner: polars_lazy::prelude::LazyGroupBy) -> LazyGroupBy {
Self(AssertUnwindSafe(inner))
Self(inner)
}

#[frb(sync)]
pub fn agg(self, expr: Expr) -> LazyFrame {
LazyFrame::new(self.0 .0.agg(vec![expr.0 .0]))
LazyFrame::new(self.0.agg(vec![expr.0]))
}
}

Expand All @@ -77,21 +76,21 @@ pub fn read_sample_dataset() -> DataFrame {

// Instead of opaque, we can also use the translatable types mode
#[frb(opaque)]
pub struct Expr(AssertUnwindSafe<polars_lazy::prelude::Expr>);
pub struct Expr(polars_lazy::prelude::Expr);

impl Expr {
fn new(inner: polars_lazy::prelude::Expr) -> Expr {
Self(AssertUnwindSafe(inner))
Self(inner)
}

#[frb(sync)]
pub fn gt(self, other: Expr) -> Expr {
Expr::new(self.0 .0.gt(other.0 .0))
Expr::new(self.0.gt(other.0))
}

#[frb(sync)]
pub fn sum(self) -> Expr {
Expr::new(self.0 .0.sum())
Expr::new(self.0.sum())
}
}

Expand Down
53 changes: 20 additions & 33 deletions frb_example/gallery/rust/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ flutter_rust_bridge::frb_generated_default_handler!();

fn wire_draw_mandelbrot_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
image_size: impl CstDecode<crate::api::mandelbrot::Size> + core::panic::UnwindSafe,
zoom_point: impl CstDecode<crate::api::mandelbrot::Point> + core::panic::UnwindSafe,
scale: impl CstDecode<f64> + core::panic::UnwindSafe,
num_threads: impl CstDecode<i32> + core::panic::UnwindSafe,
image_size: impl CstDecode<crate::api::mandelbrot::Size>,
zoom_point: impl CstDecode<crate::api::mandelbrot::Point>,
scale: impl CstDecode<f64>,
num_threads: impl CstDecode<i32>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_async::<flutter_rust_bridge::for_generated::DcoCodec, _, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand Down Expand Up @@ -70,9 +70,8 @@ fn wire_draw_mandelbrot_impl(
}
fn wire_DataFrame_get_column_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<DataFrame>>>
+ core::panic::UnwindSafe,
name: impl CstDecode<String> + core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<DataFrame>>>,
name: impl CstDecode<String>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -93,8 +92,7 @@ fn wire_DataFrame_get_column_impl(
)
}
fn wire_DataFrame_get_column_names_impl(
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<DataFrame>>>
+ core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<DataFrame>>>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -114,8 +112,7 @@ fn wire_DataFrame_get_column_names_impl(
)
}
fn wire_DataFrame_lazy_impl(
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<DataFrame>>>
+ core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<DataFrame>>>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -137,10 +134,8 @@ fn wire_DataFrame_lazy_impl(
)
}
fn wire_Expr_gt_impl(
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>
+ core::panic::UnwindSafe,
other: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>
+ core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>,
other: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -164,8 +159,7 @@ fn wire_Expr_gt_impl(
)
}
fn wire_Expr_sum_impl(
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>
+ core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -188,8 +182,7 @@ fn wire_Expr_sum_impl(
}
fn wire_LazyFrame_collect_impl(
port_: flutter_rust_bridge::for_generated::MessagePort,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<LazyFrame>>>
+ core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<LazyFrame>>>,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_normal::<flutter_rust_bridge::for_generated::DcoCodec, _, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -213,10 +206,8 @@ fn wire_LazyFrame_collect_impl(
)
}
fn wire_LazyFrame_filter_impl(
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<LazyFrame>>>
+ core::panic::UnwindSafe,
predicate: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>
+ core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<LazyFrame>>>,
predicate: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -240,10 +231,8 @@ fn wire_LazyFrame_filter_impl(
)
}
fn wire_LazyFrame_group_by_impl(
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<LazyFrame>>>
+ core::panic::UnwindSafe,
expr: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>
+ core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<LazyFrame>>>,
expr: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -267,10 +256,8 @@ fn wire_LazyFrame_group_by_impl(
)
}
fn wire_LazyGroupBy_agg_impl(
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<LazyGroupBy>>>
+ core::panic::UnwindSafe,
expr: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>
+ core::panic::UnwindSafe,
that: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<LazyGroupBy>>>,
expr: impl CstDecode<flutter_rust_bridge::RustOpaque<std::sync::RwLock<Expr>>>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -294,7 +281,7 @@ fn wire_LazyGroupBy_agg_impl(
)
}
fn wire_col_impl(
name: impl CstDecode<String> + core::panic::UnwindSafe,
name: impl CstDecode<String>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand All @@ -313,7 +300,7 @@ fn wire_col_impl(
)
}
fn wire_lit_impl(
t: impl CstDecode<f64> + core::panic::UnwindSafe,
t: impl CstDecode<f64>,
) -> flutter_rust_bridge::for_generated::WireSyncRust2DartDco {
FLUTTER_RUST_BRIDGE_HANDLER.wrap_sync::<flutter_rust_bridge::for_generated::DcoCodec, _>(
flutter_rust_bridge::for_generated::TaskInfo {
Expand Down
3 changes: 1 addition & 2 deletions frb_example/gallery/rust/src/ignore_me/mandelbrot_related.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use flutter_rust_bridge::spawn_blocking_with;
use image::codecs::png::PngEncoder;
use image::*;
use num::Complex;
use std::panic::AssertUnwindSafe;

/// Try to determine if `c` is in the Mandelbrot set, using at most `limit`
/// iterations to decide.
Expand Down Expand Up @@ -186,7 +185,7 @@ pub async fn mandelbrot(
));
}

let bands_arr = AssertUnwindSafe(try_join_all(join_handles)).await?;
let bands_arr = try_join_all(join_handles).await?;
let pixels = bands_arr.concat();

write_image(&colorize(&pixels), bounds)
Expand Down
8 changes: 4 additions & 4 deletions frb_example/pure_dart/frb_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -9673,9 +9673,9 @@ void frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_non

void frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_non_send_hide_data(const void *ptr);

void frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_stdsyncRwLockBoxdynFnStringStringSendSyncUnwindSafeRefUnwindSafe(const void *ptr);
void frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_stdsyncRwLockBoxdynFnStringStringSendSync(const void *ptr);

void frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_stdsyncRwLockBoxdynFnStringStringSendSyncUnwindSafeRefUnwindSafe(const void *ptr);
void frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_stdsyncRwLockBoxdynFnStringStringSendSync(const void *ptr);

void frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_stdsyncRwLockBoxdynHelloTraitTwinNormal(const void *ptr);

Expand Down Expand Up @@ -11111,7 +11111,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_i_32);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_non_clone_data);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_non_send_hide_data);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_stdsyncRwLockBoxdynFnStringStringSendSyncUnwindSafeRefUnwindSafe);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_stdsyncRwLockBoxdynFnStringStringSendSync);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_stdsyncRwLockBoxdynHelloTraitTwinNormal);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_stdsyncRwLockBoxdynHelloTraitTwinSse);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_decrement_strong_count_RustOpaque_stdsyncRwLockBoxdynHelloTraitTwinSync);
Expand Down Expand Up @@ -11142,7 +11142,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_i_32);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_non_clone_data);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_non_send_hide_data);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_stdsyncRwLockBoxdynFnStringStringSendSyncUnwindSafeRefUnwindSafe);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_stdsyncRwLockBoxdynFnStringStringSendSync);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_stdsyncRwLockBoxdynHelloTraitTwinNormal);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_stdsyncRwLockBoxdynHelloTraitTwinSse);
dummy_var ^= ((int64_t) (void*) frbgen_frb_example_pure_dart_rust_arc_increment_strong_count_RustOpaque_stdsyncRwLockBoxdynHelloTraitTwinSync);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ Future<StructWithGoodAndOpaqueFieldTwinSse>
.rustAutoOpaqueStructWithGoodAndOpaqueFieldReturnOwnTwinSse(
hint: hint);

// Rust type: flutter_rust_bridge::RustOpaque<std::sync::RwLock<Box<dyn Fn (String) -> String + Send + Sync + UnwindSafe + RefUnwindSafe>>>
// Rust type: flutter_rust_bridge::RustOpaque<std::sync::RwLock<Box<dyn Fn (String) -> String + Send + Sync>>>
@sealed
class BoxFnStringString extends RustOpaque {
BoxFnStringString.dcoDecode(List<dynamic> wire)
Expand Down
Loading

0 comments on commit c0c7d24

Please sign in to comment.