use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use std::time::Instant;
use crate::directory::DirectoryStreamState;
use crate::directory::ListOptions;
use crate::directory::ListScope;
use crate::directory::ListStreamPolicy;
use crate::directory::internal::ListNextCancellationGuard;
use crate::error::FsResult;
use crate::metadata::DirEntry;
use crate::metadata::FileSystemLimits;
use crate::path::PathConstraints;
use crate::spi::AsyncDirectoryStreamSession;
use crate::spi::SpiFuture;
pub struct AsyncDirectoryStream {
session: Box<dyn AsyncDirectoryStreamSession>,
policy: ListStreamPolicy,
}
impl AsyncDirectoryStream {
#[inline]
pub(crate) fn new(
scope: ListScope,
session: Box<dyn AsyncDirectoryStreamSession>,
options: ListOptions,
provider: &str,
path_semantics: crate::path::PathSemantics,
limits: FileSystemLimits,
constraints: PathConstraints,
) -> FsResult<Self> {
let policy = ListStreamPolicy::new(
scope,
options,
provider,
path_semantics,
limits,
constraints,
Instant::now(),
)?;
Ok(Self { session, policy })
}
#[inline]
#[must_use = "inspect the stream lifecycle state"]
pub const fn state(&self) -> DirectoryStreamState {
self.policy.state()
}
pub fn next_entry_async(&mut self) -> SpiFuture<'_, FsResult<Option<DirEntry>>> {
Box::pin(async move {
self.policy.before_next(Instant::now())?;
let Self { session, policy } = self;
let guard = ListNextCancellationGuard::new(policy);
let result = session.next_entry_async().await;
guard.finish(result, Instant::now())
})
}
}
impl Debug for AsyncDirectoryStream {
#[inline]
fn fmt(&self, formatter: &mut Formatter<'_>) -> FmtResult {
formatter.debug_struct("AsyncDirectoryStream").finish_non_exhaustive()
}
}