Skip to content

Commit

Permalink
refactor(ws server): impl IdProvider for Box<T> (#684)
Browse files Browse the repository at this point in the history
* refactor(ws server): set_id_provider `Box<dyn>`

* address grumbles: thanks James

* relax trait bounds with ?Sized

* Update ws-server/src/tests.rs

* add docs
  • Loading branch information
niklasad1 authored Feb 1, 2022
1 parent 2864756 commit 425ae56
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ license = "MIT"
anyhow = "1"
arrayvec = "0.7.1"
async-trait = "0.1"
beef = { version = "0.5.1", features = ["impl_serde"] }
async-channel = { version = "1.6", optional = true }
beef = { version = "0.5.1", features = ["impl_serde"] }
thiserror = "1"
futures-channel = { version = "0.3.14", default-features = false }
futures-util = { version = "0.3.14", default-features = false, optional = true }
Expand Down
6 changes: 3 additions & 3 deletions core/src/id_providers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::traits::IdProvider;
use jsonrpsee_types::SubscriptionId;

/// Generates random integers as subscription ID.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct RandomIntegerIdProvider;

impl IdProvider for RandomIntegerIdProvider {
Expand All @@ -42,7 +42,7 @@ impl IdProvider for RandomIntegerIdProvider {
}

/// Generates random strings of length `len` as subscription ID.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct RandomStringIdProvider {
len: usize,
}
Expand All @@ -62,7 +62,7 @@ impl IdProvider for RandomStringIdProvider {
}

/// No-op implementation to be used for servers that don't support subscriptions.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct NoopIdProvider;

impl IdProvider for NoopIdProvider {
Expand Down
13 changes: 12 additions & 1 deletion core/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,18 @@ tuple_impls! {
}

/// Trait to generate subscription IDs.
pub trait IdProvider: Send + Sync {
pub trait IdProvider: Send + Sync + std::fmt::Debug {
/// Returns the next ID for the subscription.
fn next_id(&self) -> SubscriptionId<'static>;
}

// Implement `IdProvider` for `Box<T>`
//
// It's not implemented for `&'_ T` because
// of the required `'static lifetime`
// Thus, `&dyn IdProvider` won't work.
impl<T: IdProvider + ?Sized> IdProvider for Box<T> {
fn next_id(&self) -> SubscriptionId<'static> {
(**self).next_id()
}
}
21 changes: 13 additions & 8 deletions ws-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ impl<M> std::fmt::Debug for Server<M> {
.field("listener", &self.listener)
.field("cfg", &self.cfg)
.field("stop_monitor", &self.stop_monitor)
.field("id_provider", &self.id_provider)
.field("resources", &self.resources)
.finish()
}
}
Expand Down Expand Up @@ -644,19 +646,14 @@ impl Default for Settings {
}

/// Builder to configure and create a JSON-RPC Websocket server
#[derive(Debug)]
pub struct Builder<M = ()> {
settings: Settings,
resources: Resources,
middleware: M,
id_provider: Arc<dyn IdProvider>,
}

impl<M> std::fmt::Debug for Builder<M> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Builder").field("settings", &self.settings).field("resources", &self.resources).finish()
}
}

impl Default for Builder {
fn default() -> Self {
Builder {
Expand Down Expand Up @@ -810,16 +807,24 @@ impl<M> Builder<M> {
/// Configure custom `subscription ID` provider for the server to use
/// to when getting new subscription calls.
///
/// You may choose static dispatch or dynamic dispatch because
/// `IdProvider` is implemented for `Box<T>`.
///
/// Default: [`RandomIntegerIdProvider`].
///
/// # Examples
///
/// ```rust
/// use jsonrpsee_ws_server::{WsServerBuilder, RandomStringIdProvider, IdProvider};
///
/// let builder = WsServerBuilder::default().set_id_provider(RandomStringIdProvider::new(16));
/// // static dispatch
/// let builder1 = WsServerBuilder::default().set_id_provider(RandomStringIdProvider::new(16));
///
/// // or dynamic dispatch
/// let builder2 = WsServerBuilder::default().set_id_provider(Box::new(RandomStringIdProvider::new(16)));
/// ```
pub fn set_id_provider(mut self, id_provider: impl IdProvider + 'static) -> Self {
///
pub fn set_id_provider<I: IdProvider + 'static>(mut self, id_provider: I) -> Self {
self.id_provider = Arc::new(id_provider);
self
}
Expand Down
1 change: 1 addition & 0 deletions ws-server/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,7 @@ async fn unsubscribe_wrong_sub_id_type() {

#[tokio::test]
async fn custom_subscription_id_works() {
#[derive(Debug, Clone)]
struct HardcodedSubscriptionId;

impl IdProvider for HardcodedSubscriptionId {
Expand Down

0 comments on commit 425ae56

Please sign in to comment.