From fac69f8eb586a811797c4f7383b92ce3cd922543 Mon Sep 17 00:00:00 2001 From: naskya Date: Wed, 29 May 2024 19:20:42 +0900 Subject: [PATCH] chore (backend-rs): don't use unsafe unwrap() in examples --- packages/backend-rs/src/database/cache.rs | 36 +++++++++++++---------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/packages/backend-rs/src/database/cache.rs b/packages/backend-rs/src/database/cache.rs index 29b02f2335..0488e0b0aa 100644 --- a/packages/backend-rs/src/database/cache.rs +++ b/packages/backend-rs/src/database/cache.rs @@ -56,17 +56,20 @@ fn wildcard(category: Category) -> String { /// /// ``` /// # use backend_rs::database::cache; -/// # tokio_test::block_on(async { +/// # async fn f() -> Result<(), Box> { /// let key = "apple"; /// let data = "I want to cache this string".to_string(); /// /// // caches the data for 10 seconds -/// cache::set(key, &data, 10).await; +/// cache::set(key, &data, 10).await?; /// /// // get the cache -/// let cached_data = cache::get::(key).await.unwrap(); +/// let cached_data = cache::get::(key).await?; +/// +/// assert!(cached_data.is_some()); /// assert_eq!(data, cached_data.unwrap()); -/// # }) +/// # Ok(()) +/// # } /// ``` pub async fn set Deserialize<'a> + Serialize>( key: &str, @@ -97,21 +100,23 @@ pub async fn set Deserialize<'a> + Serialize>( /// /// ``` /// # use backend_rs::database::cache; -/// # tokio_test::block_on(async { +/// # async fn f() -> Result<(), Box> { /// let key = "banana"; /// let data = "I want to cache this string".to_string(); /// /// // set cache -/// cache::set(key, &data, 10).await.unwrap(); +/// cache::set(key, &data, 10).await?; /// /// // get cache -/// let cached_data = cache::get::(key).await.unwrap(); +/// let cached_data = cache::get::(key).await?; +/// assert!(cached_data.is_some()); /// assert_eq!(data, cached_data.unwrap()); /// /// // get nonexistent (or expired) cache -/// let no_cache = cache::get::("nonexistent").await.unwrap(); +/// let no_cache = cache::get::("nonexistent").await?; /// assert!(no_cache.is_none()); -/// # }) +/// # Ok(()) +/// # } /// ``` pub async fn get Deserialize<'a> + Serialize>(key: &str) -> Result, Error> { let serialized_value: Option> = redis_conn().await?.get(prefix_key(key)).await?; @@ -134,21 +139,22 @@ pub async fn get Deserialize<'a> + Serialize>(key: &str) -> Result Result<(), Box> { /// let key = "chocolate"; /// let value = "I want to cache this string".to_string(); /// /// // set cache -/// cache::set(key, &value, 10).await.unwrap(); +/// cache::set(key, &value, 10).await?; /// /// // delete the cache -/// cache::delete("foo").await.unwrap(); -/// cache::delete("nonexistent").await.unwrap(); // this is okay +/// cache::delete("foo").await?; +/// cache::delete("nonexistent").await?; // this is okay /// /// // the cache is gone -/// let cached_value = cache::get::("foo").await.unwrap(); +/// let cached_value = cache::get::("foo").await?; /// assert!(cached_value.is_none()); -/// # }) +/// # Ok(()) +/// # } /// ``` pub async fn delete(key: &str) -> Result<(), Error> { Ok(redis_conn().await?.del(prefix_key(key)).await?)