Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update request modifier fn type to impl send and sync #770

Merged
merged 4 commits into from
Dec 17, 2023
Merged
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
1 change: 1 addition & 0 deletions rumqttc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ To update your code simply remove `Key::ECC()` or `Key::RSA()` from the initiali

### Fixed
- Lowered the MSRV to 1.64.0
- Request modifier function should be Send and Sync and removed unnecessary Box

### Security

Expand Down
28 changes: 15 additions & 13 deletions rumqttc/src/v5/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ pub enum Request {
}

#[cfg(feature = "websocket")]
type RequestModifierFn =
dyn Fn(http::Request<()>) -> Pin<Box<dyn Future<Output = http::Request<()>>>>;
type RequestModifierFn = Arc<
dyn Fn(http::Request<()>) -> Pin<Box<dyn Future<Output = http::Request<()>> + Send>>
+ Send
+ Sync,
>;

// TODO: Should all the options be exposed as public? Drawback
// would be loosing the ability to panic when the user options
Expand Down Expand Up @@ -100,7 +103,7 @@ pub struct MqttOptions {
/// The server may set its own maximum inflight limit, the smaller of the two will be used.
outgoing_inflight_upper_limit: Option<u16>,
#[cfg(feature = "websocket")]
request_modifier: Option<Arc<Box<RequestModifierFn>>>,
request_modifier: Option<RequestModifierFn>,
}

impl MqttOptions {
Expand Down Expand Up @@ -201,21 +204,20 @@ impl MqttOptions {
#[cfg(feature = "websocket")]
pub fn set_request_modifier<F, O>(&mut self, request_modifier: F) -> &mut Self
where
F: Fn(http::Request<()>) -> O + 'static,
O: IntoFuture<Output = http::Request<()>>,
F: Fn(http::Request<()>) -> O + Send + Sync + 'static,
O: IntoFuture<Output = http::Request<()>> + 'static,
O::IntoFuture: Send,
{
let request_modifier = Arc::new(Box::new(request_modifier));
self.request_modifier = Some(Arc::new(Box::new(move |request| {
Box::pin({
let request_modifier = Arc::clone(&request_modifier);
async move { request_modifier(request).into_future().await }
})
})));
self.request_modifier = Some(Arc::new(move |request| {
let request_modifier = request_modifier(request).into_future();
Box::pin(request_modifier)
}));

self
}

#[cfg(feature = "websocket")]
pub fn request_modifier(&self) -> Option<Arc<Box<RequestModifierFn>>> {
pub fn request_modifier(&self) -> Option<RequestModifierFn> {
self.request_modifier.clone()
}

Expand Down