I'm working on this function:
use std::{sync::Arc, collections::HashSet};
use futures::{stream::BoxStream, StreamExt,TryStreamExt};
use brightness::{Brightness, BrightnessDevice};
fn get_devices<'a>(device_names: Arc<HashSet<String>>) -> BoxStream<'a, Result<BrightnessDevice, brightness::Error>> {
if device_names.is_empty() {
brightness::brightness_devices().boxed()
} else {
brightness::brightness_devices()
.try_filter(move |device| {
let device_names_clone = device_names.clone();
async move {
let devname: Result<String, brightness::Error> = device.device_name().await;
devname.is_ok_and(|devname| device_names_clone.contains(&devname)) // bool
}})
.boxed()
}
}
Basically I'm getting a stream of devices that have a brightness
value (like computer screens) and if the user specifies a couple of names, I want to filter that stream to only pass those devices that have that name. This name is requested from the device async. I know that that set of names needs to keep existing for the whole duration of the stream, so I wrapped it in an Arc, which seems to fix the first issue I had. It's my first time working with async Rust though so I might be missing something.
I keep getting this error with the lifetimes:
error: lifetime may not live long enough
--> src\funcs\mod.rs:55:21
|
53 | .try_filter(move |device| {
| ------- return type of closure `[async block@src\funcs\mod.rs:55:21: 58:18]` contains a lifetime `'2`
| |
| has type `&'1 brightness::BrightnessDevice`
54 | let device_names_clone = device_names.clone();
55 | / async move {
56 | | let devname: Result<String, brightness::Error> = device.device_name().await;
57 | | devname.is_ok_and(|devname| device_names_clone.contains(&devname)) // bool
58 | | }})
| |_________________^ returning this value requires that `'1` must outlive `'2`
For the brightness functions see this cargo. Or you can assume these functions exist:
brightness::r#async
pub fn brightness_devices() -> impl Stream<Item = Result<BrightnessDevice, Error>>
brightness::r#async::BrightnessDevice
fn device_name<'life0, 'async_trait>(&'life0 self) -> ::core::pin::Pin<Box<dyn ::core::future::Future<Output = Result<String, Error>> + ::core::marker::Send + 'async_trait>>
where
'life0: 'async_trait,
Self: 'async_trait