diff --git a/Cargo.toml b/Cargo.toml index 67d8493..71fa146 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jsondb" -version = "0.2.1" +version = "0.3.0" edition = "2021" authors = ["xenofem "] license = "MIT" diff --git a/src/lib.rs b/src/lib.rs index d226105..4ddc9af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,7 +24,6 @@ use std::{ cmp::Ordering, ffi::OsString, fmt::Debug, - future::Future, io::ErrorKind, ops::Deref, path::{Path, PathBuf}, @@ -134,7 +133,7 @@ impl JsonDb { /// initializing it with the schema's default value if it does not /// exist. pub async fn load(path: PathBuf) -> Result, Error> { - Self::load_or_else(path, || std::future::ready(Ok(T::default()))).await + Self::load_or_else(path, T::default).await } } @@ -164,16 +163,15 @@ impl JsonDb { /// initializing it with the provided default value if it does not /// exist. pub async fn load_or(path: PathBuf, default: T) -> Result, Error> { - Self::load_or_else(path, || std::future::ready(Ok(default))).await + Self::load_or_else(path, || default).await } /// Load a [`JsonDb`] from a given file, creating it and /// initializing it with the provided function if it does not /// exist. - pub async fn load_or_else(path: PathBuf, default: F) -> Result, Error> + pub async fn load_or_else(path: PathBuf, default: F) -> Result, Error> where - F: FnOnce() -> Fut, - Fut: Future>, + F: FnOnce() -> T, { let open_result = File::open(&path).await; let data = match open_result { @@ -184,7 +182,7 @@ impl JsonDb { } Err(e) => { if let ErrorKind::NotFound = e.kind() { - default().await? + default() } else { return Err(e.into()); }