par_dfs/async/
mod.rs

1pub mod bfs;
2pub mod dfs;
3
4pub use bfs::Bfs;
5pub use dfs::Dfs;
6
7use async_trait::async_trait;
8use futures::stream::{FuturesOrdered, Stream};
9use futures::Future;
10use std::hash::Hash;
11use std::pin::Pin;
12use std::sync::Arc;
13
14type Stack<N, E> = Vec<(usize, NodeStream<N, E>)>;
15
16type NewNodesFut<N, E> =
17    Pin<Box<dyn Future<Output = (usize, Result<NodeStream<N, E>, E>)> + Unpin + Send + 'static>>;
18
19type StreamQueue<N, E> = FuturesOrdered<NewNodesFut<N, E>>;
20
21/// A pinned [`Stream`] of [`Node`]s
22///
23/// [`Stream`]: trait@futures::stream::Stream
24/// [`Node`]: trait@crate::async::Node
25pub type NodeStream<N, E> = Pin<Box<dyn Stream<Item = Result<N, E>> + Unpin + Send>>;
26
27#[async_trait]
28/// A node which produces a [`Stream`] of children [`Node`]s for a given depth.
29///
30/// [`Stream`]: trait@futures::stream::Stream
31/// [`Node`]: trait@crate::async::Node
32pub trait Node
33where
34    Self: Sized + Hash + Eq + std::fmt::Debug,
35{
36    /// The type of the error when creating the stream fails.
37    type Error: std::fmt::Debug;
38
39    /// Returns a [`NodeStream`] of its children.
40    ///
41    /// # Errors
42    ///
43    /// Should return [`Self::Error`] if the stream can not be created.
44    ///
45    /// [`NodeStream`]: type@crate::async::NodeStream
46    /// [`Self::Error`]: type@crate::async::Node::Error
47    async fn children(
48        self: Arc<Self>,
49        depth: usize,
50    ) -> Result<NodeStream<Self, Self::Error>, Self::Error>;
51}