use crate::all::*;
use futures::Future;
pub fn spawn<I, E, Fun, Fut>(function: Fun) -> (Child<E, I>, Address<I>)
where
Fun: FnOnce(I) -> Fut + Send + 'static,
Fut: Future<Output = E> + Send,
I: InboxType,
I::Config: Default,
E: Send + 'static,
{
spawn_with(Default::default(), Default::default(), function)
}
pub fn spawn_with<I, E, Fun, Fut>(
link: Link,
config: I::Config,
function: Fun,
) -> (Child<E, I>, Address<I>)
where
Fun: FnOnce(I) -> Fut + Send + 'static,
Fut: Future<Output = E> + Send,
I: InboxType,
E: Send + 'static,
{
let (channel, inbox) = I::init_single_inbox(config, 1, ActorId::generate());
let handle = tokio::task::spawn(async move { function(inbox).await });
(
Child::new(channel.clone(), handle, link),
Address::from_channel(channel),
)
}
pub fn spawn_many<I, E, Itm, Fun, Fut>(
iter: impl ExactSizeIterator<Item = Itm>,
function: Fun,
) -> (ChildPool<E, I>, Address<I>)
where
Fun: FnOnce(Itm, I) -> Fut + Clone + Send + 'static,
Fut: Future<Output = E> + Send,
I: MultiProcessInbox,
I::Config: Default,
E: Send + 'static,
Itm: Send + 'static,
{
spawn_many_with(Default::default(), Default::default(), iter, function)
}
pub fn spawn_many_with<I, E, Itm, Fun, Fut>(
link: Link,
config: I::Config,
iter: impl ExactSizeIterator<Item = Itm>,
function: Fun,
) -> (ChildPool<E, I>, Address<I>)
where
Fun: FnOnce(Itm, I) -> Fut + Clone + Send + 'static,
Fut: Future<Output = E> + Send,
I: MultiProcessInbox,
E: Send + 'static,
Itm: Send + 'static,
{
let channel = I::init_multi_inbox(config, iter.len(), 1, ActorId::generate());
let handles = iter
.map(|i| {
let fun = function.clone();
let inbox = I::from_channel(channel.clone());
tokio::task::spawn(async move { fun(i, inbox).await })
})
.collect::<Vec<_>>();
(
Child::new(channel.clone(), handles, link),
Address::from_channel(channel),
)
}