Skip to content

Commit

Permalink
Use HasDisplayHandle instead of deprecated HasRawDisplayHandle
Browse files Browse the repository at this point in the history
Currently `connect` is unsafe because it doesn't take ownership of the
display or have a lifetime bound.
  • Loading branch information
ids1024 committed Jan 17, 2024
1 parent bf49c91 commit 869737d
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ keywords = ["clipboard", "window", "ui", "gui", "raw-window-handle"]
categories = ["gui"]

[dependencies]
raw-window-handle = "0.6"
raw-window-handle = { version = "0.6", features = ["std"] }
thiserror = "1.0"

[target.'cfg(windows)'.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/big_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() {
.unwrap();

let mut clipboard =
Clipboard::connect(&window).expect("Connect to clipboard");
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");

clipboard.write(data.clone()).unwrap();

Expand Down
3 changes: 2 additions & 1 deletion examples/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ fn main() {
.build(&event_loop)
.unwrap();

let clipboard = Clipboard::connect(&window).expect("Connect to clipboard");
let clipboard =
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");

event_loop
.run(move |event, elwt| match event {
Expand Down
2 changes: 1 addition & 1 deletion examples/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
.unwrap();

let mut clipboard =
Clipboard::connect(&window).expect("Connect to clipboard");
unsafe { Clipboard::connect(&window) }.expect("Connect to clipboard");

clipboard
.write(String::from("Hello, world!"))
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ mod platform;
#[path = "platform/dummy.rs"]
mod platform;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
use std::error::Error;

pub struct Clipboard {
raw: Box<dyn ClipboardProvider>,
}

impl Clipboard {
pub fn connect<W: HasRawDisplayHandle>(
/// Safety: the display handle must be valid for the lifetime of `Clipboard`
pub unsafe fn connect<W: HasDisplayHandle>(
window: &W,
) -> Result<Self, Box<dyn Error>> {
let raw = platform::connect(window)?;
Expand Down
4 changes: 2 additions & 2 deletions src/platform/android.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::ClipboardProvider;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
use std::error::Error;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
Ok(Box::new(Clipboard::new()?))
Expand Down
4 changes: 2 additions & 2 deletions src/platform/dummy.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::ClipboardProvider;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;

struct Dummy;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn std::error::Error>> {
Ok(Box::new(Dummy))
Expand Down
4 changes: 2 additions & 2 deletions src/platform/ios.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::ClipboardProvider;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
use std::error::Error;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
Ok(Box::new(Clipboard::new()?))
Expand Down
12 changes: 6 additions & 6 deletions src/platform/linux.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::ClipboardProvider;

use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle};
use raw_window_handle::{HasDisplayHandle, RawDisplayHandle};
use std::error::Error;

pub use clipboard_wayland as wayland;
pub use clipboard_x11 as x11;

pub fn connect<W: HasRawDisplayHandle>(
pub unsafe fn connect<W: HasDisplayHandle>(
window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
let clipboard = match window.raw_display_handle() {
Ok(RawDisplayHandle::Wayland(handle)) => Box::new(unsafe {
wayland::Clipboard::connect(handle.display.as_ptr())
}) as _,
let clipboard = match window.display_handle()?.as_raw() {
RawDisplayHandle::Wayland(handle) => {
Box::new(wayland::Clipboard::connect(handle.display.as_ptr())) as _
}
_ => Box::new(x11::Clipboard::connect()?) as _,
};

Expand Down
4 changes: 2 additions & 2 deletions src/platform/macos.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::ClipboardProvider;

use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;
use std::error::Error;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
Ok(Box::new(clipboard_macos::Clipboard::new()?))
Expand Down
4 changes: 2 additions & 2 deletions src/platform/windows.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::ClipboardProvider;

use clipboard_win::{get_clipboard_string, set_clipboard_string};
use raw_window_handle::HasRawDisplayHandle;
use raw_window_handle::HasDisplayHandle;

use std::error::Error;

pub fn connect<W: HasRawDisplayHandle>(
pub fn connect<W: HasDisplayHandle>(
_window: &W,
) -> Result<Box<dyn ClipboardProvider>, Box<dyn Error>> {
Ok(Box::new(Clipboard))
Expand Down

0 comments on commit 869737d

Please sign in to comment.