From ff358723da641af9f4ae1f742eb1b382f4630220 Mon Sep 17 00:00:00 2001 From: Vika Date: Thu, 22 Jun 2023 20:25:28 +0300 Subject: StorageError: use std::borrow::Cow for msg field This allows avoiding an unnecessary allocation whenever the error message is static. --- kittybox-rs/src/database/file/mod.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'kittybox-rs/src/database/file/mod.rs') diff --git a/kittybox-rs/src/database/file/mod.rs b/kittybox-rs/src/database/file/mod.rs index 9319b67..531f7b1 100644 --- a/kittybox-rs/src/database/file/mod.rs +++ b/kittybox-rs/src/database/file/mod.rs @@ -3,6 +3,7 @@ use crate::database::{filter_post, ErrorKind, Result, settings, Storage, Storage use async_trait::async_trait; use futures::{stream, StreamExt, TryStreamExt}; use serde_json::json; +use std::borrow::Cow; use std::collections::HashMap; use std::io::ErrorKind as IOErrorKind; use std::path::{Path, PathBuf}; @@ -19,7 +20,7 @@ impl From for StorageError { IOErrorKind::AlreadyExists => ErrorKind::Conflict, _ => ErrorKind::Backend, }, - "file I/O error", + Cow::Owned(format!("file I/O error: {}", &source)), Box::new(source), ) } @@ -29,7 +30,7 @@ impl From for StorageError { fn from(source: tokio::time::error::Elapsed) -> Self { Self::with_source( ErrorKind::Backend, - "timeout on I/O operation", + Cow::Borrowed("timeout on I/O operation"), Box::new(source), ) } @@ -135,7 +136,7 @@ fn modify_post(post: &serde_json::Value, update: &serde_json::Value) -> Result Result Result { return Err(StorageError::with_source( ErrorKind::Other, - "Feed assembly error", + Cow::Owned(format!("Feed assembly error: {}", &err)), Box::new(err), )); } @@ -560,7 +561,7 @@ impl Storage for FileStorage { hydrate_author(&mut feed, user, self).await; Ok(Some(feed)) } else { - Err(StorageError::new( + Err(StorageError::from_static( ErrorKind::PermissionDenied, "specified user cannot access this post", )) @@ -599,7 +600,7 @@ impl Storage for FileStorage { let settings: HashMap<&str, serde_json::Value> = serde_json::from_str(&content)?; match settings.get(S::ID) { Some(value) => Ok(serde_json::from_value::(value.clone())?), - None => Err(StorageError::new(ErrorKind::Backend, "Setting not set")) + None => Err(StorageError::from_static(ErrorKind::Backend, "Setting not set")) } } -- cgit 1.4.1