From d327a21d2e77cd995b0e41fa45f1624b774d9e09 Mon Sep 17 00:00:00 2001 From: Lionel Sambuc Date: Mon, 20 Jan 2020 14:39:50 +0100 Subject: [PATCH] Remove .unwrap() calls, and add more information. Also re-order the generation of two files to possibly reduce memory consumption. --- src/main.rs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 56efa47..39a8de5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -56,19 +56,25 @@ fn store(name: &str, data: T) where T: Serialize, { - let to = format!("{}.objects.json", name); - let file_out = - File::create(&to).unwrap_or_else(|e| panic!("Unable to create file: {}: {}", to, e)); - let writer = BufWriter::new(&file_out); - - serde_json::to_writer(writer, &data).unwrap(); + // Serialize first the spaces, as this is much smaller than the data point. + // This matters in case the drop() call is not made at the time of the + // second definition of the variables, but only at the end fo the block. let to = format!("{}.spaces.json", name); let file_out = - File::create(&to).unwrap_or_else(|e| panic!("Unable to create file: {}: {}", to, e)); + File::create(&to).unwrap_or_else(|e| panic!("Unable to create file '{}': {}", to, e)); let writer = BufWriter::new(&file_out); - serde_json::to_writer(writer, &get_reference_space()).unwrap(); + serde_json::to_writer(writer, &get_reference_space()) + .unwrap_or_else(|e| panic!("Unable to serialize to '{}': {}", to, e)); + + let to = format!("{}.objects.json", name); + let file_out = + File::create(&to).unwrap_or_else(|e| panic!("Unable to create file '{}': {}", to, e)); + let writer = BufWriter::new(&file_out); + + serde_json::to_writer(writer, &data) + .unwrap_or_else(|e| panic!("Unable to serialize to '{}': {}", to, e)); } fn get_point(space_name: &str, rng: &mut ThreadRng, die: &Uniform) -> SpatialObject {