vfs/directory/immutable/
connection.rsuse crate::directory::connection::{BaseConnection, ConnectionState};
use crate::directory::entry_container::Directory;
use crate::execution_scope::ExecutionScope;
use crate::node::OpenNode;
use crate::{ObjectRequestRef, ProtocolsExt};
use fidl_fuchsia_io as fio;
use fio::DirectoryRequest;
use futures::TryStreamExt;
use std::future::Future;
use std::pin::pin;
use std::sync::Arc;
use zx_status::Status;
pub struct ImmutableConnection<DirectoryType: Directory> {
base: BaseConnection<DirectoryType>,
}
impl<DirectoryType: Directory> ImmutableConnection<DirectoryType> {
async fn handle_requests<RS>(mut self, mut requests: RS)
where
RS: futures::stream::Stream<Item = Result<DirectoryRequest, fidl::Error>>,
{
let mut requests = pin!(requests);
while let Ok(Some(request)) = requests.try_next().await {
let _guard = self.base.scope.active_guard();
if !matches!(self.base.handle_request(request).await, Ok(ConnectionState::Alive)) {
break;
}
}
}
pub fn create(
scope: ExecutionScope,
directory: Arc<DirectoryType>,
protocols: impl ProtocolsExt,
object_request: ObjectRequestRef<'_>,
) -> Result<impl Future<Output = ()>, Status> {
Self::create_transform_stream(
scope,
directory,
protocols,
object_request,
std::convert::identity,
)
}
pub fn create_transform_stream<Transform, RS>(
scope: ExecutionScope,
directory: Arc<DirectoryType>,
protocols: impl ProtocolsExt,
object_request: ObjectRequestRef<'_>,
transform: Transform,
) -> Result<impl Future<Output = ()>, Status>
where
Transform: FnOnce(fio::DirectoryRequestStream) -> RS,
RS: futures::stream::Stream<Item = Result<DirectoryRequest, fidl::Error>>,
{
let directory = OpenNode::new(directory);
let connection = ImmutableConnection {
base: BaseConnection::new(scope, directory, protocols.to_directory_options()?),
};
let object_request = object_request.take();
Ok(async move {
if let Ok(requests) = object_request.into_request_stream(&connection.base).await {
connection.handle_requests(transform(requests)).await;
}
})
}
}