From 95a24fa9870b7cdd9de3ab4d64ec2732a31add53 Mon Sep 17 00:00:00 2001 From: electria Date: Tue, 25 Aug 2026 09:37:00 -0700 Subject: [PATCH] refactor: use Cow for errors since some errors are just string literals; this lets us avoid allocating a new string --- src/main.rs | 35 +++++++++++++++++++---------------- src/utils.rs | 4 ++-- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7cd446b..d2e411c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,9 +33,9 @@ enum Message { FocusNext, FocusPrevious, - ImagePicked(Result), + ImagePicked(Result>), ImageDisplayReady(Result), - SavePathPicked(Result), + SavePathPicked(Result>), FileDropped(PathBuf), } @@ -46,7 +46,7 @@ struct State { image_display: Option, image_filter: widget::image::FilterMethod, - error: Option, + error: Option>, info: Option, infobar_shown: bool, @@ -58,7 +58,7 @@ impl State { if let Some(path) = env::args().nth(1) { match state.load_image(path) { - Err(e) => state.error = Some(e), + Err(e) => state.error = Some(e.into()), Ok(task) => return (state, task), } } @@ -108,22 +108,25 @@ impl State { } fn update(&mut self, message: Message) -> Task { match message { - Message::ImagePicked(result) => match result.and_then(|path| self.load_image(path)) { - Err(e) => self.error = Some(e), - Ok(task) => { - self.error = None; - return task; + Message::ImagePicked(result) => { + match result.and_then(|path| self.load_image(path).map_err(Cow::from)) { + Err(e) => self.error = Some(e.into()), + Ok(task) => { + self.error = None; + return task; + } } - }, + } Message::ImageDisplayReady(result) => { self.image_display = Some(result.unwrap()); } Message::SavePathPicked(result) => { self.error = result + .map_err(Cow::from) .and_then(|path| { self.image.as_ref().map_or_else( || Err("no image to save".into()), - |image| image.save(path).map_err(|e| e.to_string()), + |image| image.save(path).map_err(|e| e.to_string().into()), ) }) .err(); @@ -181,11 +184,11 @@ impl State { Ok(mut clipboard) => { self.error = clipboard .set_image(utils::arboard_from_rgbaimage(image)) - .map_err(|e| e.to_string()) + .map_err(|e| e.to_string().into()) .err(); self.clipboard = Some(clipboard); } - Err(e) => self.error = Some(e.to_string()), + Err(e) => self.error = Some(e.to_string().into()), } } else { self.error = Some("no image to yank".into()); @@ -199,7 +202,7 @@ impl State { Ok(mut clipboard) => { let result = clipboard.get_image().map(utils::rgbaimage_from_arboard); - self.error = result.as_ref().map_err(|e| e.to_string()).err(); + self.error = result.as_ref().map_err(|e| e.to_string().into()).err(); self.clipboard = Some(clipboard); if let Ok(image) = result { @@ -211,7 +214,7 @@ impl State { return self.allocate_image(); } } - Err(e) => self.error = Some(e.to_string()), + Err(e) => self.error = Some(e.to_string().into()), } } @@ -220,7 +223,7 @@ impl State { self.error = None; return task; } - Err(e) => self.error = Some(e), + Err(e) => self.error = Some(e.into()), }, } diff --git a/src/utils.rs b/src/utils.rs index f1684f7..8c1d427 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -34,7 +34,7 @@ fn load_image_impl(path: impl AsRef) -> ImageResult { Ok(decoded_image.into_rgba8()) } -pub async fn pick_image() -> Result { +pub async fn pick_image() -> Result> { let Some(filehandle) = AsyncFileDialog::new() .add_filter( "image", @@ -52,7 +52,7 @@ pub async fn pick_image() -> Result { Ok(filehandle.path().to_owned()) } -pub async fn pick_save_path() -> Result { +pub async fn pick_save_path() -> Result> { let Some(filehandle) = AsyncFileDialog::new().save_file().await else { return Err("no path to save provided".into()); };