From 6ffbbe85bdcba95f7503d6bd0307ba1d9bfb9f16 Mon Sep 17 00:00:00 2001 From: juliancoffee Date: Tue, 8 Jun 2021 22:01:44 +0300 Subject: [PATCH] serialize AssetTweakWrapper, not just T asset_tweak::tweak_expect_or_create works by writing default data to file and then read it as asset. the problem is that it was writing T, and read AssetTweakWrapper which are different types. Tests didn't handle case when you will load data back so bug was hidden. --- common/assets/src/lib.rs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/common/assets/src/lib.rs b/common/assets/src/lib.rs index ed87eb2885..b3031053d6 100644 --- a/common/assets/src/lib.rs +++ b/common/assets/src/lib.rs @@ -269,7 +269,7 @@ pub mod asset_tweak { use ron::ser::{to_writer_pretty, PrettyConfig}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; - #[derive(Clone, Deserialize)] + #[derive(Clone, Deserialize, Serialize)] struct AssetTweakWrapper(T); impl Asset for AssetTweakWrapper @@ -357,9 +357,10 @@ pub mod asset_tweak { let f = fs::File::create(tweak_dir.join(&filename)).unwrap_or_else(|err| { panic!("failed to create file {:?}. Error: {:?}", &filename, err) }); - to_writer_pretty(f, &value, PrettyConfig::new()).unwrap_or_else(|err| { - panic!("failed to write to file {:?}. Error: {:?}", &filename, err) - }); + to_writer_pretty(f, &AssetTweakWrapper(value.clone()), PrettyConfig::new()) + .unwrap_or_else(|err| { + panic!("failed to write to file {:?}. Error: {:?}", &filename, err) + }); value } @@ -485,15 +486,27 @@ pub mod asset_tweak { fn test_tweaked_create() { let root = find_root().expect("failed to discover repository_root"); let tweak_dir = root.join("assets/tweak/"); + let test_path1 = tweak_dir.join("__test_int_create.ron"); let _file_guard1 = FileGuard::hold(&test_path1); let x = tweak_expect_or_create("__test_int_create", 5); assert_eq!(x, 5); assert!(test_path1.is_file()); + // Recheck it loads back correctly + let x = tweak_expect_or_create("__test_int_create", 5); + assert_eq!(x, 5); + + let test_path2 = tweak_dir.join("__test_tuple_create.ron"); + let _file_guard2 = FileGuard::hold(&test_path2); + let (x, y, z) = tweak_expect_or_create("__test_tuple_create", (5.0, 6.0, 7.0)); + assert_eq!((x, y, z), (5.0, 6.0, 7.0)); + // Recheck it loads back correctly + let (x, y, z) = tweak_expect_or_create("__test_tuple_create", (5.0, 6.0, 7.0)); + assert_eq!((x, y, z), (5.0, 6.0, 7.0)); // Test that file has stronger priority - let test_path2 = tweak_dir.join("__test_priority.ron"); - let (_file_guard2, mut file) = FileGuard::create(&test_path2); + let test_path3 = tweak_dir.join("__test_priority.ron"); + let (_file_guard3, mut file) = FileGuard::create(&test_path3); file.write_all(b"(10)") .expect("failed to write to the file"); let x = tweak_expect_or_create("__test_priority", 6);