pub trait WebSocketServerTrait: DowncastSync {
// Required methods
fn bind<'life0, 'async_trait>(
self: Arc<Self>,
addr: &'life0 str,
) -> Pin<Box<dyn Future<Output = Result<TcpListener>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn listen<'async_trait>(
self: Arc<Self>,
listener: TcpListener,
config: Option<WebSocketConfig>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait;
fn stop(&self) -> Result<()>;
fn join<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
fn stop_and_join<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait,
'life0: 'async_trait;
}
Expand description
Base WebSocketServer trait allows the WebSocketServer<T>
struct
to be retained by the trait reference by casting it to the trait
as follows:
use std::sync::Arc;
use async_trait::async_trait;
use workflow_websocket::server::{Result,WebSocketServerTrait,WebSocketConfig,TcpListener};
struct Server{}
#[async_trait]
impl WebSocketServerTrait for Server {
async fn bind(self: Arc<Self>, addr: &str) -> Result<TcpListener>{
unimplemented!()
}
async fn listen(self: Arc<Self>, listener : TcpListener, config: Option<WebSocketConfig>) -> Result<()>{
unimplemented!()
}
fn stop(&self) -> Result<()>{
unimplemented!()
}
async fn join(&self) -> Result<()>{
unimplemented!()
}
async fn stop_and_join(&self) -> Result<()>{
unimplemented!()
}
}
let server_trait: Arc<dyn WebSocketServerTrait> = Arc::new(Server{});
let server = server_trait.downcast_arc::<Server>();
This can help simplify web socket handling in case the supplied
T
generic contains complex generic types that typically
results in generics propagating up into the ownership type chain.
This trait is used in the workflow-rpc
crate to isolate RpcHandler
generics from the RpcServer owning the WebSocket.
Required Methods§
fn bind<'life0, 'async_trait>(
self: Arc<Self>,
addr: &'life0 str,
) -> Pin<Box<dyn Future<Output = Result<TcpListener>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn listen<'async_trait>(
self: Arc<Self>,
listener: TcpListener,
config: Option<WebSocketConfig>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
fn stop(&self) -> Result<()>
fn join<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stop_and_join<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Implementations§
Source§impl dyn WebSocketServerTrait
impl dyn WebSocketServerTrait
Sourcepub fn is<__T: WebSocketServerTrait>(&self) -> bool
pub fn is<__T: WebSocketServerTrait>(&self) -> bool
Returns true if the trait object wraps an object of type __T
.
Sourcepub fn downcast<__T: WebSocketServerTrait>(
self: Box<Self>,
) -> Result<Box<__T>, Box<Self>>
pub fn downcast<__T: WebSocketServerTrait>( self: Box<Self>, ) -> Result<Box<__T>, Box<Self>>
Returns a boxed object from a boxed trait object if the underlying object is of type
__T
. Returns the original boxed trait if it isn’t.
Sourcepub fn downcast_rc<__T: WebSocketServerTrait>(
self: Rc<Self>,
) -> Result<Rc<__T>, Rc<Self>>
pub fn downcast_rc<__T: WebSocketServerTrait>( self: Rc<Self>, ) -> Result<Rc<__T>, Rc<Self>>
Returns an Rc
-ed object from an Rc
-ed trait object if the underlying object is of
type __T
. Returns the original Rc
-ed trait if it isn’t.
Sourcepub fn downcast_ref<__T: WebSocketServerTrait>(&self) -> Option<&__T>
pub fn downcast_ref<__T: WebSocketServerTrait>(&self) -> Option<&__T>
Returns a reference to the object within the trait object if it is of type __T
, or
None
if it isn’t.
Sourcepub fn downcast_mut<__T: WebSocketServerTrait>(&mut self) -> Option<&mut __T>
pub fn downcast_mut<__T: WebSocketServerTrait>(&mut self) -> Option<&mut __T>
Returns a mutable reference to the object within the trait object if it is of type
__T
, or None
if it isn’t.
Sourcepub fn downcast_arc<__T: WebSocketServerTrait + Any + Send + Sync>(
self: Arc<Self>,
) -> Result<Arc<__T>, Arc<Self>>
pub fn downcast_arc<__T: WebSocketServerTrait + Any + Send + Sync>( self: Arc<Self>, ) -> Result<Arc<__T>, Arc<Self>>
Returns an Arc
-ed object from an Arc
-ed trait object if the underlying object is of
type __T
. Returns the original Arc
-ed trait if it isn’t.