Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Accept references for set_contents" #61

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- Use `String` in `ClipboardProvider::set_contents` for trait to be *object-safe*

## 0.9.0

- Bump minimum supported Rust version to `1.65.0`
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn main() {
let mut ctx = ClipboardContext::new().unwrap();

let msg = "Hello, world!";
ctx.set_contents(msg).unwrap();
ctx.set_contents(msg.to_owned()).unwrap();

let content = ctx.get_contents().unwrap();

Expand All @@ -29,7 +29,7 @@ The `ClipboardProvider` trait has the following functions:

```rust
fn get_contents(&mut self) -> Result<String, Box<Error>>;
fn set_contents<T: AsRef<str>>(&mut self, T) -> Result<(), Box<Error>>;
fn set_contents(&mut self, String) -> Result<(), Box<Error>>;
```

`ClipboardContext` is a type alias for one of {`WindowsClipboardContext`, `OSXClipboardContext`, `X11ClipboardContext`, `NopClipboardContext`}, all of which implement `ClipboardProvider`. Which concrete type is chosen for `ClipboardContext` depends on the OS (via conditional compilation).
Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn main() {
let mut ctx = ClipboardContext::new().unwrap();

let msg = "Hello, world!";
ctx.set_contents(msg).unwrap();
ctx.set_contents(msg.to_owned()).unwrap();

let content = ctx.get_contents().unwrap();

Expand Down
2 changes: 1 addition & 1 deletion examples/primary_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {

let the_string = "Hello, world!";

ctx.set_contents(the_string).unwrap();
ctx.set_contents(the_string.to_owned()).unwrap();
}

#[cfg(not(target_os = "linux"))]
Expand Down
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ pub trait ClipboardProvider: Send {
/// Method to get the clipboard contents as a String
fn get_contents(&mut self) -> Result<String>;
/// Method to set the clipboard contents as a String
fn set_contents<T: AsRef<str>>(&mut self, _: T) -> Result<()>;
fn set_contents(&mut self, _: String) -> Result<()>;
}
2 changes: 1 addition & 1 deletion src/nop_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl ClipboardProvider for NopClipboardContext {
Ok("".to_string())
}

fn set_contents<T: AsRef<str>>(&mut self, _: T) -> Result<()> {
fn set_contents(&mut self, _: String) -> Result<()> {
println!(
"Attempting to set the contents of the clipboard, which hasn't yet been implemented \
on this platform."
Expand Down
4 changes: 2 additions & 2 deletions src/osx_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ impl ClipboardProvider for OSXClipboardContext {
})
}

fn set_contents<T: AsRef<str>>(&mut self, data: T) -> Result<()> {
let string_array = NSArray::from_vec(vec![NSString::from_str(data.as_ref())]);
fn set_contents(&mut self, data: String) -> Result<()> {
let string_array = NSArray::from_vec(vec![NSString::from_str(&data)]);
let _: usize = unsafe { msg_send![self.pasteboard, clearContents] };
let success: bool = unsafe { msg_send![self.pasteboard, writeObjects: string_array] };
if success {
Expand Down
8 changes: 4 additions & 4 deletions src/wayland_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ impl ClipboardProvider for Clipboard {
Ok(self.context.lock().unwrap().load()?)
}

fn set_contents<T: AsRef<str>>(&mut self, data: T) -> Result<()> {
self.context.lock().unwrap().store(data.as_ref());
fn set_contents(&mut self, data: String) -> Result<()> {
self.context.lock().unwrap().store(data);

Ok(())
}
Expand All @@ -56,8 +56,8 @@ impl ClipboardProvider for Primary {
Ok(self.context.lock().unwrap().load_primary()?)
}

fn set_contents<T: AsRef<str>>(&mut self, data: T) -> Result<()> {
self.context.lock().unwrap().store_primary(data.as_ref());
fn set_contents(&mut self, data: String) -> Result<()> {
self.context.lock().unwrap().store_primary(data);

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/windows_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl ClipboardProvider for WindowsClipboardContext {
Ok(get_clipboard_string()?)
}

fn set_contents<T: AsRef<str>>(&mut self, data: T) -> Result<()> {
Ok(set_clipboard_string(data.as_ref())?)
fn set_contents(&mut self, data: String) -> Result<()> {
Ok(set_clipboard_string(&data)?)
}
}
8 changes: 2 additions & 6 deletions src/x11_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ where
)?)?)
}

fn set_contents<T: AsRef<str>>(&mut self, data: T) -> Result<()> {
Ok(self.0.store(
S::atom(&self.0.setter.atoms),
self.0.setter.atoms.utf8_string,
data.as_ref(),
)?)
fn set_contents(&mut self, data: String) -> Result<()> {
Ok(self.0.store(S::atom(&self.0.setter.atoms), self.0.setter.atoms.utf8_string, data)?)
}
}
Loading