Skip to content

Commit a54e139

Browse files
committed
remove some unnecessary optionals
1 parent d108cbd commit a54e139

File tree

5 files changed

+23
-46
lines changed

5 files changed

+23
-46
lines changed

crates/bevy_mod_scripting_core/src/commands.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ impl<P: IntoScriptPluginParams> Command for DeleteScript<P> {
3535
let runner = world
3636
.get_resource::<CallbackSettings<P>>()
3737
.expect("No CallbackSettings resource found")
38-
.callback_handler
39-
.expect("No callback handler set");
38+
.callback_handler;
4039

4140
let mut ctxts = world
4241
.remove_non_send_resource::<ScriptContexts<P>>()
@@ -132,7 +131,7 @@ impl<P: IntoScriptPluginParams> Command for CreateOrUpdateScript<P> {
132131
// assign context
133132
let assigner = settings.assigner.clone();
134133
let builder = settings.loader.clone();
135-
let runner = runner.callback_handler.expect("No callback handler set");
134+
let runner = runner.callback_handler;
136135

137136
world.resource_scope(|world, mut scripts: Mut<Scripts>| {
138137

crates/bevy_mod_scripting_core/src/handler.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,7 @@ pub type HandlerFn<P> = fn(
3838
/// A resource that holds the settings for the callback handler for a specific combination of type parameters
3939
#[derive(Resource)]
4040
pub struct CallbackSettings<P: IntoScriptPluginParams> {
41-
pub callback_handler: Option<HandlerFn<P>>,
42-
}
43-
44-
impl<P: IntoScriptPluginParams> Default for CallbackSettings<P> {
45-
fn default() -> Self {
46-
Self {
47-
callback_handler: None,
48-
}
49-
}
41+
pub callback_handler: HandlerFn<P>,
5042
}
5143

5244
macro_rules! push_err_and_continue {
@@ -88,16 +80,7 @@ pub fn event_handler<L: IntoCallbackLabel, P: IntoScriptPluginParams>(
8880
let (mut script_events, callback_settings, context_settings, scripts, entities) =
8981
params.get_mut(world);
9082

91-
let handler = *callback_settings
92-
.callback_handler
93-
.as_ref()
94-
.unwrap_or_else(|| {
95-
panic!(
96-
"No handler registered for - Runtime: {}, Context: {}",
97-
type_name::<P::R>(),
98-
type_name::<P::C>()
99-
)
100-
});
83+
let handler = callback_settings.callback_handler;
10184
let pre_handling_initializers = context_settings.context_pre_handling_initializers.clone();
10285
let scripts = scripts.clone();
10386
let mut errors = Vec::default();
@@ -253,7 +236,7 @@ mod test {
253236
app.add_event::<ScriptCallbackEvent>();
254237
app.add_event::<ScriptErrorEvent>();
255238
app.insert_resource::<CallbackSettings<P>>(CallbackSettings {
256-
callback_handler: Some(handler_fn),
239+
callback_handler: handler_fn,
257240
});
258241
app.add_systems(Update, event_handler::<L, P>);
259242
app.insert_resource::<Scripts>(Scripts { scripts });

crates/bevy_mod_scripting_core/src/lib.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,20 @@ pub trait IntoScriptPluginParams: 'static {
5353
type R: Runtime;
5454

5555
fn build_runtime() -> Self::R;
56-
57-
// fn supported_language() -> Language;
5856
}
5957

6058
/// Bevy plugin enabling scripting within the bevy mod scripting framework
6159
pub struct ScriptingPlugin<P: IntoScriptPluginParams> {
6260
/// Settings for the runtime
63-
pub runtime_settings: Option<RuntimeSettings<P>>,
61+
pub runtime_settings: RuntimeSettings<P>,
6462
/// The handler used for executing callbacks in scripts
65-
pub callback_handler: Option<HandlerFn<P>>,
63+
pub callback_handler: HandlerFn<P>,
6664
/// The context builder for loading contexts
6765
pub context_builder: ContextBuilder<P>,
6866
/// The context assigner for assigning contexts to scripts.
6967
pub context_assigner: ContextAssigner<P>,
70-
pub language_mapper: Option<AssetPathToLanguageMapper>,
68+
69+
pub language_mapper: AssetPathToLanguageMapper,
7170

7271
/// initializers for the contexts, run when loading the script
7372
pub context_initializers: Vec<ContextInitializer<P>>,
@@ -77,7 +76,7 @@ pub struct ScriptingPlugin<P: IntoScriptPluginParams> {
7776

7877
impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
7978
fn build(&self, app: &mut bevy::prelude::App) {
80-
app.insert_resource(self.runtime_settings.as_ref().cloned().unwrap_or_default())
79+
app.insert_resource(self.runtime_settings.clone())
8180
.insert_non_send_resource::<RuntimeContainer<P>>(RuntimeContainer {
8281
runtime: P::build_runtime(),
8382
})
@@ -95,13 +94,11 @@ impl<P: IntoScriptPluginParams> Plugin for ScriptingPlugin<P> {
9594
register_script_plugin_systems::<P>(app);
9695
once_per_app_init(app);
9796

98-
if let Some(language_mapper) = &self.language_mapper {
99-
app.world_mut()
100-
.resource_mut::<ScriptAssetSettings>()
101-
.as_mut()
102-
.script_language_mappers
103-
.push(*language_mapper);
104-
}
97+
app.world_mut()
98+
.resource_mut::<ScriptAssetSettings>()
99+
.as_mut()
100+
.script_language_mappers
101+
.push(self.language_mapper);
105102

106103
register_types(app);
107104
}
@@ -131,10 +128,7 @@ impl<P: IntoScriptPluginParams> ScriptingPlugin<P> {
131128
///
132129
/// Initializers will be run after the runtime is created, but before any contexts are loaded.
133130
pub fn add_runtime_initializer(&mut self, initializer: RuntimeInitializer<P>) -> &mut Self {
134-
self.runtime_settings
135-
.get_or_insert_with(Default::default)
136-
.initializers
137-
.push(initializer);
131+
self.runtime_settings.initializers.push(initializer);
138132
self
139133
}
140134
}

crates/languages/bevy_mod_scripting_lua/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use bevy_mod_scripting_core::{
1212
error::ScriptError,
1313
event::CallbackLabel,
1414
reflection_extensions::PartialReflectExt,
15+
runtime::RuntimeSettings,
1516
script::ScriptId,
1617
IntoScriptPluginParams, ScriptingPlugin,
1718
};
@@ -47,15 +48,15 @@ impl Default for LuaScriptingPlugin {
4748
LuaScriptingPlugin {
4849
scripting_plugin: ScriptingPlugin {
4950
context_assigner: Default::default(),
50-
runtime_settings: None,
51-
callback_handler: Some(lua_handler),
51+
runtime_settings: RuntimeSettings::default(),
52+
callback_handler: lua_handler,
5253
context_builder: ContextBuilder::<LuaScriptingPlugin> {
5354
load: lua_context_load,
5455
reload: lua_context_reload,
5556
},
56-
language_mapper: Some(AssetPathToLanguageMapper {
57+
language_mapper: AssetPathToLanguageMapper {
5758
map: lua_language_mapper,
58-
}),
59+
},
5960
context_initializers: vec![
6061
|_script_id, context| {
6162
// set the world global

crates/script_integration_test_harness/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn execute_integration_test<
8181
})?;
8282

8383
// call on_script_loaded as normal
84-
let val = (callback_settings.callback_handler.unwrap())(
84+
let val = (callback_settings.callback_handler)(
8585
vec![],
8686
Entity::from_raw(0),
8787
&(script_id.to_owned()).into(),
@@ -98,7 +98,7 @@ pub fn execute_integration_test<
9898
}
9999

100100
// call on_test callback
101-
let val = (callback_settings.callback_handler.unwrap())(
101+
let val = (callback_settings.callback_handler)(
102102
vec![],
103103
Entity::from_raw(0),
104104
&(script_id.to_owned()).into(),

0 commit comments

Comments
 (0)