From 7fd3f07c0519db515fb9766ecc54ac797c69c025 Mon Sep 17 00:00:00 2001 From: ComplexSpaces Date: Fri, 22 Jul 2022 19:31:38 -0500 Subject: [PATCH] Accept optionally borrowed data instead of String for setting text --- examples/daemonize.rs | 6 +++--- examples/hello_world.rs | 4 ++-- src/lib.rs | 15 +++++++++------ src/platform/linux/mod.rs | 5 +++-- src/platform/linux/wayland.rs | 5 +++-- src/platform/linux/x11.rs | 9 ++++++--- src/platform/osx.rs | 3 ++- src/platform/windows.rs | 6 +++--- 8 files changed, 31 insertions(+), 22 deletions(-) diff --git a/examples/daemonize.rs b/examples/daemonize.rs index f0ca215..b657ff6 100644 --- a/examples/daemonize.rs +++ b/examples/daemonize.rs @@ -14,11 +14,11 @@ const DAEMONIZE_ARG: &str = "__internal_daemonize"; fn main() -> Result<(), Box> { #[cfg(target_os = "linux")] if env::args().nth(1).as_deref() == Some(DAEMONIZE_ARG) { - Clipboard::new()?.set().wait().text("Hello, world!".into())?; + Clipboard::new()?.set().wait().text("Hello, world!")?; return Ok(()); } - let _logger = SimpleLogger::new().init().unwrap(); + SimpleLogger::new().init().unwrap(); if cfg!(target_os = "linux") { process::Command::new(env::current_exe()?) @@ -29,7 +29,7 @@ fn main() -> Result<(), Box> { .current_dir("/") .spawn()?; } else { - Clipboard::new()?.set_text("Hello, world!".into())?; + Clipboard::new()?.set_text("Hello, world!")?; } Ok(()) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 0e50137..07f8c19 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -2,11 +2,11 @@ use arboard::Clipboard; use simple_logger::SimpleLogger; fn main() { - let _logger = SimpleLogger::new().init().unwrap(); + SimpleLogger::new().init().unwrap(); let mut clipboard = Clipboard::new().unwrap(); println!("Clipboard text was: {:?}", clipboard.get_text()); let the_string = "Hello, world!"; - clipboard.set_text(the_string.into()).unwrap(); + clipboard.set_text(the_string).unwrap(); println!("But now the clipboard text should be: \"{}\"", the_string); } diff --git a/src/lib.rs b/src/lib.rs index 5d33933..0ab0a0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,8 @@ and conditions of the chosen license apply to this file. #![crate_type = "lib"] mod common; +use std::borrow::Cow; + pub use common::Error; #[cfg(feature = "image-data")] pub use common::ImageData; @@ -54,7 +56,7 @@ impl Clipboard { } /// Places the text onto the clipboard. Any valid utf-8 string is accepted. - pub fn set_text(&mut self, text: String) -> Result<(), Error> { + pub fn set_text<'a, T: Into>>(&mut self, text: T) -> Result<(), Error> { self.set().text(text) } @@ -135,7 +137,8 @@ pub struct Set<'clipboard> { impl Set<'_> { /// Completes the "set" operation by placing text onto the clipboard. Any valid UTF-8 string /// is accepted. - pub fn text(self, text: String) -> Result<(), Error> { + pub fn text<'a, T: Into>>(self, text: T) -> Result<(), Error> { + let text = text.into(); self.platform.text(text) } @@ -177,7 +180,7 @@ fn all_tests() { { let mut ctx = Clipboard::new().unwrap(); let text = "some string"; - ctx.set_text(text.to_owned()).unwrap(); + ctx.set_text(text).unwrap(); assert_eq!(ctx.get_text().unwrap(), text); // We also need to check that the content persists after the drop; this is @@ -195,14 +198,14 @@ fn all_tests() { { let mut ctx = Clipboard::new().unwrap(); let text = "Some utf8: 🤓 ∑φ(n)<ε 🐔"; - ctx.set_text(text.to_owned()).unwrap(); + ctx.set_text(text).unwrap(); assert_eq!(ctx.get_text().unwrap(), text); } { let mut ctx = Clipboard::new().unwrap(); let text = "hello world"; - ctx.set_text(text.into()).unwrap(); + ctx.set_text(text).unwrap(); assert_eq!(ctx.get_text().unwrap(), text); ctx.clear_default().unwrap(); @@ -232,7 +235,7 @@ fn all_tests() { ctx.set_image(img_data.clone()).unwrap(); assert!(matches!(ctx.get_text(), Err(Error::ContentNotAvailable))); - ctx.set_text("clipboard test".into()).unwrap(); + ctx.set_text("clipboard test").unwrap(); assert!(matches!(ctx.get_image(), Err(Error::ContentNotAvailable))); // Test if we get the same image that we put onto the clibboard diff --git a/src/platform/linux/mod.rs b/src/platform/linux/mod.rs index 0dc5378..0181c14 100644 --- a/src/platform/linux/mod.rs +++ b/src/platform/linux/mod.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; #[cfg(feature = "image-data")] use std::{cell::RefCell, rc::Rc}; @@ -172,7 +173,7 @@ impl<'clipboard> Set<'clipboard> { Self { clipboard, wait: false, selection: LinuxClipboardKind::Clipboard } } - pub(crate) fn text(self, text: String) -> Result<(), Error> { + pub(crate) fn text(self, text: Cow<'_, str>) -> Result<(), Error> { match self.clipboard { Clipboard::X11(clipboard) => clipboard.set_text(text, self.selection, self.wait), #[cfg(feature = "wayland-data-control")] @@ -267,7 +268,7 @@ impl<'clipboard> Clear<'clipboard> { pub(crate) fn clear(self) -> Result<(), Error> { let mut set = Set::new(self.clipboard); set.selection = self.selection; - set.text(String::new()) + set.text(Cow::Borrowed("")) } } diff --git a/src/platform/linux/wayland.rs b/src/platform/linux/wayland.rs index 7cf915b..9648891 100644 --- a/src/platform/linux/wayland.rs +++ b/src/platform/linux/wayland.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::convert::TryInto; use std::io::Read; @@ -76,7 +77,7 @@ impl Clipboard { pub(crate) fn set_text( &self, - text: String, + text: Cow<'_, str>, selection: LinuxClipboardKind, wait: bool, ) -> Result<(), Error> { @@ -84,7 +85,7 @@ impl Clipboard { let mut opts = Options::new(); opts.foreground(wait); opts.clipboard(selection.try_into()?); - let source = Source::Bytes(text.as_bytes().into()); + let source = Source::Bytes(text.into_owned().into_bytes().into_boxed_slice()); opts.copy(source, MimeType::Text).map_err(|e| match e { CopyError::PrimarySelectionUnsupported => Error::ClipboardNotSupported, other => into_unknown(other), diff --git a/src/platform/linux/x11.rs b/src/platform/linux/x11.rs index 8bde64a..fd96c46 100644 --- a/src/platform/linux/x11.rs +++ b/src/platform/linux/x11.rs @@ -13,6 +13,7 @@ and conditions of the chosen license apply to this file. // https://freedesktop.org/wiki/ClipboardManager/ use std::{ + borrow::Cow, cell::RefCell, collections::{hash_map::Entry, HashMap}, sync::{ @@ -852,12 +853,14 @@ impl Clipboard { pub(crate) fn set_text( &self, - message: String, + message: Cow<'_, str>, selection: LinuxClipboardKind, wait: bool, ) -> Result<()> { - let data = - ClipboardData { bytes: message.into_bytes(), format: self.inner.atoms.UTF8_STRING }; + let data = ClipboardData { + bytes: message.into_owned().into_bytes(), + format: self.inner.atoms.UTF8_STRING, + }; self.inner.write(data, selection, wait) } diff --git a/src/platform/osx.rs b/src/platform/osx.rs index 4786015..d54bfa6 100644 --- a/src/platform/osx.rs +++ b/src/platform/osx.rs @@ -26,6 +26,7 @@ use objc::{ use objc_foundation::{INSArray, INSObject, INSString, NSArray, NSDictionary, NSObject, NSString}; use objc_id::{Id, Owned}; use once_cell::sync::Lazy; +use std::borrow::Cow; // Required to bring NSPasteboard into the path of the class-resolver #[link(name = "AppKit", kind = "framework")] @@ -254,7 +255,7 @@ impl<'clipboard> Set<'clipboard> { Self { clipboard } } - pub(crate) fn text(self, data: String) -> Result<(), Error> { + pub(crate) fn text(self, data: Cow<'_, str>) -> Result<(), Error> { self.clipboard.clear(); let string_array = NSArray::from_vec(vec![NSString::from_str(&data)]); diff --git a/src/platform/windows.rs b/src/platform/windows.rs index c3e45ef..70901b3 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -8,9 +8,9 @@ the Apache 2.0 or the MIT license at the licensee's choice. The terms and conditions of the chosen license apply to this file. */ -use std::marker::PhantomData; +use std::{borrow::Cow, marker::PhantomData}; #[cfg(feature = "image-data")] -use std::{borrow::Cow, convert::TryInto, mem::size_of}; +use std::{convert::TryInto, mem::size_of}; use clipboard_win::Clipboard as SystemClipboard; @@ -461,7 +461,7 @@ impl<'clipboard> Set<'clipboard> { Self { clipboard: PhantomData, exclude_from_cloud: false, exclude_from_history: false } } - pub(crate) fn text(self, data: String) -> Result<(), Error> { + pub(crate) fn text(self, data: Cow<'_, str>) -> Result<(), Error> { clipboard_win::raw::set_string(&data).map_err(|_| Error::Unknown { description: "Could not place the specified text to the clipboard".into(), })?;