From 03cb426285bc9f492d8494f0909249b9754f3b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 11 Sep 2022 23:13:20 +0200 Subject: [PATCH 1/5] write file in a task --- examples/scene/scene.rs | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/examples/scene/scene.rs b/examples/scene/scene.rs index 9cda9c1c9b29f..3c40e3b944a7c 100644 --- a/examples/scene/scene.rs +++ b/examples/scene/scene.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io::Write; -use bevy::{prelude::*, utils::Duration}; +use bevy::{prelude::*, tasks::IoTaskPool, utils::Duration}; fn main() { App::new() @@ -104,17 +104,24 @@ fn save_scene_system(world: &mut World) { let scene = DynamicScene::from_world(&scene_world, type_registry); // Scenes can be serialized like this: - info!("{}", scene.serialize_ron(type_registry).unwrap()); - - // Write the scene RON data to file (leveraging From for ron::error::Error) - File::create(format!("assets/{}", NEW_SCENE_FILE_PATH)) - .map_err(|err| err.into()) - .and_then(|mut file| { - scene - .serialize_ron(type_registry) - .and_then(|data| file.write(data.as_bytes()).map_err(|err| err.into())) + let serialized_scene = scene.serialize_ron(type_registry).unwrap(); + + // Showing the scene in the console + info!("{}", serialized_scene); + + // Writing the scene to a new file. Using a task to avoid calling the filesystem APIs in a system + // as they are blocking + IoTaskPool::get() + .spawn(async move { + // Write the scene RON data to file (leveraging From for ron::error::Error) + File::create(format!("assets/{}", NEW_SCENE_FILE_PATH)) + .and_then(|mut file| { + file.write(serialized_scene.as_bytes()) + .map_err(|err| err.into()) + }) + .expect("Error while writing scene to file"); }) - .expect("Error while writing scene to file"); + .detach(); } // This is only necessary for the info message in the UI. See examples/ui/text.rs for a standalone From 98f104ec9977d9639e6cc1dfea610758dd6c6d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 11 Sep 2022 23:15:25 +0200 Subject: [PATCH 2/5] comment not true anymore --- examples/scene/scene.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/scene/scene.rs b/examples/scene/scene.rs index 3c40e3b944a7c..368404c1a9df8 100644 --- a/examples/scene/scene.rs +++ b/examples/scene/scene.rs @@ -113,7 +113,7 @@ fn save_scene_system(world: &mut World) { // as they are blocking IoTaskPool::get() .spawn(async move { - // Write the scene RON data to file (leveraging From for ron::error::Error) + // Write the scene RON data to file File::create(format!("assets/{}", NEW_SCENE_FILE_PATH)) .and_then(|mut file| { file.write(serialized_scene.as_bytes()) From cf6b9bf2ae287bbbdff3809f578f011df5a01720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 11 Sep 2022 23:20:26 +0200 Subject: [PATCH 3/5] this map_err is not needed either --- examples/scene/scene.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/examples/scene/scene.rs b/examples/scene/scene.rs index 368404c1a9df8..7e38e81bab954 100644 --- a/examples/scene/scene.rs +++ b/examples/scene/scene.rs @@ -115,10 +115,7 @@ fn save_scene_system(world: &mut World) { .spawn(async move { // Write the scene RON data to file File::create(format!("assets/{}", NEW_SCENE_FILE_PATH)) - .and_then(|mut file| { - file.write(serialized_scene.as_bytes()) - .map_err(|err| err.into()) - }) + .and_then(|mut file| file.write(serialized_scene.as_bytes())) .expect("Error while writing scene to file"); }) .detach(); From a7b05c5784f21d44567678926b0bd2222429d47a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 11 Sep 2022 23:33:30 +0200 Subject: [PATCH 4/5] do not write file in wasm --- examples/scene/scene.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/scene/scene.rs b/examples/scene/scene.rs index 7e38e81bab954..3c59b3bac56dd 100644 --- a/examples/scene/scene.rs +++ b/examples/scene/scene.rs @@ -111,6 +111,8 @@ fn save_scene_system(world: &mut World) { // Writing the scene to a new file. Using a task to avoid calling the filesystem APIs in a system // as they are blocking + // This can't work in WASM as there is not filesystem access + #[cfg(not(target_arch = "wasm32"))] IoTaskPool::get() .spawn(async move { // Write the scene RON data to file From 1ed1ed66b3949d55f09b20e1c218d33f40b842f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sun, 11 Sep 2022 23:43:54 +0200 Subject: [PATCH 5/5] english Co-authored-by: ira --- examples/scene/scene.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/scene/scene.rs b/examples/scene/scene.rs index 3c59b3bac56dd..ffbb93fb5873c 100644 --- a/examples/scene/scene.rs +++ b/examples/scene/scene.rs @@ -111,7 +111,7 @@ fn save_scene_system(world: &mut World) { // Writing the scene to a new file. Using a task to avoid calling the filesystem APIs in a system // as they are blocking - // This can't work in WASM as there is not filesystem access + // This can't work in WASM as there is no filesystem access #[cfg(not(target_arch = "wasm32"))] IoTaskPool::get() .spawn(async move {