Skip to content

Commit

Permalink
Revert "Accept references for set_contents"
Browse files Browse the repository at this point in the history
When using generic in a trait the trait becomes not object-safe forcing
generics or static dispatch in a such code. For a crate like copypasta
that's something we don't really want to, since it's usually
self-contained and has different clipboard providers.

This reverts commit 10f1137.
  • Loading branch information
kchibisov committed Oct 12, 2023
1 parent 0a7e3fb commit 0bae16e
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 21 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ 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`
- Change `ClipboardProvider::set_contents` parameter type to `AsRef<str>`
- Prefer file's path over text on macOS
- Bump minimum supported Rust version to `1.61.0`

## 0.8.2

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)?)
}
}

0 comments on commit 0bae16e

Please sign in to comment.