use std::panic;
use crate::{OrphanedSubscriberError, Subscriber};
#[allow(dead_code, reason = "Unused by default features.")]
pub(crate) async fn map_changed<S, T>(
subscriber: &mut Subscriber<S>,
mut map_fn: impl FnMut(&S) -> T,
) -> Result<T, OrphanedSubscriberError> {
let next_changed = subscriber.read_changed().await?;
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| map_fn(&next_changed)));
match result {
Ok(next_item) => Ok(next_item),
Err(panicked) => {
drop(next_changed);
panic::resume_unwind(panicked);
}
}
}
#[allow(dead_code, reason = "Unused by default features.")]
pub(crate) async fn filter_map_changed<S, T>(
subscriber: &mut Subscriber<S>,
mut filter_map_fn: impl FnMut(&S) -> Option<T>,
) -> Result<T, OrphanedSubscriberError> {
loop {
let next_changed = subscriber.read_changed().await?;
let result = panic::catch_unwind(panic::AssertUnwindSafe(|| filter_map_fn(&next_changed)));
match result {
Ok(Some(next_item)) => {
return Ok(next_item);
}
Ok(None) => {
}
Err(panicked) => {
drop(next_changed);
panic::resume_unwind(panicked);
}
}
}
}