Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/asynch/clients/websocket/_std.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::exceptions::XRPLWebSocketException;
use super::{WebSocketClosed, WebSocketOpen};
use super::{ConnectionStatus, WebSocketClosed, WebSocketOpen};
use crate::asynch::clients::client::XRPLClient;
use crate::asynch::clients::exceptions::{XRPLClientException, XRPLClientResult};
use crate::asynch::clients::websocket::websocket_base::{MessageHandler, WebsocketBase};
Expand Down Expand Up @@ -164,9 +164,10 @@ where
impl<M, Status> AsyncWebSocketClient<M, Status>
where
M: RawMutex,
Status: ConnectionStatus,
{
pub fn is_open(&self) -> bool {
core::any::type_name::<Status>() == core::any::type_name::<WebSocketOpen>()
Status::IS_OPEN
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/asynch/clients/websocket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ use super::exceptions::XRPLClientResult;
pub struct WebSocketOpen;
pub struct WebSocketClosed;

mod sealed {
pub trait Sealed {}
impl Sealed for super::WebSocketOpen {}
impl Sealed for super::WebSocketClosed {}
}

/// Type-state marker describing whether a [`websocket`](self) client is open.
///
/// Implemented only for [`WebSocketOpen`] and [`WebSocketClosed`], this lets
/// `is_open()` resolve from the type state at compile time instead of relying
/// on the unstable [`core::any::type_name`] string representation.
pub trait ConnectionStatus: sealed::Sealed {
/// Whether this state represents an open connection.
const IS_OPEN: bool;
}

impl ConnectionStatus for WebSocketOpen {
const IS_OPEN: bool = true;
}

impl ConnectionStatus for WebSocketClosed {
const IS_OPEN: bool = false;
}

#[allow(async_fn_in_trait)]
pub trait XRPLAsyncWebsocketIO {
async fn xrpl_send(&mut self, message: XRPLRequest<'_>) -> XRPLClientResult<()>;
Expand Down Expand Up @@ -100,3 +124,19 @@ where
}
}
}

#[cfg(test)]
mod tests {
use super::{ConnectionStatus, WebSocketClosed, WebSocketOpen};

/// Mirrors how `is_open()` reads the constant through the type state.
fn is_open<S: ConnectionStatus>() -> bool {
S::IS_OPEN
}

#[test]
fn connection_status_is_open_resolves_from_type_state() {
assert!(is_open::<WebSocketOpen>());
assert!(!is_open::<WebSocketClosed>());
}
}