From f39927559a42187b9d0c1d25e3c07ea7ea95d395 Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 27 Sep 2021 15:36:22 +0300 Subject: Finally figured out how to read without a BufReader BufReader was really unneccesary here, since I was batch-reading all of this in one go. --- src/database/file/mod.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/database/file/mod.rs b/src/database/file/mod.rs index a059d22..9e33f0b 100644 --- a/src/database/file/mod.rs +++ b/src/database/file/mod.rs @@ -197,21 +197,13 @@ impl Storage for FileStorage { let lock = get_lockable_file(f).await; let guard = lock.read()?; - // HOW DOES THIS TYPECHECK?!!!!!!!! - // Read::read(&mut self) requires a mutable reference - // yet Read is implemented for &File - // We can't get a &mut File from &File, can we? - // And we need a &mut File to use Read::read_to_string() - // Yet if we pass it to a BufReader it works?!! - // - // I hate magic - // - // TODO find a way to get rid of BufReader here let mut content = String::new(); - let mut reader = BufReader::new(&*guard); - reader.read_to_string(&mut content).await?; - drop(reader); - drop(guard); + // Apparently this typechecks. Somehow. + // I can take &mut for a &File because File is not a real type + // The operating system guards it using something + // that looks like a mutex to Rust's runtime, allowing me + // to grab a mutable reference from an immutable reference + (&mut &*guard).read_to_string(&mut content).await?; Ok(Some(serde_json::from_str(&content)?)) } Err(err) => { -- cgit 1.4.1