Skip to main content

protosocket_rpc/server/
spawn.rs

1use std::{future::Future, marker::PhantomData};
2
3/// A strategy for spawning futures.
4///
5/// Notably, Send is not required at the Spawn level. If you have
6/// a non-Send service, you can implement your own Spawn.
7pub trait Spawn<F>: 'static
8where
9    F: Future + 'static,
10{
11    /// Spawn a connection driver task and a connection server task.
12    fn spawn(&mut self, future: F);
13}
14
15/// When everything in your `SocketService` is `Send`, you can use a TokioSpawn.
16#[derive(Debug)]
17pub struct TokioSpawn<F> {
18    _phantom: PhantomData<F>,
19}
20
21impl<F> Default for TokioSpawn<F> {
22    fn default() -> Self {
23        Self {
24            _phantom: Default::default(),
25        }
26    }
27}
28impl<F> Spawn<F> for TokioSpawn<F>
29where
30    F: Future + Send + 'static,
31    F::Output: Send + 'static,
32{
33    fn spawn(&mut self, driver: F) {
34        tokio::spawn(driver);
35    }
36}
37
38/// When everything in your `SocketService` is `Send`, and you want a connection to be thread-pinned, you can use a LevelSpawnConnection.
39#[derive(Debug)]
40pub struct LevelSpawn<F> {
41    _phantom: PhantomData<F>,
42}
43impl<F> Default for LevelSpawn<F> {
44    fn default() -> Self {
45        Self {
46            _phantom: Default::default(),
47        }
48    }
49}
50impl<F> Spawn<F> for LevelSpawn<F>
51where
52    F: Future + Send + 'static,
53    F::Output: Send + 'static,
54{
55    fn spawn(&mut self, future: F) {
56        level_runtime::spawn_local(future);
57    }
58}