mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-03-04 17:23:33 +01:00
minedmap/region_group: add async map functions
This commit is contained in:
parent
78fe1ec50e
commit
dcc9e6e794
1 changed files with 42 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
||||||
use std::iter;
|
use std::{future::Future, iter};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use futures_util::future::OptionFuture;
|
||||||
|
|
||||||
/// A generic array of 3x3 elements
|
/// A generic array of 3x3 elements
|
||||||
///
|
///
|
||||||
|
@ -70,6 +71,46 @@ impl<T> RegionGroup<T> {
|
||||||
Ok(RegionGroup { center, neighs })
|
Ok(RegionGroup { center, neighs })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub async fn async_map<U, F, Fut>(self, mut f: F) -> RegionGroup<U>
|
||||||
|
where
|
||||||
|
Fut: Future<Output = U>,
|
||||||
|
F: FnMut(T) -> Fut,
|
||||||
|
{
|
||||||
|
let center = f(self.center);
|
||||||
|
let neighs = futures_util::future::join_all(
|
||||||
|
self.neighs
|
||||||
|
.map(|entry| OptionFuture::from(entry.map(&mut f))),
|
||||||
|
);
|
||||||
|
let (center, neighs) = futures_util::join!(center, neighs);
|
||||||
|
RegionGroup {
|
||||||
|
center,
|
||||||
|
neighs: <[Option<_>; 9]>::try_from(neighs).ok().unwrap(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn async_try_map<U, F, Fut>(self, mut f: F) -> Result<RegionGroup<U>>
|
||||||
|
where
|
||||||
|
Fut: Future<Output = Result<U>>,
|
||||||
|
F: FnMut(T) -> Fut,
|
||||||
|
{
|
||||||
|
let center = f(self.center);
|
||||||
|
let neighs = futures_util::future::join_all(
|
||||||
|
self.neighs
|
||||||
|
.map(|entry| OptionFuture::from(entry.map(&mut f))),
|
||||||
|
);
|
||||||
|
let (center, neighs) = futures_util::join!(center, neighs);
|
||||||
|
let center = center?;
|
||||||
|
|
||||||
|
let neighs: Vec<_> = neighs
|
||||||
|
.into_iter()
|
||||||
|
.map(|entry| entry.and_then(Result::ok))
|
||||||
|
.collect();
|
||||||
|
let neighs = <[Option<_>; 9]>::try_from(neighs).ok().unwrap();
|
||||||
|
|
||||||
|
Ok(RegionGroup { center, neighs })
|
||||||
|
}
|
||||||
|
|
||||||
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
pub fn iter(&self) -> impl Iterator<Item = &T> {
|
||||||
iter::once(&self.center).chain(self.neighs.iter().filter_map(Option::as_ref))
|
iter::once(&self.center).chain(self.neighs.iter().filter_map(Option::as_ref))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue