Skip to content

Commit 2aeb58d

Browse files
zicklagTekhnaeRaav
andauthored
refactor: change Sessions::create_with to take SessionPlugin (#499)
I'm opening this PR for @TekhnaeRaav, but I'm not sure about this one yet, since it does require annotating the argument type when passing in a closure, which isn't as nice, even though it's more flexible because you can pass in any plugin. I'm a little less acquainted with this API right now. @MaxCWhitehead whenever you get the chance to take a peek at this I wonder if you have thoughts. Co-authored-by: Tekhnae Raav <tekhnaeraav@katharostech.com>
1 parent f80fa50 commit 2aeb58d

File tree

6 files changed

+53
-48
lines changed

6 files changed

+53
-48
lines changed

demos/asset_packs/src/main.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ fn main() {
4646

4747
// Create a new session for the game menu. Each session is it's own bones world with it's own
4848
// plugins, systems, and entities.
49-
game.sessions.create_with("menu", |builder| {
50-
// Install the default bones_framework plugin for this session
51-
builder
52-
.install_plugin(DefaultSessionPlugin)
53-
// Add our menu system to the update stage
54-
.add_system_to_stage(Update, menu_system);
55-
});
49+
game.sessions
50+
.create_with("menu", |builder: &mut SessionBuilder| {
51+
// Install the default bones_framework plugin for this session
52+
builder
53+
.install_plugin(DefaultSessionPlugin)
54+
// Add our menu system to the update stage
55+
.add_system_to_stage(Update, menu_system);
56+
});
5657

5758
BonesBevyRenderer::new(game).app().run();
5859
}

demos/hello_world/src/main.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ fn main() {
1010

1111
// Create a new session for the game menu. Each session is it's own bones world with it's own
1212
// plugins, systems, and entities.
13-
game.sessions.create_with("menu", |session| {
14-
session
15-
// Install the default bones_framework plugin for this session
16-
.install_plugin(DefaultSessionPlugin)
17-
// Add our menu system to the update stage
18-
.add_system_to_stage(Update, menu_system);
19-
});
13+
game.sessions
14+
.create_with("menu", |session: &mut SessionBuilder| {
15+
session
16+
// Install the default bones_framework plugin for this session
17+
.install_plugin(DefaultSessionPlugin)
18+
// Add our menu system to the update stage
19+
.add_system_to_stage(Update, menu_system);
20+
});
2021

2122
BonesBevyRenderer::new(game).app().run();
2223
}

demos/scripting/src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ fn main() {
2222
.register_default_assets();
2323
GameMeta::register_schema();
2424

25-
game.sessions.create_with("launch", |builder| {
26-
builder.add_startup_system(launch_game_session);
27-
});
25+
game.sessions
26+
.create_with("launch", |builder: &mut SessionBuilder| {
27+
builder.add_startup_system(launch_game_session);
28+
});
2829

2930
let mut renderer = BonesBevyRenderer::new(game);
3031
renderer.app_namespace = (
@@ -42,7 +43,7 @@ fn launch_game_session(
4243
) {
4344
session_ops.delete = true;
4445
// Build game session and add to `Sessions`
45-
sessions.create_with("game", |builder| {
46+
sessions.create_with("game", |builder: &mut SessionBuilder| {
4647
builder
4748
.install_plugin(DefaultSessionPlugin)
4849
// Install the plugin that will load our lua plugins and run them in the game session

framework_crates/bones_framework/tests/reset.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ pub fn startup_system_reset() {
1111
game.init_shared_resource::<Counter>();
1212

1313
// Session startup increments counter by 1
14-
game.sessions.create_with("game", |builder| {
15-
builder.add_startup_system(|mut counter: ResMut<Counter>| {
16-
// Increment to 1
17-
counter.0 += 1;
14+
game.sessions
15+
.create_with("game", |builder: &mut SessionBuilder| {
16+
builder.add_startup_system(|mut counter: ResMut<Counter>| {
17+
// Increment to 1
18+
counter.0 += 1;
19+
});
1820
});
19-
});
2021

2122
// Step twice, startup system should only run once
2223
game.step(Instant::now());
@@ -52,23 +53,24 @@ pub fn single_success_system_reset() {
5253
let mut game = Game::new();
5354

5455
// Session startup increments counter by 1
55-
game.sessions.create_with("game", |builder| {
56-
builder.init_resource::<Counter>();
57-
{
58-
let res = builder.resource_mut::<Counter>().unwrap();
59-
assert_eq!(res.0, 0);
60-
}
61-
// system
62-
builder.add_single_success_system(|mut counter: ResMut<Counter>| -> Option<()> {
63-
// Increment until 2
64-
counter.0 += 1;
65-
if counter.0 >= 2 {
66-
return Some(());
56+
game.sessions
57+
.create_with("game", |builder: &mut SessionBuilder| {
58+
builder.init_resource::<Counter>();
59+
{
60+
let res = builder.resource_mut::<Counter>().unwrap();
61+
assert_eq!(res.0, 0);
6762
}
68-
69-
None
63+
// system
64+
builder.add_single_success_system(|mut counter: ResMut<Counter>| -> Option<()> {
65+
// Increment until 2
66+
counter.0 += 1;
67+
if counter.0 >= 2 {
68+
return Some(());
69+
}
70+
71+
None
72+
});
7073
});
71-
});
7274

7375
// Step three times, single success should've incremented counter to 2 and completed.
7476
game.step(Instant::now());
@@ -126,9 +128,10 @@ pub fn reset_world_resource_override() {
126128
let mut game = Game::new();
127129

128130
// insert counter resource
129-
game.sessions.create_with("game", |builder| {
130-
builder.init_resource::<Counter>();
131-
});
131+
game.sessions
132+
.create_with("game", |builder: &mut SessionBuilder| {
133+
builder.init_resource::<Counter>();
134+
});
132135

133136
game.step(Instant::now());
134137
{
@@ -165,9 +168,10 @@ pub fn reset_world_emtpy_resource() {
165168
let mut game = Game::new();
166169

167170
// insert counter resource
168-
game.sessions.create_with("game", |builder| {
169-
builder.init_resource::<Counter>();
170-
});
171+
game.sessions
172+
.create_with("game", |builder: &mut SessionBuilder| {
173+
builder.init_resource::<Counter>();
174+
});
171175

172176
game.step(Instant::now());
173177
{

framework_crates/bones_lib/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,13 +792,13 @@ impl Sessions {
792792
pub fn create_with<N: TryInto<Ustr>>(
793793
&mut self,
794794
name: N,
795-
build_function: impl FnOnce(&mut SessionBuilder),
795+
plugin: impl SessionPlugin,
796796
) -> &mut Session
797797
where
798798
<N as TryInto<Ustr>>::Error: Debug,
799799
{
800800
let mut builder = SessionBuilder::new(name);
801-
build_function(&mut builder);
801+
builder.install_plugin(plugin);
802802
builder.finish_and_add(self)
803803
}
804804

framework_crates/bones_schema/src/alloc/layout.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ const LAYOUT_ERR: LayoutError = if let Err(e) = Layout::from_size_align(0, 0) {
4141
};
4242

4343
impl LayoutExt for Layout {
44-
#[must_use = "this returns the padding needed, \
45-
without modifying the `Layout`"]
4644
#[inline]
4745
fn padding_needed_for(&self, align: usize) -> usize {
4846
let len = self.size();

0 commit comments

Comments
 (0)