Skip to main content

rama_net/socket/
svc.rs

1use crate::address::SocketAddress;
2
3use rama_core::{Service, error::BoxError};
4
5/// Glue trait that is used as the trait bound for
6/// code creating/preparing a socket on one layer or another.
7///
8/// Can also be manually implemented as an alternative [`Service`] trait,
9/// but from a Rama POV it is mostly used for UX trait bounds.
10pub trait SocketService: Send + Sync + 'static {
11    /// Socket returned by the [`SocketService`]
12    type Socket: Send + 'static;
13    /// Error returned in case of connection / setup failure
14    type Error: Into<BoxError> + Send + 'static;
15
16    /// Create a binding to a Unix/Linux/Windows socket.
17    fn bind_socket_with_address(
18        &self,
19        addr: impl Into<SocketAddress>,
20    ) -> impl Future<Output = Result<Self::Socket, Self::Error>> + Send + '_;
21}
22
23impl<S, Socket> SocketService for S
24where
25    S: Service<SocketAddress, Output = Socket, Error: Into<BoxError> + Send + 'static>,
26    Socket: Send + 'static,
27{
28    type Socket = Socket;
29    type Error = S::Error;
30
31    fn bind_socket_with_address(
32        &self,
33        addr: impl Into<SocketAddress>,
34    ) -> impl Future<Output = Result<Self::Socket, Self::Error>> + Send + '_ {
35        self.serve(addr.into())
36    }
37}