1use std::{future::Future, net::SocketAddr, pin::Pin};
2
3use crate::core::{CoreResult, ShutdownToken};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ServiceInfo {
8 pub name: String,
10 pub addr: SocketAddr,
12}
13
14impl ServiceInfo {
15 pub fn new(name: impl Into<String>, addr: SocketAddr) -> Self {
17 Self {
18 name: name.into(),
19 addr,
20 }
21 }
22}
23
24pub type ServiceFuture<'a> = Pin<Box<dyn Future<Output = CoreResult<()>> + Send + 'a>>;
26
27pub trait Service: Send + Sync + 'static {
33 fn name(&self) -> &str;
35
36 fn start(&self, shutdown: ShutdownToken) -> ServiceFuture<'_>;
38
39 fn stop(&self) -> ServiceFuture<'_> {
41 Box::pin(async { Ok(()) })
42 }
43}
44
45pub struct FnService<F> {
47 name: String,
48 start: F,
49}
50
51impl<F> FnService<F> {
52 pub fn new(name: impl Into<String>, start: F) -> Self {
54 Self {
55 name: name.into(),
56 start,
57 }
58 }
59}
60
61impl<F, Fut> Service for FnService<F>
62where
63 F: Fn(ShutdownToken) -> Fut + Send + Sync + 'static,
64 Fut: Future<Output = CoreResult<()>> + Send + 'static,
65{
66 fn name(&self) -> &str {
67 &self.name
68 }
69
70 fn start(&self, shutdown: ShutdownToken) -> ServiceFuture<'_> {
71 Box::pin((self.start)(shutdown))
72 }
73}