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

fix(core): šŸ› resolved Storybook crash and enhanced panic location repā€¦ #559

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ Please only add new entries below the [Unreleased](#unreleased---releasedate) he

- **ribir**: compile wasm (#543 @wjian23)

### Changed

- **core**: Enhanced panic location tracking during widget build (#559 @M-Adoo)

### Fixed

- **examples**: fix crash issue in Storybook (#559 @M-Adoo)


## [0.3.0-alpha.2] - 2024-04-03


Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ keywords = ["gui", "ui", "declarative", "compose-ui"]
license = "MIT"
readme = "README.md"
version = "0.3.0-alpha.3"
package.rust-version = "1.77.0"
package = "1.77.0"

[workspace.dependencies]
ahash = "0.8.11"
Expand Down
13 changes: 12 additions & 1 deletion core/src/builtin_widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ impl<T> FatObj<T> {
/// Maps an `FatObj<T>` to `FatObj<V>` by applying a function to the host
/// object.
#[inline]
#[track_caller]
pub fn map<V>(self, f: impl FnOnce(T) -> V) -> FatObj<V> {
FatObj {
host: f(self.host),
Expand Down Expand Up @@ -985,6 +986,7 @@ impl<T: MultiChild> MultiChild for FatObj<T> {}

crate::widget::multi_build_replace_impl! {
impl<T: {#} > {#} for FatObj<T> {
#[track_caller]
fn build(self, ctx: &BuildCtx) -> Widget {
self.map(|host| host.build(ctx)).build(ctx)
}
Expand All @@ -993,6 +995,7 @@ crate::widget::multi_build_replace_impl! {

impl WidgetBuilder for FatObj<Widget> {
#[inline]
#[track_caller]
fn build(self, ctx: &BuildCtx) -> Widget {
let mut host = self.host;
self.host_id.set(host.id());
Expand Down Expand Up @@ -1069,32 +1072,40 @@ impl<T: ComposeWithChild<C, M>, C, M> ComposeWithChild<C, M> for FatObj<T> {
type Target = FatObj<T::Target>;

#[inline]
#[track_caller]
fn with_child(self, child: C, ctx: &BuildCtx) -> Self::Target {
self.map(|host| host.with_child(child, ctx))
self.map(
#[cfg_attr(feature = "nightly", track_caller)]
|host| host.with_child(child, ctx),
)
}
}

impl<C> SingleWithChild<C, ()> for FatObj<()> {
type Target = FatObj<C>;

#[inline]
#[track_caller]
fn with_child(self, child: C, _: &BuildCtx) -> Self::Target { self.map(move |_| child) }
}

impl<T: PairWithChild<C>, C> PairWithChild<C> for FatObj<T> {
type Target = Pair<FatObj<T>, C>;

#[inline]
#[track_caller]
fn with_child(self, child: C, _: &BuildCtx) -> Self::Target { Pair::new(self, child) }
}

impl<T: SingleParent + 'static> SingleParent for FatObj<T> {
#[track_caller]
fn compose_child(self, child: Widget, ctx: &BuildCtx) -> Widget {
self.map(|host| host.compose_child(child, ctx)).build(ctx)
}
}

impl<T: MultiParent + 'static> MultiParent for FatObj<T> {
#[track_caller]
fn compose_children(self, children: impl Iterator<Item = Widget>, ctx: &BuildCtx) -> Widget {
self
.map(|host| host.compose_children(children, ctx))
Expand Down
2 changes: 2 additions & 0 deletions core/src/builtin_widgets/theme/compose_decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ where
State<T>: ComposeWithChild<C, M>,
{
type Target = <State<T> as ComposeWithChild<C, M>>::Target;
#[track_caller]
fn with_child(self, child: C, ctx: &BuildCtx) -> Self::Target {
State::value(self).with_child(child, ctx)
}
Expand All @@ -36,6 +37,7 @@ where
Widget: ChildFrom<C, M>,
{
type Target = Widget;
#[track_caller]
fn with_child(self, child: C, ctx: &BuildCtx) -> Self::Target {
let tid = TypeId::of::<W>();
let style = ctx.find_cfg(|t| match t {
Expand Down
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(feature = "nightly", feature(closure_track_caller))]
#[macro_use]
extern crate bitflags;
extern crate lazy_static;
Expand Down
2 changes: 2 additions & 0 deletions core/src/widget_children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,14 @@ impl<W: PairChild, C> PairWithChild<C> for W {
type Target = Pair<W, C>;

#[inline]
#[track_caller]
fn with_child(self, child: C, _: &BuildCtx) -> Self::Target { Pair { parent: self, child } }
}

impl<W, C1: PairChild, C2> PairWithChild<C2> for Pair<W, C1> {
type Target = Pair<W, Pair<C1, C2>>;

#[track_caller]
fn with_child(self, c: C2, ctx: &BuildCtx) -> Self::Target {
let Pair { parent: widget, child } = self;
Pair {
Expand Down
1 change: 1 addition & 0 deletions core/src/widget_children/child_convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ impl<T> ChildFrom<T, ()> for T {

impl<C, T: FromAnother<C, M>, M> ChildFrom<C, (M,)> for T {
#[inline]
#[track_caller]
fn child_from(value: C, ctx: &BuildCtx) -> Self { FromAnother::from_another(value, ctx) }
}

Expand Down
9 changes: 7 additions & 2 deletions core/src/widget_children/compose_child_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ where
State<T>: ComposeWithChild<C, M>,
{
type Target = <State<T> as ComposeWithChild<C, M>>::Target;
#[track_caller]
fn with_child(self, child: C, ctx: &BuildCtx) -> Self::Target {
State::value(self).with_child(child, ctx)
}
Expand All @@ -41,7 +42,7 @@ where
C1: ComposeWithChild<C2, M>,
{
type Target = Pair<W, C1::Target>;

#[track_caller]
fn with_child(self, c: C2, ctx: &BuildCtx) -> Self::Target {
let Pair { parent: widget, child } = self;
Pair {
Expand All @@ -60,6 +61,7 @@ where
type Target = Pair<Self, Child>;

#[inline]
#[track_caller]
fn with_child(self, child: C, ctx: &BuildCtx) -> Self::Target {
Pair {
parent: self,
Expand All @@ -78,6 +80,7 @@ where
type Target = Pair<W, Child::Builder>;

#[inline]
#[track_caller]
fn with_child(self, c: C, ctx: &BuildCtx) -> Self::Target {
let builder = Child::builder();
let child = builder.with_child(c, ctx);
Expand All @@ -93,7 +96,7 @@ where
Child::Builder: ComposeWithChild<C, M, Target = Child::Builder>,
{
type Target = Pair<W, Child::Builder>;

#[track_caller]
fn with_child(self, c: C, ctx: &BuildCtx) -> Self::Target {
let builder = Child::builder();
let child = builder.with_child(c, ctx);
Expand Down Expand Up @@ -146,6 +149,7 @@ where
type Target = Self;

#[inline]
#[track_caller]
fn with_child(mut self, child: C, ctx: &BuildCtx) -> Self::Target {
self.push(ChildFrom::child_from(child, ctx));
self
Expand All @@ -160,6 +164,7 @@ where
type Target = Self;

#[inline]
#[track_caller]
fn with_child(mut self, child: C, ctx: &BuildCtx) -> Self::Target {
self.extend(child.into_iter().map(|v| ChildFrom::child_from(v, ctx)));
self
Expand Down
3 changes: 2 additions & 1 deletion core/src/widget_children/multi_child_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ where
C: FillVec<M>,
{
type Target = MultiPair<P>;

#[track_caller]
fn with_child(self, child: C, ctx: &BuildCtx) -> Self::Target {
let mut children = vec![];
child.fill_vec(&mut children, ctx);
Expand All @@ -68,6 +68,7 @@ where
{
type Target = Self;
#[inline]
#[track_caller]
fn with_child(mut self, child: C, ctx: &BuildCtx) -> Self::Target {
child.fill_vec(&mut self.children, ctx);
self
Expand Down
6 changes: 3 additions & 3 deletions core/src/widget_children/single_child_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait SingleWithChild<C, M: ?Sized> {
crate::widget::multi_build_replace_impl_include_self! {
impl<P: SingleParent, C: {#}> SingleWithChild<C, dyn {#}> for P {
type Target = Widget;

#[track_caller]
fn with_child(self, child: C, ctx: &BuildCtx) -> Self::Target {
self.compose_child(child.build(ctx), ctx)
}
Expand All @@ -24,7 +24,7 @@ crate::widget::multi_build_replace_impl_include_self! {
C: {#},
{
type Target = Widget;

#[track_caller]
fn with_child(self, child: Option<C>, ctx: &BuildCtx) -> Self::Target {
if let Some(child) = child {
self.with_child(child, ctx)
Expand All @@ -43,7 +43,7 @@ crate::widget::multi_build_replace_impl_include_self! {
V: {#} + 'static,
{
type Target = Widget;

#[track_caller]
fn with_child(self, child: PP, ctx: &BuildCtx) -> Self::Target {
let child = crate::pipe::pipe_option_to_widget!(child, ctx);
self.with_child(child, ctx)
Expand Down
3 changes: 3 additions & 0 deletions examples/storybook/src/storybook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,21 @@ fn content() -> impl WidgetBuilder {
@ { svgs::HOME }
@ { Label::new("Video") }
}
@TabPane { @ { fn_widget!(Void) } }
}
@Tab {
@TabItem {
@ { svgs::HOME }
@ { Label::new("Photos") }
}
@TabPane { @ { fn_widget!(Void) } }
}
@Tab {
@TabItem {
@ { svgs::HOME }
@ { Label::new("Audio") }
}
@TabPane { @ { fn_widget!(Void) } }
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions macros/src/child_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) fn derive_child_template(input: &mut syn::DeriveInput) -> syn::Result
tokens.extend(quote! {
impl #g_impl ComposeWithChild<_C, [_M; #f_idx]> for #builder #g_ty #g_where {
type Target = Self;
#[track_caller]
fn with_child(mut self, c: _C, ctx: &BuildCtx) -> Self::Target {
assert!(self.#field_name.is_none(), "Try to fill same type twice.");
self.#field_name = Some(ChildFrom::child_from(c, ctx));
Expand Down Expand Up @@ -71,16 +72,19 @@ pub(crate) fn derive_child_template(input: &mut syn::DeriveInput) -> syn::Result

impl #g_impl FromAnother<#builder #g_ty, ()> for #name #g_ty #g_where {
#[inline]
#[track_caller]
fn from_another(value: #builder #g_ty, _: &BuildCtx) -> Self { value.build_tml() }
}

impl #g_impl std::convert::From<#builder #g_ty> for #name #g_ty #g_where {
#[inline]
#[track_caller]
fn from(value: #builder #g_ty) -> Self { value.build_tml() }
}

impl #g_impl std::convert::From<#builder #g_ty> for Option<#name #g_ty> #g_where {
#[inline]
#[track_caller]
fn from(value: #builder #g_ty) -> Self { Some(value.build_tml()) }
}
});
Expand Down Expand Up @@ -110,6 +114,7 @@ pub(crate) fn derive_child_template(input: &mut syn::DeriveInput) -> syn::Result
impl #g_impl TemplateBuilder for #builder #g_ty #g_where {
type Target = #name #g_ty;
#[inline]
#[track_caller]
fn build_tml(self) -> Self::Target {#name { #(#init_values),* }}
}
});
Expand All @@ -133,7 +138,7 @@ pub(crate) fn derive_child_template(input: &mut syn::DeriveInput) -> syn::Result
tokens.extend(quote! {
impl #g_impl TemplateBuilder for #builder #g_ty #g_where {
type Target = #name #g_ty;
#[inline]
#[track_caller]
fn build_tml(self) -> Self::Target {#name(#(#init_values),* ) }
}
});
Expand All @@ -152,7 +157,7 @@ pub(crate) fn derive_child_template(input: &mut syn::DeriveInput) -> syn::Result

impl #g_impl TemplateBuilder for #builder #g_ty #g_where {
type Target = #name #g_ty;
#[inline]
#[track_caller]
fn build_tml(self) -> Self::Target {
self.0.expect(&#err_str)
}
Expand Down
Loading