summaryrefslogtreecommitdiffstats
path: root/examples/todos.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2019-12-16 21:38:56 +0100
committerLibravatar GitHub <noreply@github.com>2019-12-16 21:38:56 +0100
commit0f2e20f5e5b1f0658ab4e6cbe6fdda9ca97f2b36 (patch)
tree6b4c601bfa0ced1e003f597d7f485be7c108e12c /examples/todos.rs
parent3702b109977a249247a0f1be40e57bec2cbaa4e3 (diff)
parent430ab6e44432d044f8444575053d97651f0f7d20 (diff)
downloadiced-0f2e20f5e5b1f0658ab4e6cbe6fdda9ca97f2b36.tar.gz
iced-0f2e20f5e5b1f0658ab4e6cbe6fdda9ca97f2b36.tar.bz2
iced-0f2e20f5e5b1f0658ab4e6cbe6fdda9ca97f2b36.zip
Merge pull request #122 from hecrj/feature/event-subscriptions
Event subscriptions
Diffstat (limited to 'examples/todos.rs')
-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(())
}