Skip to content

Commit d2c2548

Browse files
committed
Use conditional type aliases to reduce cfg noise on structs
1 parent ec6cdec commit d2c2548

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

sdks/rust/src/db_connection.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ use tokio::{
5454

5555
pub(crate) type SharedCell<T> = Arc<StdMutex<T>>;
5656

57+
#[cfg(not(feature = "web"))]
58+
type SharedAsyncCell<T> = Arc<TokioMutex<T>>;
59+
#[cfg(feature = "web")]
60+
type SharedAsyncCell<T> = SharedCell<T>;
61+
5762
/// Implementation of `DbConnection`, `EventContext`,
5863
/// and anything else that provides access to the database connection.
5964
///
@@ -74,10 +79,7 @@ pub struct DbContextImpl<M: SpacetimeModule> {
7479

7580
/// Receiver channel for WebSocket messages,
7681
/// which are pre-parsed in the background by [`parse_loop`].
77-
#[cfg(not(feature = "web"))]
78-
recv: Arc<TokioMutex<mpsc::UnboundedReceiver<ParsedMessage<M>>>>,
79-
#[cfg(feature = "web")]
80-
recv: SharedCell<mpsc::UnboundedReceiver<ParsedMessage<M>>>,
82+
recv: SharedAsyncCell<mpsc::UnboundedReceiver<ParsedMessage<M>>>,
8183

8284
/// Channel into which operations which apparently mutate SDK state,
8385
/// e.g. registering callbacks, push [`PendingMutation`] messages,
@@ -87,10 +89,7 @@ pub struct DbContextImpl<M: SpacetimeModule> {
8789

8890
/// Receive end of `pending_mutations_send`,
8991
/// from which [Self::apply_pending_mutations] and friends read mutations.
90-
#[cfg(not(feature = "web"))]
91-
pending_mutations_recv: Arc<TokioMutex<mpsc::UnboundedReceiver<PendingMutation<M>>>>,
92-
#[cfg(feature = "web")]
93-
pending_mutations_recv: SharedCell<mpsc::UnboundedReceiver<PendingMutation<M>>>,
92+
pending_mutations_recv: SharedAsyncCell<mpsc::UnboundedReceiver<PendingMutation<M>>>,
9493

9594
/// This connection's `Identity`.
9695
///

sdks/rust/src/websocket.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ use tokio_tungstenite_wasm::{Message as WebSocketMessage, WebSocketStream};
3333
use crate::compression::decompress_server_message;
3434
use crate::metrics::CLIENT_METRICS;
3535

36+
#[cfg(not(feature = "web"))]
37+
type TokioTungsteniteError = tokio_tungstenite::tungstenite::Error;
38+
#[cfg(feature = "web")]
39+
type TokioTungsteniteError = tokio_tungstenite_wasm::Error;
40+
3641
#[derive(Error, Debug, Clone)]
3742
pub enum UriError {
3843
#[error("Unknown URI scheme {scheme}, expected http, https, ws or wss")]
@@ -59,22 +64,12 @@ pub enum WsError {
5964
#[error(transparent)]
6065
UriError(#[from] UriError),
6166

62-
#[cfg(not(feature = "web"))]
63-
#[error("Error in WebSocket connection with {uri}: {source}")]
64-
Tungstenite {
65-
uri: Uri,
66-
#[source]
67-
// `Arc` is required for `Self: Clone`, as `tungstenite::Error: !Clone`.
68-
source: Arc<tokio_tungstenite::tungstenite::Error>,
69-
},
70-
71-
#[cfg(feature = "web")]
7267
#[error("Error in WebSocket connection with {uri}: {source}")]
7368
Tungstenite {
7469
uri: Uri,
7570
#[source]
7671
// `Arc` is required for `Self: Clone`, as `tungstenite::Error: !Clone`.
77-
source: Arc<tokio_tungstenite_wasm::Error>,
72+
source: Arc<TokioTungsteniteError>,
7873
},
7974

8075
#[error("Received empty raw message, but valid messages always start with a one-byte compression flag")]

0 commit comments

Comments
 (0)