Skip to content

Commit

Permalink
src: Use new Block trait on futures
Browse files Browse the repository at this point in the history
  • Loading branch information
eNV25 committed Oct 1, 2023
1 parent 5288ccd commit fc7f017
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
31 changes: 22 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
use std::collections::HashMap;
use std::{collections::HashMap, future::Future};

use async_io::block_on;
use zbus::{fdo, names::InterfaceName, zvariant::Value, SignalContext};

mod block {
pub trait Sealed {}
}

pub trait Block<T>: block::Sealed {
fn block(self) -> T;
fn block_io(self) -> T;
}

impl<F: Future<Output = T>, T> block::Sealed for F {}
impl<F: Future<Output = T>, T> Block<T> for F {
fn block(self) -> T {
futures_lite::future::block_on(self)
}
fn block_io(self) -> T {
async_io::block_on(self)
}
}

pub fn properties_changed(
ctxt: &SignalContext<'_>,
interface_name: InterfaceName<'_>,
changed_properties: &HashMap<&str, &Value<'_>>,
) -> zbus::Result<()> {
block_on(fdo::Properties::properties_changed(
ctxt,
interface_name,
changed_properties,
&[],
))
fdo::Properties::properties_changed(ctxt, interface_name, changed_properties, &[]).block_io()
}

pub fn seeked(ctxt: &SignalContext<'_>, position: i64) -> zbus::Result<()> {
block_on(crate::mpris2::Player::seeked(ctxt, position))
crate::mpris2::Player::seeked(ctxt, position).block_io()
}
16 changes: 9 additions & 7 deletions src/mpris2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
time::Duration,
};

use async_io::{block_on, Timer};
use async_io::Timer;
use async_process::Command;
use data_encoding::BASE64;
use futures_lite::FutureExt;
Expand All @@ -18,6 +18,8 @@ use zbus::{
SignalContext,
};

use crate::Block;

#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct Root(pub crate::Handle);
Expand Down Expand Up @@ -393,33 +395,33 @@ pub fn metadata(
fn thumbnail(ctx: impl Into<*mut crate::mpv_handle> + Copy) -> Option<String> {
let path = get!(ctx, "path").unwrap_or_default();
if path == get!(ctx, "stream-open-filename").unwrap_or_default() {
let cmd = Command::new("ffmpegthumbnailer")
Command::new("ffmpegthumbnailer")
.args(["-m", "-cjpeg", "-s0", "-o-", "-i"])
.arg(&path)
.kill_on_drop(true)
.output()
.or(async {
Timer::after(Duration::from_secs(1)).await;
Err(io::ErrorKind::TimedOut.into())
});
block_on(cmd)
})
.block_io()
.ok()
.map(|output| BASE64.encode(&output.stdout))
.map(|data| format!("data:image/jpeg;base64,{data}"))
} else {
["yt-dlp", "yt-dlp_x86", "youtube-dl"]
.into_iter()
.find_map(|cmd| {
let cmd = Command::new(cmd)
Command::new(cmd)
.args(["--no-warnings", "--get-thumbnail"])
.arg(&path)
.kill_on_drop(true)
.output()
.or(async {
Timer::after(Duration::from_secs(5)).await;
Err(io::ErrorKind::TimedOut.into())
});
block_on(cmd)
})
.block_io()
.ok()
.and_then(|output| String::from_utf8(output.stdout).map(truncate_newline).ok())
})
Expand Down

0 comments on commit fc7f017

Please sign in to comment.