summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-12-14 20:48:32 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-12-14 20:48:32 +0100
commit430ab6e44432d044f8444575053d97651f0f7d20 (patch)
treeee687ca590b6428aaf3a9c432d9d85810f96bcb3
parent5185d6a0f3e5993247923e857164842ef8be9105 (diff)
downloadiced-430ab6e44432d044f8444575053d97651f0f7d20.tar.gz
iced-430ab6e44432d044f8444575053d97651f0f7d20.tar.bz2
iced-430ab6e44432d044f8444575053d97651f0f7d20.zip
Port `todos` to `async_std`
-rw-r--r--examples/todos.rs27
1 files changed, 16 insertions, 11 deletions
diff --git a/examples/todos.rs b/examples/todos.rs
index 5f435fdc..42e88f65 100644
--- a/examples/todos.rs
+++ b/examples/todos.rs
@@ -517,21 +517,23 @@ impl SavedState {
}
async fn load() -> Result<SavedState, LoadError> {
- use std::io::Read;
+ use async_std::prelude::*;
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)?;
file.read_to_string(&mut contents)
+ .await
.map_err(|_| LoadError::FileError)?;
serde_json::from_str(&contents).map_err(|_| LoadError::FormatError)
}
async fn save(self) -> Result<(), SaveError> {
- use std::io::Write;
+ use async_std::prelude::*;
let json = serde_json::to_string_pretty(&self)
.map_err(|_| SaveError::FormatError)?;
@@ -539,20 +541,23 @@ impl SavedState {
let path = Self::path();
if let Some(dir) = path.parent() {
- std::fs::create_dir_all(dir)
+ async_std::fs::create_dir_all(dir)
+ .await
.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())
- .map_err(|_| SaveError::WriteError)?;
+ file.write_all(json.as_bytes())
+ .await
+ .map_err(|_| SaveError::WriteError)?;
+ }
// 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
- // subscriptions
- std::thread::sleep(std::time::Duration::from_secs(2));
+ async_std::task::sleep(std::time::Duration::from_secs(2)).await;
Ok(())
}