Skip to content

Commit 1d9d3d4

Browse files
committed
Use types for default implementations of L2 traits
This allows us to avoid dynamic dispatch and use static dispatch instead, which can improve performance and reduce code size. The InMemorySubscriber::for_clients function no longer takes a LocalUriProvider parameter, because it was not used anyways.
1 parent 0556d7d commit 1d9d3d4

4 files changed

Lines changed: 48 additions & 63 deletions

File tree

src/communication/default_notifier.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ use super::{
2626

2727
/// A [`Notifier`] that uses the uProtocol Transport Layer API to send and receive
2828
/// notifications to/from (other) uEntities.
29-
pub struct SimpleNotifier {
30-
transport: Arc<dyn UTransport>,
31-
uri_provider: Arc<dyn LocalUriProvider>,
29+
pub struct SimpleNotifier<T, P> {
30+
transport: Arc<T>,
31+
uri_provider: Arc<P>,
3232
}
3333

34-
impl SimpleNotifier {
34+
impl<T: UTransport, P: LocalUriProvider> SimpleNotifier<T, P> {
3535
/// Creates a new Notifier for a given transport.
3636
///
3737
/// # Arguments
3838
///
3939
/// * `transport` - The uProtocol Transport Layer implementation to use for sending and receiving notification messages.
4040
/// * `uri_provider` - The helper for creating URIs that represent local resources.
41-
pub fn new(transport: Arc<dyn UTransport>, uri_provider: Arc<dyn LocalUriProvider>) -> Self {
41+
pub fn new(transport: Arc<T>, uri_provider: Arc<P>) -> Self {
4242
SimpleNotifier {
4343
transport,
4444
uri_provider,
@@ -47,7 +47,7 @@ impl SimpleNotifier {
4747
}
4848

4949
#[async_trait]
50-
impl Notifier for SimpleNotifier {
50+
impl<T: UTransport, P: LocalUriProvider> Notifier for SimpleNotifier<T, P> {
5151
async fn notify(
5252
&self,
5353
resource_id: u16,
@@ -111,7 +111,7 @@ mod tests {
111111
StaticUriProvider, UCode, UPriority, UStatus, UUri, UUID,
112112
};
113113

114-
fn new_uri_provider() -> Arc<dyn LocalUriProvider> {
114+
fn new_uri_provider() -> Arc<StaticUriProvider> {
115115
Arc::new(StaticUriProvider::new("", 0x0005, 0x02))
116116
}
117117

src/communication/default_pubsub.rs

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,19 @@ impl UListener for SubscriptionChangeListener {
184184
}
185185

186186
/// A [`Publisher`] that uses the uProtocol Transport Layer API for publishing events to topics.
187-
pub struct SimplePublisher {
188-
transport: Arc<dyn UTransport>,
189-
uri_provider: Arc<dyn LocalUriProvider>,
187+
pub struct SimplePublisher<T, P> {
188+
transport: Arc<T>,
189+
uri_provider: Arc<P>,
190190
}
191191

192-
impl SimplePublisher {
192+
impl<T: UTransport, P: LocalUriProvider> SimplePublisher<T, P> {
193193
/// Creates a new client.
194194
///
195195
/// # Arguments
196196
///
197197
/// * `transport` - The transport to use for sending messages.
198198
/// * `uri_provider` - The service to use for creating the event messages' _sink_ address.
199-
pub fn new(transport: Arc<dyn UTransport>, uri_provider: Arc<dyn LocalUriProvider>) -> Self {
199+
pub fn new(transport: Arc<T>, uri_provider: Arc<P>) -> Self {
200200
SimplePublisher {
201201
transport,
202202
uri_provider,
@@ -205,7 +205,7 @@ impl SimplePublisher {
205205
}
206206

207207
#[async_trait]
208-
impl Publisher for SimplePublisher {
208+
impl<T: UTransport, P: LocalUriProvider> Publisher for SimplePublisher<T, P> {
209209
async fn publish(
210210
&self,
211211
resource_id: u16,
@@ -242,15 +242,16 @@ impl Publisher for SimplePublisher {
242242
/// about the new subscription and a (client provided) subscription change handler is registered with the
243243
/// listener. When a subscription change notification arrives from the USubscription service, the corresponding
244244
/// handler is being looked up and invoked.
245-
pub struct InMemorySubscriber {
246-
transport: Arc<dyn UTransport>,
247-
_uri_provider: Arc<dyn LocalUriProvider>,
248-
usubscription: Arc<dyn USubscription>,
249-
notifier: Arc<dyn Notifier>,
245+
pub struct InMemorySubscriber<T, S, N> {
246+
transport: Arc<T>,
247+
usubscription: Arc<S>,
248+
notifier: Arc<N>,
250249
subscription_change_listener: Arc<SubscriptionChangeListener>,
251250
}
252251

253-
impl InMemorySubscriber {
252+
impl<T: UTransport + 'static, P: LocalUriProvider + 'static>
253+
InMemorySubscriber<T, RpcClientUSubscription, SimpleNotifier<T, P>>
254+
{
254255
/// Creates a new Subscriber for a given transport.
255256
///
256257
/// The subscriber keeps track of subscription change handlers in memory only.
@@ -260,35 +261,32 @@ impl InMemorySubscriber {
260261
/// # Errors
261262
///
262263
/// Returns an error if the Notifier cannot register a listener for notifications from the USubscription service.
263-
pub async fn new(
264-
transport: Arc<dyn UTransport>,
265-
uri_provider: Arc<dyn LocalUriProvider>,
266-
) -> Result<Self, RegistrationError> {
264+
pub async fn new(transport: Arc<T>, uri_provider: Arc<P>) -> Result<Self, RegistrationError> {
267265
let rpc_client = InMemoryRpcClient::new(transport.clone(), uri_provider.clone())
268266
.await
269267
.map(Arc::new)?;
270268
let usubscription_client = Arc::new(RpcClientUSubscription::new(rpc_client));
271269
let notifier = Arc::new(SimpleNotifier::new(transport.clone(), uri_provider.clone()));
272-
Self::for_clients(transport, uri_provider, usubscription_client, notifier).await
270+
Self::for_clients(transport, usubscription_client, notifier).await
273271
}
272+
}
274273

274+
impl<T: UTransport, S: USubscription, N: Notifier> InMemorySubscriber<T, S, N> {
275275
/// Creates a new Subscriber for given clients.
276276
///
277277
/// # Arguments
278278
///
279279
/// * `transport` - The transport to use for registering the event listeners for subscribed topics.
280-
/// * `uri-provider` - The service to use for creating topic addresses.
281280
/// * `usubscription` - The client to use for interacting with the (local) USubscription service.
282281
/// * `notifier` - The client to use for registering the listener for subscription updates from USubscription.
283282
///
284283
/// # Errors
285284
///
286285
/// Returns an error if the Notifier cannot register a listener for notifications from the USubscription service.
287286
pub async fn for_clients(
288-
transport: Arc<dyn UTransport>,
289-
uri_provider: Arc<dyn LocalUriProvider>,
290-
usubscription: Arc<dyn USubscription>,
291-
notifier: Arc<dyn Notifier>,
287+
transport: Arc<T>,
288+
usubscription: Arc<S>,
289+
notifier: Arc<N>,
292290
) -> Result<Self, RegistrationError> {
293291
// register a generic listener for subscription updates
294292
// whenever a uE later tries to subscribe to a topic, it can provide an optional callback for
@@ -304,7 +302,6 @@ impl InMemorySubscriber {
304302
.await?;
305303
Ok(InMemorySubscriber {
306304
transport,
307-
_uri_provider: uri_provider,
308305
usubscription,
309306
notifier,
310307
subscription_change_listener,
@@ -400,7 +397,7 @@ impl InMemorySubscriber {
400397
}
401398

402399
#[async_trait]
403-
impl Subscriber for InMemorySubscriber {
400+
impl<T: UTransport, U: USubscription, N: Notifier> Subscriber for InMemorySubscriber<T, U, N> {
404401
async fn subscribe(
405402
&self,
406403
topic_filter: &UUri,
@@ -459,11 +456,11 @@ mod tests {
459456
StaticUriProvider, UAttributes, UCode, UMessageType, UPriority, UStatus, UUri, UUID,
460457
};
461458

462-
fn new_uri_provider() -> Arc<dyn LocalUriProvider> {
459+
fn new_uri_provider() -> Arc<StaticUriProvider> {
463460
Arc::new(StaticUriProvider::new("", 0x0005, 0x02))
464461
}
465462

466-
fn succeeding_notifier() -> Arc<dyn Notifier> {
463+
fn succeeding_notifier() -> Arc<MockNotifier> {
467464
let mut notifier = MockNotifier::new();
468465
notifier
469466
.expect_start_listening()
@@ -586,7 +583,6 @@ mod tests {
586583
// WHEN trying to create a Subscriber for this Notifier
587584
let creation_attempt = InMemorySubscriber::for_clients(
588585
Arc::new(MockTransport::new()),
589-
new_uri_provider(),
590586
Arc::new(MockUSubscription::new()),
591587
Arc::new(notifier),
592588
)
@@ -612,7 +608,6 @@ mod tests {
612608

613609
let subscriber = InMemorySubscriber {
614610
transport: Arc::new(MockTransport::new()),
615-
_uri_provider: new_uri_provider(),
616611
usubscription: Arc::new(MockUSubscription::new()),
617612
notifier: Arc::new(notifier),
618613
subscription_change_listener,
@@ -682,7 +677,6 @@ mod tests {
682677
// and a Subscriber using that USubscription client
683678
let subscriber = InMemorySubscriber::for_clients(
684679
Arc::new(transport),
685-
new_uri_provider(),
686680
Arc::new(usubscription_client),
687681
succeeding_notifier(),
688682
)
@@ -752,7 +746,6 @@ mod tests {
752746
// and a Subscriber using that USubscription client, Notifier and transport
753747
let subscriber = InMemorySubscriber::for_clients(
754748
Arc::new(transport),
755-
new_uri_provider(),
756749
Arc::new(usubscription_client),
757750
succeeding_notifier(),
758751
)
@@ -834,7 +827,6 @@ mod tests {
834827
// and a Subscriber using that USubscription client, Notifier and transport
835828
let subscriber = InMemorySubscriber::for_clients(
836829
Arc::new(transport),
837-
new_uri_provider(),
838830
Arc::new(usubscription_client),
839831
succeeding_notifier(),
840832
)
@@ -891,7 +883,6 @@ mod tests {
891883
// and a Subscriber using that USubscription client, Notifier and transport
892884
let subscriber = InMemorySubscriber::for_clients(
893885
Arc::new(transport),
894-
new_uri_provider(),
895886
Arc::new(usubscription_client),
896887
succeeding_notifier(),
897888
)
@@ -928,7 +919,6 @@ mod tests {
928919
// and a Subscriber using that USubscription client, Notifier and transport
929920
let subscriber = InMemorySubscriber::for_clients(
930921
Arc::new(transport),
931-
new_uri_provider(),
932922
Arc::new(usubscription_client),
933923
succeeding_notifier(),
934924
)
@@ -975,7 +965,6 @@ mod tests {
975965
// and a Subscriber using that USubscription client, Notifier and transport
976966
let subscriber = InMemorySubscriber::for_clients(
977967
Arc::new(transport),
978-
new_uri_provider(),
979968
Arc::new(usubscription_client),
980969
succeeding_notifier(),
981970
)
@@ -1031,7 +1020,6 @@ mod tests {
10311020
// and a Subscriber using that USubscription client, Notifier and transport
10321021
let subscriber = InMemorySubscriber::for_clients(
10331022
Arc::new(transport),
1034-
new_uri_provider(),
10351023
Arc::new(usubscription_client),
10361024
succeeding_notifier(),
10371025
)

src/communication/in_memory_rpc_client.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,13 @@ impl UListener for ResponseListener {
156156
/// implementation and a response handler is created and registered with the listener.
157157
/// When an RPC Response message arrives from the service, the corresponding handler is being looked
158158
/// up and invoked.
159-
pub struct InMemoryRpcClient {
160-
transport: Arc<dyn UTransport>,
161-
uri_provider: Arc<dyn LocalUriProvider>,
159+
pub struct InMemoryRpcClient<T, P> {
160+
transport: Arc<T>,
161+
uri_provider: Arc<P>,
162162
response_listener: Arc<ResponseListener>,
163163
}
164164

165-
impl InMemoryRpcClient {
165+
impl<T: UTransport, P: LocalUriProvider> InMemoryRpcClient<T, P> {
166166
/// Creates a new RPC client for a given transport.
167167
///
168168
/// # Arguments
@@ -174,10 +174,7 @@ impl InMemoryRpcClient {
174174
///
175175
/// Returns an error if the generic RPC Response listener could not be
176176
/// registered with the given transport.
177-
pub async fn new(
178-
transport: Arc<dyn UTransport>,
179-
uri_provider: Arc<dyn LocalUriProvider>,
180-
) -> Result<Self, RegistrationError> {
177+
pub async fn new(transport: Arc<T>, uri_provider: Arc<P>) -> Result<Self, RegistrationError> {
181178
let response_listener = Arc::new(ResponseListener {
182179
pending_requests: Mutex::new(HashMap::new()),
183180
});
@@ -204,7 +201,7 @@ impl InMemoryRpcClient {
204201
}
205202

206203
#[async_trait]
207-
impl RpcClient for InMemoryRpcClient {
204+
impl<T: UTransport, P: LocalUriProvider> RpcClient for InMemoryRpcClient<T, P> {
208205
async fn invoke_method(
209206
&self,
210207
method: UUri,
@@ -282,7 +279,7 @@ mod tests {
282279

283280
use crate::{utransport::MockTransport, StaticUriProvider, UMessageBuilder, UPriority, UUri};
284281

285-
fn new_uri_provider() -> Arc<dyn LocalUriProvider> {
282+
fn new_uri_provider() -> Arc<StaticUriProvider> {
286283
Arc::new(StaticUriProvider::new("", 0x0005, 0x02))
287284
}
288285

src/communication/in_memory_rpc_server.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ use crate::{
2828

2929
use super::{RegistrationError, RequestHandler, RpcServer, ServiceInvocationError, UPayload};
3030

31-
struct RequestListener {
31+
struct RequestListener<T: UTransport> {
3232
request_handler: Arc<dyn RequestHandler>,
33-
transport: Arc<dyn UTransport>,
33+
transport: Arc<T>,
3434
}
3535

36-
impl RequestListener {
36+
impl<T: UTransport> RequestListener<T> {
3737
async fn process_valid_request(&self, resource_id: u16, request_message: UMessage) {
3838
let transport_clone = self.transport.clone();
3939
let request_handler_clone = self.request_handler.clone();
@@ -93,7 +93,7 @@ impl RequestListener {
9393
}
9494

9595
#[async_trait]
96-
impl UListener for RequestListener {
96+
impl<T: UTransport> UListener for RequestListener<T> {
9797
async fn on_receive(&self, msg: UMessage) {
9898
if msg.is_request() {
9999
// cannot fail because inbound messages are validated at the transport layer already
@@ -117,15 +117,15 @@ impl UListener for RequestListener {
117117
/// the given request handler and registered with the underlying transport. The listener is also
118118
/// mapped to the endpoint's method resource ID in order to prevent registration of multiple
119119
/// request handlers for the same method.
120-
pub struct InMemoryRpcServer {
121-
transport: Arc<dyn UTransport>,
122-
uri_provider: Arc<dyn LocalUriProvider>,
120+
pub struct InMemoryRpcServer<T, P> {
121+
transport: Arc<T>,
122+
uri_provider: Arc<P>,
123123
request_listeners: tokio::sync::Mutex<HashMap<u16, Arc<dyn UListener>>>,
124124
}
125125

126-
impl InMemoryRpcServer {
126+
impl<T: UTransport, P: LocalUriProvider> InMemoryRpcServer<T, P> {
127127
/// Creates a new RPC server for a given transport.
128-
pub fn new(transport: Arc<dyn UTransport>, uri_provider: Arc<dyn LocalUriProvider>) -> Self {
128+
pub fn new(transport: Arc<T>, uri_provider: Arc<P>) -> Self {
129129
InMemoryRpcServer {
130130
transport,
131131
uri_provider,
@@ -161,7 +161,7 @@ impl InMemoryRpcServer {
161161
}
162162

163163
#[async_trait]
164-
impl RpcServer for InMemoryRpcServer {
164+
impl<T: UTransport + 'static, P: LocalUriProvider> RpcServer for InMemoryRpcServer<T, P> {
165165
async fn register_endpoint(
166166
&self,
167167
origin_filter: Option<&UUri>,
@@ -244,7 +244,7 @@ mod tests {
244244
UAttributes, UCode, UUri, UUID,
245245
};
246246

247-
fn new_uri_provider() -> Arc<dyn LocalUriProvider> {
247+
fn new_uri_provider() -> Arc<StaticUriProvider> {
248248
Arc::new(StaticUriProvider::new("", 0x0005, 0x02))
249249
}
250250

0 commit comments

Comments
 (0)