Skip to content

Relax Sync bound on Fut required on sse::upgrade#647

Merged
Fishrock123 merged 2 commits intohttp-rs:masterfrom
rubendura:sse-upgrade-relax-sync-bound
Jul 28, 2020
Merged

Relax Sync bound on Fut required on sse::upgrade#647
Fishrock123 merged 2 commits intohttp-rs:masterfrom
rubendura:sse-upgrade-relax-sync-bound

Conversation

@rubendura
Copy link
Copy Markdown
Contributor

@rubendura rubendura commented Jul 15, 2020

Description

Relax Sync bound on Fut required on sse::upgrade

Motivation and Context

async_std::spawn doesn't seem to require a Sync bound on any Futures
used on it. tide::sse::upgrade will just spawn the Future returned by
the Fn parameter. With this in mind, the Sync bound on that returned
Future seems unnecessary.

This, in my experience, seems to conflict with any usages of async-trait within that future, since async-trait related futures don't appear to be Sync. The main README in the async-trait repo seems to suggest the same

Async fns get transformed into methods that return Pin<Box<dyn Future + Send + 'async_trait>> and delegate to a private async freestanding function.

How Has This Been Tested?

The code compiles and my project is happy. Not sure how to test this, but I'm open to suggestions!

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

async_std::spawn doesn't seem to require a Sync bound on any Futures
used on it. tide::sse::upgrade will just spawn the Future returned by
the Fn parameter. With this in mind, the Sync bound on that returned
Future seems unnecessary.
@jbr jbr requested a review from yoshuawuyts July 15, 2020 00:23
Copy link
Copy Markdown
Member

@jbr jbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in discord, this seems reasonable to me. The only thing we do with this future is hand it to async_std::task::spawn, so if it doesn't care about Sync-ness, maybe we don't need to either?

This commit also removes a PhantomData field from the SseEndpoint
struct, since it doesn't seem to be required and it was turning the type
!Sync. Removing it make the type Sync again, and allows us to remove the
Sync bound from the returned Fut from the handler function.
@rubendura
Copy link
Copy Markdown
Contributor Author

I pushed an extra change making sse::endpoint accept a handler returning a non-Sync Future. This is mostly the same change I applied to sse::upgrade.

In order to do so I had to remove an apparently unnecessary PhantomData in SseEndpoint capturing the Fut type being made non-Sync in this last commit, such that SseEndpoint stays Sync, while the handler can return a non-Sync Future (since that'll only be spawned and that doesn't require the Sync bound.

There's still a PhantomData<State> in the SseEndpoint type which I don't fully understand why it's needed. Removing it makes the compiler complain that the State generic parameter is unused. However, that parameter is in fact used within the F type which is stored in the SseEndpoint type. Is this a compiler bug? (latest stable 1.45.0)
Also curiously removing the PhantomData<State> from the type makes the compiler also complain about the Fut type being unused. Can someone shed some light on what's going on there?

@rubendura rubendura requested a review from jbr July 19, 2020 23:56
Copy link
Copy Markdown
Member

@Fishrock123 Fishrock123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I 100% understand but you seem to need it and it compiles so I'm going to call it ok and worst comes to worst if something goes wrong then we can always revert it.

@Fishrock123 Fishrock123 merged commit 9329f88 into http-rs:master Jul 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants