refactor: Favor futures crate over futures-lite to reduce dependencies

This commit is contained in:
Michael Aaron Murphy 2022-03-27 16:54:26 +02:00 committed by Michael Murphy
parent b770b59e7b
commit 4153f9f060
18 changed files with 116 additions and 107 deletions

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright © 2021 System76
use futures_lite::{AsyncBufReadExt, AsyncWriteExt, StreamExt};
use futures::{AsyncBufReadExt, AsyncWriteExt, StreamExt};
use pop_launcher::*;
use regex::Regex;
use smol::{
@ -158,7 +158,7 @@ async fn qcalc(regex: &mut Regex, expression: &str, decimal_comma: bool) -> Opti
}
};
let mut reader = smol::io::BufReader::new(stdout).lines().skip(2);
let mut reader = futures::io::BufReader::new(stdout).lines().skip(2);
let mut output = String::new();
fn has_issue(line: &str) -> bool {

View file

@ -5,7 +5,7 @@ mod graphics;
use crate::*;
use freedesktop_desktop_entry::{default_paths, DesktopEntry, Iter as DesktopIter, PathSource};
use futures_lite::{AsyncWrite, StreamExt};
use futures::{AsyncWrite, StreamExt};
use pop_launcher::*;
use std::borrow::Cow;
use std::hash::{Hash, Hasher};

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright © 2021 System76
use futures_lite::prelude::*;
use futures::prelude::*;
use pop_launcher::*;
use smol::Unblock;
use std::{collections::BTreeMap, io, path::PathBuf};

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright © 2021 System76
use futures_lite::*;
use futures::*;
use pop_launcher::*;
use smol::process::{Child, ChildStdout, Command, Stdio};
use std::cell::Cell;
@ -107,7 +107,7 @@ pub async fn main() {
Ok::<(), flume::SendError<Event>>(())
};
let _ = future::zip(request_handler, search_handler).await;
let _ = futures::future::join(request_handler, search_handler).await;
}
/// Maintains state for search requests
@ -148,7 +148,7 @@ impl SearchContext {
tracing::debug!("searching for {}", search);
let (mut child, mut stdout) = match query(&search).await {
Ok((child, stdout)) => (child, futures_lite::io::BufReader::new(stdout).lines()),
Ok((child, stdout)) => (child, futures::io::BufReader::new(stdout).lines()),
Err(why) => {
tracing::error!("failed to spawn fdfind process: {}", why);
@ -179,7 +179,7 @@ impl SearchContext {
None
};
match interrupt.or(stdout.next()).await {
match crate::or(interrupt, stdout.next()).await {
Some(result) => match result {
Ok(line) => append = line,
Err(why) => {

View file

@ -12,9 +12,9 @@ pub mod scripts;
pub mod terminal;
pub mod web;
use futures_lite::{AsyncWrite, AsyncWriteExt};
use futures::{AsyncWrite, AsyncWriteExt};
use pop_launcher::PluginResponse;
use std::{borrow::Cow, ffi::OsStr, path::Path};
use std::{borrow::Cow, ffi::OsStr, future::Future, path::Path};
pub async fn send<W: AsyncWrite + Unpin>(tx: &mut W, response: PluginResponse) {
if let Ok(mut bytes) = serde_json::to_string(&response) {
@ -23,6 +23,17 @@ pub async fn send<W: AsyncWrite + Unpin>(tx: &mut W, response: PluginResponse) {
}
}
/// Run both futures and take the output of the first one to finish.
pub async fn or<T>(future1: impl Future<Output = T>, future2: impl Future<Output = T>) -> T {
futures::pin_mut!(future1);
futures::pin_mut!(future2);
futures::future::select(future1, future2)
.await
.factor_first()
.0
}
/// Fetch the mime for a given path
pub fn mime_from_path(path: &Path) -> Cow<'static, str> {
if path.is_dir() {

View file

@ -3,7 +3,7 @@
use crate::*;
use freedesktop_desktop_entry as fde;
use futures_lite::{AsyncWrite, AsyncWriteExt, StreamExt};
use futures::{AsyncWrite, AsyncWriteExt, StreamExt};
use pop_launcher::*;
use serde::{Deserialize, Serialize};
use std::{convert::TryFrom, fs, path::PathBuf, sync::Arc};

View file

@ -2,7 +2,7 @@
// Copyright © 2021 System76
use async_pidfd::AsyncPidFd;
use futures_lite::prelude::*;
use futures::prelude::*;
use pop_launcher::*;
use smol::Unblock;
use std::io;
@ -145,7 +145,7 @@ fn pactl_sinks() -> flume::Receiver<String> {
if let Ok(mut child) = child {
if let Some(stdout) = child.stdout.take() {
let mut lines = futures_lite::io::BufReader::new(stdout).lines();
let mut lines = futures::io::BufReader::new(stdout).lines();
while let Some(Ok(line)) = lines.next().await {
if let Some(stripped) = line.strip_prefix("Sink #") {
let _ = tx.send_async(stripped.trim().to_owned()).await;

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright © 2021 System76
use futures_lite::prelude::*;
use futures::prelude::*;
use gtk::prelude::*;
use pop_launcher::*;
use slab::Slab;

View file

@ -5,7 +5,7 @@ use crate::*;
use pop_launcher::*;
use flume::Sender;
use futures_lite::{AsyncBufReadExt, StreamExt};
use futures::{AsyncBufReadExt, StreamExt};
use smol::process::{Command, Stdio};
use std::collections::VecDeque;
use std::{
@ -98,7 +98,7 @@ impl App {
}
};
futures_lite::future::zip(script_sender, script_receiver).await;
futures::future::join(script_sender, script_receiver).await;
}
async fn search(&mut self, query: &str) {
@ -158,7 +158,7 @@ async fn load_from(path: &Path, paths: &mut VecDeque<PathBuf>, tx: Sender<Script
smol::spawn(async move {
let mut file = match smol::fs::File::open(&path).await {
Ok(file) => smol::io::BufReader::new(file).lines(),
Ok(file) => futures::io::BufReader::new(file).lines(),
Err(why) => {
tracing::error!("cannot open script at {}: {}", path.display(), why);
return;

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
// Copyright © 2021 System76
use futures_lite::prelude::*;
use futures::prelude::*;
use pop_launcher::*;
use smol::Unblock;
use std::{io, path::PathBuf};

View file

@ -6,11 +6,11 @@ use std::io;
use std::path::{Path, PathBuf};
use std::time::Duration;
use futures_lite::StreamExt;
use futures::io::AsyncReadExt;
use futures::StreamExt;
use isahc::config::{Configurable, RedirectPolicy};
use isahc::http::header::CONTENT_TYPE;
use isahc::{AsyncReadResponseExt, HttpClient};
use smol::io::AsyncReadExt;
use smol::Unblock;
use url::Url;
@ -159,9 +159,7 @@ impl App {
// Ensure we recreate the pop-launcher cache dir if it was removed at runtime
let cache_dir = favicon_path.parent().unwrap();
if !cache_dir.exists() {
smol::fs::create_dir_all(cache_dir)
.await
.expect("error creating cache directory");
std::fs::create_dir_all(cache_dir).expect("error creating cache directory");
}
let copy = smol::fs::write(&favicon_path, icon).await;