Skip to content

Commit 45885b1

Browse files
authored
Make RuntimePlugin & RuntimePlugins Send/Sync (#2771)
## Motivation and Context We will eventually want to store additional runtime plugins on fluent builders, clients, etc. To make this work, the RuntimePlugin trait needs to assert Send/Sync ## Description Small tweaks + test on the RuntimePlugin trait ## Testing CI ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 07bd832 commit 45885b1

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/OperationGenerator.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ open class OperationGenerator(
5151
"""
5252
pub(crate) fn register_default_runtime_plugins(
5353
runtime_plugins: #{RuntimePlugins},
54-
operation: #{Box}<dyn #{RuntimePlugin} + #{Send} + #{Sync}>,
54+
operation: impl #{RuntimePlugin} + 'static,
5555
handle: #{Arc}<crate::client::Handle>,
5656
config_override: #{Option}<crate::config::Builder>,
5757
) -> #{RuntimePlugins} {
@@ -180,7 +180,7 @@ open class OperationGenerator(
180180
) -> #{RuntimePlugins} {
181181
#{register_default_runtime_plugins}(
182182
runtime_plugins,
183-
#{Box}::new(Self::new()) as _,
183+
Self::new(),
184184
handle,
185185
config_override
186186
)

rust-runtime/aws-smithy-runtime-api/src/client/runtime_plugin.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
use crate::client::interceptors::InterceptorRegistrar;
77
use aws_smithy_types::config_bag::{ConfigBag, FrozenLayer};
88
use std::fmt::Debug;
9+
use std::sync::Arc;
910

1011
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
11-
pub type BoxRuntimePlugin = Box<dyn RuntimePlugin + Send + Sync>;
1212

1313
/// RuntimePlugin Trait
1414
///
1515
/// A RuntimePlugin is the unit of configuration for augmenting the SDK with new behavior
1616
///
1717
/// Runtime plugins can set configuration and register interceptors.
18-
pub trait RuntimePlugin: Debug {
18+
pub trait RuntimePlugin: Debug + Send + Sync {
1919
fn config(&self) -> Option<FrozenLayer> {
2020
None
2121
}
@@ -25,40 +25,44 @@ pub trait RuntimePlugin: Debug {
2525
}
2626
}
2727

28-
impl RuntimePlugin for BoxRuntimePlugin {
28+
#[derive(Debug, Clone)]
29+
struct SharedRuntimePlugin(Arc<dyn RuntimePlugin>);
30+
31+
impl SharedRuntimePlugin {
32+
fn new(plugin: impl RuntimePlugin + 'static) -> Self {
33+
Self(Arc::new(plugin))
34+
}
35+
}
36+
37+
impl RuntimePlugin for SharedRuntimePlugin {
2938
fn config(&self) -> Option<FrozenLayer> {
30-
self.as_ref().config()
39+
self.0.config()
3140
}
3241

3342
fn interceptors(&self, interceptors: &mut InterceptorRegistrar) {
34-
self.as_ref().interceptors(interceptors)
43+
self.0.interceptors(interceptors)
3544
}
3645
}
3746

38-
#[derive(Default)]
47+
#[derive(Default, Clone, Debug)]
3948
pub struct RuntimePlugins {
40-
client_plugins: Vec<BoxRuntimePlugin>,
41-
operation_plugins: Vec<BoxRuntimePlugin>,
49+
client_plugins: Vec<SharedRuntimePlugin>,
50+
operation_plugins: Vec<SharedRuntimePlugin>,
4251
}
4352

4453
impl RuntimePlugins {
4554
pub fn new() -> Self {
4655
Default::default()
4756
}
4857

49-
pub fn with_client_plugin(
50-
mut self,
51-
plugin: impl RuntimePlugin + Send + Sync + 'static,
52-
) -> Self {
53-
self.client_plugins.push(Box::new(plugin));
58+
pub fn with_client_plugin(mut self, plugin: impl RuntimePlugin + 'static) -> Self {
59+
self.client_plugins.push(SharedRuntimePlugin::new(plugin));
5460
self
5561
}
5662

57-
pub fn with_operation_plugin(
58-
mut self,
59-
plugin: impl RuntimePlugin + Send + Sync + 'static,
60-
) -> Self {
61-
self.operation_plugins.push(Box::new(plugin));
63+
pub fn with_operation_plugin(mut self, plugin: impl RuntimePlugin + 'static) -> Self {
64+
self.operation_plugins
65+
.push(SharedRuntimePlugin::new(plugin));
6266
self
6367
}
6468

@@ -106,4 +110,10 @@ mod tests {
106110
fn can_add_runtime_plugin_implementors_to_runtime_plugins() {
107111
RuntimePlugins::new().with_client_plugin(SomeStruct);
108112
}
113+
114+
#[test]
115+
fn runtime_plugins_are_send_sync() {
116+
fn assert_send_sync<T: Send + Sync>() {}
117+
assert_send_sync::<RuntimePlugins>();
118+
}
109119
}

0 commit comments

Comments
 (0)