-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(body): remove Sync bound for Body::wrap_stream #2187
Conversation
@seanmonstar This is the follow-up to #2159, WDYT? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be useful for users, definitely! Mutex
does add overhead of an additional allocation (std::sync::Mutex
boxes internally due to pthread_mutex_t
requiring a stable address), and the poison stuff...
Well, now that it is proven, the alternative is to just |
I decided to take a shot at the SyncWrapper: rust-lang/rust#71529 |
We could have a |
570693b
to
50efe3b
Compare
@seanmonstar following some discussion on zulip I published sync_wrapper and updated this PR — is this okay? |
Hmm, the test failure is not related to this change AFAICS, and I also cannot reproduce it when running the same command locally … |
Could we inline the type into the module? It seems small enough that we don't need to add the cost of downloading/compiling another dependency... |
We sure could do that, but if we kept the docs then the increase in size of |
The type seems small enough to me to inline. There's costs to having a separate crate, some real (new network calls to download metadata and source, having to link libraries), and perceived (users get grumpy as the dependency count gets bigger). |
Okay, will probably do it tomorrow. |
A stream wrapped into a Body previously needed to implement `Sync` so that the Body type implements this autotrait as well (which is needed due to limitations in async/await). Since a stream only offers one method that is called with an exclusive reference, this type is statically proven to be Sync already. In theory it should be fine to add an `unsafe impl Sync`, but this commit instead adds a SyncWrapper to enlist the compiler’s help in proving that this is (and remains) correct. This makes it easier to construct response bodies for client code.
I pushed the version which has the SyncWrapper in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wonderful!
Note that this was a breaking change (see the PR linked above). |
How is this a breaking change? We've made the bounds less strict, so everything that matched before should match still. |
This comment has been minimized.
This comment has been minimized.
Sure, that would be a breaking change, but that's not what we did. We changed |
Right, I had to take another look. The issue is that this impl changed: https://github.com/hyperium/hyper/pull/2187/files#diff-b032ea26c55c1f4f1c2c32cd030e0bc0L405. |
Hmm, the |
Or asked differently: can you show some compiler error demonstrating what you mean by “breaking change”? That would make it easier for me to grasp. |
|
Oh, this is a limitation of the Rust type system that I wasn’t aware of: the actually implemented From instance covers the needs of the one you ask for, so it should be fine, but rustc doesn’t seem to understand that. |
Out of interest: why is this second part of the bound present? Shouldn’t |
I'm not sure, but it doesn't compile without them 😅. That crate is rather generics-heavy, and it's not impossible that some bounds are unnecessary. |
Summary: Hyper used to have stricter trait bounds for stream bodies, however they were relaxed in hyperium/hyper#2187. Remove the code to spawn a task that simply connected streams up now that hyper is more relaxed. As the spawned task was just polling for data, it shouldn't add any extra computational work onto the task used for a request. This change is basically identical to D27963458 (e5cc9a1). Reviewed By: mitrandir77 Differential Revision: D35931360 fbshipit-source-id: 065fecc77b4ac1218c2be21e0a3639103429c5bc
A stream wrapped into a Body previously needed to implement
Sync
sothat the Body type implements this autotrait as well (which is needed
due to limitations in async/await). Since a stream only offers one
method that is called with an exclusive reference, this type is
statically proven to be Sync already. In theory it should be fine to add
an
unsafe impl Sync
, but this commit instead adds a Mutex that isnever locked to prove that this approach is correct.
This makes it easier to construct response bodies for client code.
For more discussion on this see rust-internals.