Port todos to async_std

This commit is contained in:
Héctor Ramón Jiménez 2019-12-14 20:48:32 +01:00
parent 5185d6a0f3
commit 430ab6e444

View File

@ -517,21 +517,23 @@ impl SavedState {
} }
async fn load() -> Result<SavedState, LoadError> { async fn load() -> Result<SavedState, LoadError> {
use std::io::Read; use async_std::prelude::*;
let mut contents = String::new(); let mut contents = String::new();
let mut file = std::fs::File::open(Self::path()) let mut file = async_std::fs::File::open(Self::path())
.await
.map_err(|_| LoadError::FileError)?; .map_err(|_| LoadError::FileError)?;
file.read_to_string(&mut contents) file.read_to_string(&mut contents)
.await
.map_err(|_| LoadError::FileError)?; .map_err(|_| LoadError::FileError)?;
serde_json::from_str(&contents).map_err(|_| LoadError::FormatError) serde_json::from_str(&contents).map_err(|_| LoadError::FormatError)
} }
async fn save(self) -> Result<(), SaveError> { async fn save(self) -> Result<(), SaveError> {
use std::io::Write; use async_std::prelude::*;
let json = serde_json::to_string_pretty(&self) let json = serde_json::to_string_pretty(&self)
.map_err(|_| SaveError::FormatError)?; .map_err(|_| SaveError::FormatError)?;
@ -539,20 +541,23 @@ impl SavedState {
let path = Self::path(); let path = Self::path();
if let Some(dir) = path.parent() { if let Some(dir) = path.parent() {
std::fs::create_dir_all(dir) async_std::fs::create_dir_all(dir)
.await
.map_err(|_| SaveError::DirectoryError)?; .map_err(|_| SaveError::DirectoryError)?;
} }
let mut file = {
std::fs::File::create(path).map_err(|_| SaveError::FileError)?; let mut file = async_std::fs::File::create(path)
.await
.map_err(|_| SaveError::FileError)?;
file.write_all(json.as_bytes()) file.write_all(json.as_bytes())
.await
.map_err(|_| SaveError::WriteError)?; .map_err(|_| SaveError::WriteError)?;
}
// This is a simple way to save at most once every couple seconds // This is a simple way to save at most once every couple seconds
// We will be able to get rid of it once we implement event async_std::task::sleep(std::time::Duration::from_secs(2)).await;
// subscriptions
std::thread::sleep(std::time::Duration::from_secs(2));
Ok(()) Ok(())
} }