//! Models what the server API should be like //! so to allow us full control of the [Uni] and [Channel]s being used. use std::marker::PhantomData; use crate::uni::{GenericUni, MessagingMutinyStream, channel::{ChannelZeroCopy, ChannelMove}, Uni}; // All possible Channels & Uni variations for our server //////////////////////////////////////////////////////// /// A zero-cost abstraction type allowing to use `reactive-mutiny` Zero-Copy channels /// (incoming & outgoing messages are guaranteed to be zero-copied) pub type ZeroCopySocketServer = SocketServer>, ChannelZeroCopy>; /// A zero-cost abstraction type ready to use `reactive-mutiny' Movable channels /// (incoming & outgoing messages might be copied, if the compiler can't optimize those operations) pub type MovableSocketServer = SocketServer>, ChannelMove>; pub struct SocketServer, SenderChannelType> { pub _phantom: PhantomData<(RemoteMessages,LocalMessages,ProcessorUniType,SenderChannelType)> } impl, SenderChannelType> SocketServer { /// Starts the server, returning an `Arc` so it may still be shutdown pub fn start> (self, _connection_events_handler: impl FnMut(ConnectionEvents), _processor: impl FnOnce(MessagingMutinyStream) -> LocalMessagesIteratorType) -> impl SocketServerController { self } } pub trait SocketServerController { fn shutdown(self); } impl, SenderChannelType> SocketServerController for SocketServer { fn shutdown(self) {} } /// Helps to infer some types: /// ```nocompile /// type THE_TYPE_I_WANT = as GenericSocketServer>::THE_TYPE_YOU_WANT pub trait GenericSocketServer { const CONFIG: usize; type RemoteMessages; type LocalMessages; type ProcessorUniType: GenericUni; type SenderChannelType; type ConnectionEventType; type StreamItemType; type StreamType; } impl, SenderChannelType> GenericSocketServer for SocketServer { const CONFIG: usize = CONFIG; type RemoteMessages = RemoteMessages; type LocalMessages = LocalMessages; type ProcessorUniType = ProcessorUniType; type SenderChannelType = SenderChannelType; type ConnectionEventType = SenderChannelType; type StreamItemType = ProcessorUniType::DerivedItemType; type StreamType = MessagingMutinyStream; } /// The possible connection events pub enum ConnectionEvents { /// Happens when the client connects _Connected(SenderChannelType), /// Happens when the client disconnects _Disconnected(SenderChannelType), /// Happens when there was an error when reading/writing data _IOError(SenderChannelType), }