refactor: simplify trait bounds in the public interface

This commit is contained in:
Ashley Wulber 2024-02-29 16:38:12 -05:00
parent 50ccb2b60a
commit 23ac0ffc5b
No known key found for this signature in database
GPG key ID: 5216D4F46A90A820

View file

@ -54,10 +54,7 @@ impl Clipboard {
/// Load custom clipboard data. /// Load custom clipboard data.
/// ///
/// Load the requested type from a clipboard on the last observed seat. /// Load the requested type from a clipboard on the last observed seat.
pub fn load<T: AllowedMimeTypes + 'static>(&self) -> Result<T> pub fn load<T: AllowedMimeTypes + 'static>(&self) -> Result<T> {
where
<T as TryFrom<(Vec<u8>, MimeType)>>::Error: std::error::Error + Send + Sync,
{
self.load_inner(SelectionTarget::Clipboard) self.load_inner(SelectionTarget::Clipboard)
} }
@ -72,10 +69,7 @@ impl Clipboard {
/// ///
/// Load the requested type from a primary clipboard on the last observed /// Load the requested type from a primary clipboard on the last observed
/// seat. /// seat.
pub fn load_primary<T: AllowedMimeTypes + 'static>(&self) -> Result<T> pub fn load_primary<T: AllowedMimeTypes + 'static>(&self) -> Result<T> {
where
<T as TryFrom<(Vec<u8>, MimeType)>>::Error: std::error::Error + Send + Sync,
{
self.load_inner(SelectionTarget::Primary) self.load_inner(SelectionTarget::Primary)
} }
@ -115,16 +109,17 @@ impl Clipboard {
self.store_primary(Text(text.into())); self.store_primary(Text(text.into()));
} }
fn load_inner<T: AllowedMimeTypes + 'static>(&self, target: SelectionTarget) -> Result<T> fn load_inner<T: AllowedMimeTypes + 'static>(&self, target: SelectionTarget) -> Result<T> {
where
<T as TryFrom<(Vec<u8>, MimeType)>>::Error: std::error::Error + Send + Sync,
{
let _ = self.request_sender.send(worker::Command::Load(T::allowed().to_vec(), target)); let _ = self.request_sender.send(worker::Command::Load(T::allowed().to_vec(), target));
match self.request_receiver.recv() { match self.request_receiver.recv() {
Ok(res) => res.and_then(|(data, mime)| { Ok(res) => res.and_then(|(data, mime)| {
T::try_from((data, mime)) T::try_from((data, mime)).map_err(|_| {
.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)) std::io::Error::new(
std::io::ErrorKind::Other,
"Failed to load data of the requested type.",
)
})
}), }),
// The clipboard thread is dead, however we shouldn't crash downstream, // The clipboard thread is dead, however we shouldn't crash downstream,
// so propogating an error. // so propogating an error.