Overview of copypasta
masterrust-clipboard that adds support for the Wayland clipboard. It is a cross-platform Rust library designed for getting and setting the contents of the OS-level clipboard.repository·master·Indexed 43 days ago
https://github.com/alacritty/copypastaA cross-platform Rust library for getting and setting the contents of the OS-level clipboard. A fork of rust-clipboard, version 0.10.2 adds support for the Wayland clipboard alongside Windows, OSX, and X11. It provides a ClipboardProvider trait and a ClipboardContext type alias to abstract platform-specific backends.
rust-clipboard that adds support for the Wayland clipboard. It is a cross-platform Rust library designed for getting and setting the contents of the OS-level clipboard.The ClipboardContext type is a platform-specific type alias. Depending on the target OS, it resolves to one of the following types, all of which implement ClipboardProvider:
WindowsClipboardContextOSXClipboardContextX11ClipboardContextNopClipboardContext (No-op)This abstraction allows you to write cross-platform clipboard code without manually selecting the backend.
To use the library, create a ClipboardContext using ClipboardContext::new(). This context implements the ClipboardProvider trait, allowing you to call set_contents to write to the clipboard and get_contents to read from it. Note that ClipboardContext is a type alias that automatically selects the appropriate backend (Windows, OSX, X11, or Wayland) based on your operating system.
extern crate copypasta;
use copypasta::{ClipboardContext, ClipboardProvider};
fn main() {
let mut ctx = ClipboardContext::new().unwrap();
let msg = "Hello, world!";
ctx.set_contents(msg.to_owned()).unwrap();
let content = ctx.get_contents().unwrap();
println!("{}", content);
}The ClipboardProvider trait defines the core interface for interacting with the clipboard. Any type implementing this trait can be used to read or write string data.
trait ClipboardProvider {
fn get_contents(&mut self) -> Result<String, Box<Error>>;
fn set_contents(&mut self, String) -> Result<(), Box<Error>>;
}The ClipboardContext type alias provides a unified way to reference the underlying system clipboard context regardless of the target operating system. It automatically resolves to the correct implementation:
x11_clipboard::X11ClipboardContextwayland_clipboard module)windows_clipboard::WindowsClipboardContextosx_clipboard::OSXClipboardContextnop_clipboard::NopClipboardContext (No-op implementation)To integrate custom clipboard functionality into the copypasta ecosystem, implement the ClipboardProvider trait. This trait provides a platform-agnostic interface for retrieving and updating clipboard data as strings. All implementations must be Send to ensure thread safety.
use copypasta::ClipboardProvider;
use copypasta::Result;
struct MyClipboard;
impl ClipboardProvider for MyClipboard {
fn get_contents(&mut self) -> Result<String> {
// Implementation to retrieve clipboard content
Ok("content".to_string())
}
fn set_contents(&mut self, contents: String) -> Result<()> {
// Implementation to set clipboard content
Ok(())
}
}The copypasta crate provides the ClipboardProvider trait, which defines the interface for interacting with the system clipboard. To integrate a custom clipboard implementation, you must implement this trait. The crate also provides a platform-specific ClipboardContext type alias that resolves to the appropriate context for your current operating system (e.g., X11ClipboardContext on Linux/X11, WindowsClipboardContext on Windows, or OSXClipboardContext on macOS).
use copypasta::ClipboardProvider;
// Implementation would follow the trait definition provided by the crate
// struct MyClipboard;
// impl ClipboardProvider for MyClipboard { ... }The copypasta::Result<T> type is a specialized alias for std::result::Result<T, Box<dyn Error + Send + Sync + 'static>>. It is used across the crate to handle errors that are thread-safe and have a static lifetime, making it suitable for asynchronous or multi-threaded clipboard operations.
use copypasta::Result;
fn do_something() -> Result<()> {
// ...
Ok(())
}