pub trait WebSocketServerTrait: DowncastSync {
// Required methods
fn bind<'life0, 'async_trait>(
self: Arc<Self>,
addr: &'life0 str,
) -> Pin<Box<dyn Future<Output = Result<TcpListener, Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait;
fn listen<'async_trait>(
self: Arc<Self>,
listener: TcpListener,
config: Option<WebSocketConfig>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where Self: 'async_trait;
fn stop(&self) -> Result<(), Error>;
fn join<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: 'async_trait;
fn stop_and_join<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>
where 'life0: 'async_trait,
Self: '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, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn listen<'async_trait>(
self: Arc<Self>,
listener: TcpListener,
config: Option<WebSocketConfig>,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
Self: 'async_trait,
fn stop(&self) -> Result<(), Error>
fn join<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
fn stop_and_join<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
Self: 'async_trait,
Implementations§
Source§impl dyn WebSocketServerTrait
impl dyn WebSocketServerTrait
Sourcepub fn is<__T>(&self) -> boolwhere
__T: WebSocketServerTrait,
pub fn is<__T>(&self) -> boolwhere
__T: WebSocketServerTrait,
Returns true if the trait object wraps an object of type __T
.
Sourcepub fn downcast<__T>(
self: Box<dyn WebSocketServerTrait>,
) -> Result<Box<__T>, Box<dyn WebSocketServerTrait>>where
__T: WebSocketServerTrait,
pub fn downcast<__T>(
self: Box<dyn WebSocketServerTrait>,
) -> Result<Box<__T>, Box<dyn WebSocketServerTrait>>where
__T: WebSocketServerTrait,
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>(
self: Rc<dyn WebSocketServerTrait>,
) -> Result<Rc<__T>, Rc<dyn WebSocketServerTrait>>where
__T: WebSocketServerTrait,
pub fn downcast_rc<__T>(
self: Rc<dyn WebSocketServerTrait>,
) -> Result<Rc<__T>, Rc<dyn WebSocketServerTrait>>where
__T: WebSocketServerTrait,
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>(&self) -> Option<&__T>where
__T: WebSocketServerTrait,
pub fn downcast_ref<__T>(&self) -> Option<&__T>where
__T: WebSocketServerTrait,
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>(&mut self) -> Option<&mut __T>where
__T: WebSocketServerTrait,
pub fn downcast_mut<__T>(&mut self) -> Option<&mut __T>where
__T: WebSocketServerTrait,
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>(
self: Arc<dyn WebSocketServerTrait>,
) -> Result<Arc<__T>, Arc<dyn WebSocketServerTrait>>
pub fn downcast_arc<__T>( self: Arc<dyn WebSocketServerTrait>, ) -> Result<Arc<__T>, Arc<dyn WebSocketServerTrait>>
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.