Skip to content

Hoist bsn expressions up to reduce ownership / type constraints#23905

Merged
cart merged 1 commit intobevyengine:mainfrom
cart:hoist_bsn
Apr 21, 2026
Merged

Hoist bsn expressions up to reduce ownership / type constraints#23905
cart merged 1 commit intobevyengine:mainfrom
cart:hoist_bsn

Conversation

@cart
Copy link
Copy Markdown
Member

@cart cart commented Apr 21, 2026

Objective

bsn! currently embeds expressions directly into the patch closure, which requires moving all of the "expression items" into the closure. This thrusts constraints like Send + Sync + 'static on anything used in the expression:

/// Workaround 1 (used in Feathers)
fn label(text: impl Into<String>) -> impl Scene {
    let text = Text::new(text.into());
    bsn! {
        template_value(text)
    }
}
/// Workaround 2
fn label(text: impl Into<String> + Send + Sync + 'static) -> impl Scene {
    bsn! {
        Text(text)
    }
}

Solution

Hoist expression above the closures, evaluate them immediately, and move the results into the closure. This significantly reduces the burden on developers defining scenes.

This now works! No workaround necessary!

fn label(text: impl Into<String>) -> impl Scene {
    bsn! {
        Text(text)
    }
}

Borrowing like this is also allowed because of implicit into(), which converts the &str into String, which is then captured in the patch closure:

fn label(text: &str) -> impl Scene {
    bsn! {
        Text(text)
    }
}

Likewise, patterns like this now work:

fn cube(meshes: &mut Assets<Mesh>) -> impl Scene {
    bsn! {
        Mesh3d({meshes.add(Cuboid::default())})
    }
}

@cart cart added this to the 0.19 milestone Apr 21, 2026
@cart cart added C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Scenes Composing and serializing ECS objects labels Apr 21, 2026
Copy link
Copy Markdown
Contributor

@andriyDev andriyDev left a comment

Choose a reason for hiding this comment

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

Nice!

@kfc35 kfc35 added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Apr 21, 2026
@cart cart added this pull request to the merge queue Apr 21, 2026
Merged via the queue into bevyengine:main with commit 95d219f Apr 21, 2026
47 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Scenes Composing and serializing ECS objects C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants