Skip to content

Replace unwraps with expects in bevy_asset #3892

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

Closed
Closed
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
21 changes: 16 additions & 5 deletions crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ impl AssetServer {
// load asset dependencies and prepare asset type hashmap
for (label, loaded_asset) in load_context.labeled_assets.iter_mut() {
let label_id = LabelId::from(label.as_ref().map(|label| label.as_str()));
let type_uuid = loaded_asset.value.as_ref().unwrap().type_uuid();
let type_uuid = loaded_asset
.value
.as_ref()
.expect("Asset {label_id} was not loaded.")
.type_uuid();
source_info.asset_types.insert(label_id, type_uuid);
for dependency in loaded_asset.dependencies.iter() {
self.load_untracked(dependency.clone(), false);
Expand All @@ -358,7 +362,7 @@ impl AssetServer {
self.server
.asset_io
.watch_path_for_changes(asset_path.path())
.unwrap();
.expect("Could not watch path for changes.");
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this one could be .watch_path_for_changes(asset_path.path())?;

self.create_assets_in_load_context(&mut load_context);
Ok(asset_path_id)
}
Expand Down Expand Up @@ -399,7 +403,9 @@ impl AssetServer {
let path = path.as_ref();
if !self.server.asset_io.is_directory(path) {
return Err(AssetServerError::AssetFolderNotADirectory(
path.to_str().unwrap().to_string(),
path.to_str()
.expect("Could not convert path to string.")
Copy link
Contributor

@rparrett rparrett Feb 8, 2022

Choose a reason for hiding this comment

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

Seems like this should only fail if the path has invalid utf8, so the message could be made more specific.

.to_string(),
));
}

Expand Down Expand Up @@ -500,11 +506,16 @@ impl AssetServer {
// triggered unless the `Assets` collection is actually updated.
pub(crate) fn update_asset_storage<T: Asset>(&self, mut assets: ResMut<Assets<T>>) {
let asset_lifecycles = self.server.asset_lifecycles.read();
let asset_lifecycle = asset_lifecycles.get(&T::TYPE_UUID).unwrap();
let asset_lifecycle = asset_lifecycles.get(&T::TYPE_UUID).unwrap_or_else(|| {
panic!(
"Could not find an asset of the appropriate type UUID={}.",
T::TYPE_UUID
)
});
let mut asset_sources_guard = None;
let channel = asset_lifecycle
.downcast_ref::<AssetLifecycleChannel<T>>()
.unwrap();
.expect("Could not downcast successfully.");

loop {
match channel.receiver.try_recv() {
Expand Down
4 changes: 3 additions & 1 deletion crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ impl AddAsset for App {
return self;
}
let assets = {
let asset_server = self.world.get_resource::<AssetServer>().unwrap();
let asset_server = self.world.get_resource::<AssetServer>().expect(
"Could not find `AssetServer` resource in the `World`. Did you forget to add it?",
);
asset_server.register_asset_type::<T>()
};

Expand Down
17 changes: 13 additions & 4 deletions crates/bevy_asset/src/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ impl Debug for HandleType {

impl<T: Asset> Handle<T> {
pub(crate) fn strong(id: HandleId, ref_change_sender: Sender<RefChange>) -> Self {
ref_change_sender.send(RefChange::Increment(id)).unwrap();
ref_change_sender
.send(RefChange::Increment(id))
.expect("Could not increment reference count when creating a strong handle.");
Self {
id,
handle_type: HandleType::Strong(ref_change_sender),
Expand Down Expand Up @@ -174,7 +176,9 @@ impl<T: Asset> Handle<T> {
return;
}
let sender = assets.ref_change_sender.clone();
sender.send(RefChange::Increment(self.id)).unwrap();
sender.send(RefChange::Increment(self.id)).expect(
"Could not increment reference count when converting a weak handle a strong handle.",
);
self.handle_type = HandleType::Strong(sender);
}

Expand Down Expand Up @@ -278,7 +282,10 @@ impl<T: Asset> Default for Handle<T> {

impl<T: Asset> Debug for Handle<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
let name = std::any::type_name::<T>().split("::").last().unwrap();
let name = std::any::type_name::<T>()
.split("::")
.last()
.expect("Could not split type name correctly.");
write!(f, "{:?}Handle<{}>({:?})", self.handle_type, name, self.id)
}
}
Expand Down Expand Up @@ -313,7 +320,9 @@ impl HandleUntyped {
}

pub(crate) fn strong(id: HandleId, ref_change_sender: Sender<RefChange>) -> Self {
ref_change_sender.send(RefChange::Increment(id)).unwrap();
ref_change_sender
.send(RefChange::Increment(id))
.expect("Could not increment reference count when creating a strong handle.");
Self {
id,
handle_type: HandleType::Strong(ref_change_sender),
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_asset/src/io/android_asset_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl AssetIo for AndroidAssetIo {
Box::pin(async move {
let asset_manager = ndk_glue::native_activity().asset_manager();
let mut opened_asset = asset_manager
.open(&CString::new(path.to_str().unwrap()).unwrap())
.open(&CString::new(path.to_str().expect("Could not convert path to string")).expect("Could not open file"))
.ok_or(AssetIoError::NotFound(path.to_path_buf()))?;
let bytes = opened_asset.get_buffer()?;
Ok(bytes.to_vec())
Expand Down
18 changes: 12 additions & 6 deletions crates/bevy_asset/src/io/file_asset_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ impl FileAssetIo {
wasm32 / android targets"
);
#[cfg(feature = "filesystem_watcher")]
file_asset_io.watch_for_changes().unwrap();
file_asset_io
.watch_for_changes()
.expect("Could not watch for changes.");
}
file_asset_io
}
Expand All @@ -57,9 +59,9 @@ impl FileAssetIo {
.map(|path| {
path.parent()
.map(|exe_parent_path| exe_parent_path.to_owned())
.unwrap()
.expect("Path does not have a parent.")
})
.unwrap()
.expect("Acquiring the path to the current executable failed.")
}
}
}
Expand Down Expand Up @@ -92,8 +94,10 @@ impl AssetIo for FileAssetIo {
let root_path = self.root_path.to_owned();
Ok(Box::new(fs::read_dir(root_path.join(path))?.map(
move |entry| {
let path = entry.unwrap().path();
path.strip_prefix(&root_path).unwrap().to_owned()
let path = entry.expect("Could not read entry in folder.").path();
path.strip_prefix(&root_path)
.expect("Could not strip prefix from path.")
.to_owned()
},
)))
}
Expand Down Expand Up @@ -157,7 +161,9 @@ pub fn filesystem_watcher_system(asset_server: Res<AssetServer>) {
{
for path in paths.iter() {
if !changed.contains(path) {
let relative_path = path.strip_prefix(&asset_io.root_path).unwrap();
let relative_path = path
.strip_prefix(&asset_io.root_path)
.expect("Could not strip prefix from path.");
let _ = asset_server.load_untracked(relative_path.into(), true);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_asset/src/io/wasm_asset_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ impl AssetIo for WasmAssetIo {
fn load_path<'a>(&'a self, path: &'a Path) -> BoxedFuture<'a, Result<Vec<u8>, AssetIoError>> {
Box::pin(async move {
let path = self.root_path.join(path);
let window = web_sys::window().unwrap();
let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().unwrap()))
let window = web_sys::window().expect("Could not get window.");
let resp_value = JsFuture::from(window.fetch_with_str(path.to_str().expect("Could not convert path to string")))
.await
.unwrap();
let resp: Response = resp_value.dyn_into().unwrap();
Expand Down
12 changes: 9 additions & 3 deletions crates/bevy_asset/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ impl<'a> LoadContext<'a> {
asset_metas.push(AssetMeta {
dependencies: asset.dependencies.clone(),
label: label.clone(),
type_uuid: asset.value.as_ref().unwrap().type_uuid(),
type_uuid: asset
.value
.as_ref()
.expect("No value was found for BoxedLoadedAsset")
.type_uuid(),
});
}
asset_metas
Expand Down Expand Up @@ -182,7 +186,7 @@ impl<T: AssetDynamic> AssetLifecycle for AssetLifecycleChannel<T> {
id,
version,
}))
.unwrap()
.expect("Could not send an AssetLifecycleEvent::Create correctly.")
} else {
panic!(
"Failed to downcast asset to {}.",
Expand All @@ -192,7 +196,9 @@ impl<T: AssetDynamic> AssetLifecycle for AssetLifecycleChannel<T> {
}

fn free_asset(&self, id: HandleId) {
self.sender.send(AssetLifecycleEvent::Free(id)).unwrap();
self.sender
.send(AssetLifecycleEvent::Free(id))
.expect("Could not send an AssetLifecycleEvent::Free correctly.");
}
}

Expand Down