forked from Orchid/orchid
Initial extension asynchronization efforts.
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
use std::future::{Future, ready};
|
||||
use std::num::NonZero;
|
||||
|
||||
use futures::FutureExt;
|
||||
use futures::future::{join, join_all};
|
||||
use futures::future::LocalBoxFuture;
|
||||
use hashbrown::HashMap;
|
||||
use orchid_base::interner::intern;
|
||||
use orchid_base::interner::Interner;
|
||||
use orchid_base::name::PathSlice;
|
||||
|
||||
use crate::api;
|
||||
use crate::system::SysCtx;
|
||||
|
||||
pub trait VirtFS: Send + Sync + 'static {
|
||||
fn load(&self, path: &PathSlice) -> api::OrcResult<api::Loaded>;
|
||||
fn load<'a>(
|
||||
&'a self,
|
||||
path: &'a PathSlice,
|
||||
ctx: SysCtx,
|
||||
) -> LocalBoxFuture<'a, api::OrcResult<api::Loaded>>;
|
||||
}
|
||||
|
||||
pub enum DeclFs {
|
||||
@@ -18,26 +22,25 @@ pub enum DeclFs {
|
||||
Mod(&'static [(&'static str, DeclFs)]),
|
||||
}
|
||||
impl DeclFs {
|
||||
pub fn to_api_rec(
|
||||
pub async fn to_api_rec(
|
||||
&self,
|
||||
vfses: &mut HashMap<api::VfsId, &'static dyn VirtFS>,
|
||||
) -> impl Future<Output = api::EagerVfs> + '_ {
|
||||
i: &Interner,
|
||||
) -> api::EagerVfs {
|
||||
match self {
|
||||
DeclFs::Lazy(fs) => {
|
||||
let vfsc: u16 = vfses.len().try_into().expect("too many vfses (more than u16::MAX)");
|
||||
let id = api::VfsId(NonZero::new(vfsc + 1).unwrap());
|
||||
vfses.insert(id, *fs);
|
||||
ready(api::EagerVfs::Lazy(id)).boxed_local()
|
||||
api::EagerVfs::Lazy(id)
|
||||
},
|
||||
DeclFs::Mod(children) => {
|
||||
let promises: Vec<_> =
|
||||
children.iter().map(|(k, v)| join(intern(*k), v.to_api_rec(vfses))).collect();
|
||||
async {
|
||||
api::EagerVfs::Eager(
|
||||
join_all(promises).await.into_iter().map(|(k, v)| (k.to_api(), v)).collect(),
|
||||
)
|
||||
let mut output = std::collections::HashMap::new();
|
||||
for (k, v) in children.iter() {
|
||||
output
|
||||
.insert(i.i::<String>(*k).await.to_api(), v.to_api_rec(vfses, i).boxed_local().await);
|
||||
}
|
||||
.boxed_local()
|
||||
api::EagerVfs::Eager(output)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user