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

Improve core::task::TaskObj #51532

Merged
merged 1 commit into from
Jun 13, 2018
Merged

Conversation

MajorBreakfast
Copy link
Contributor

  • Rename UnsafePoll to UnsafeTask to avoid confusion with Poll
  • Rename TaskObj::from_poll_task() to TaskObj::new()
  • Rename TaskObj's poll and drop fields to poll_fn and drop_fn
  • Implement Future for TaskObj. Reason: It's a custom trait object for a future, so it should implement future
  • Remove unsafe impl Sync for TaskObj. I don't think we need it. Was this safe? UnsafeTask only requires to implement Send

@cramertj
@aturon

@rust-highfive
Copy link
Collaborator

r? @withoutboats

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 13, 2018
@MajorBreakfast
Copy link
Contributor Author

r? @cramertj

@@ -467,7 +469,6 @@ impl fmt::Debug for TaskObj {
}

unsafe impl Send for TaskObj {}
unsafe impl Sync for TaskObj {}
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this? We don't particularly need it, but it's totally sound AFAICT, and it will give the correct behavior were we to add more non-mutating methods in the future (in addition to the current Debug::fmt).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can put it in again, but is it safe? trait UnsafeTask: Send + 'static <-- no Sync

Copy link
Contributor Author

@MajorBreakfast MajorBreakfast Jun 13, 2018

Choose a reason for hiding this comment

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

According to the comment above TaskObj is supposed to be approximately equivalent to Box<Future<Output = ()> + Send> which has also no Sync bound.

Copy link
Member

Choose a reason for hiding this comment

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

UnsafeTask only has &mut methods-- those are only used for mutating the inner task. Since no &self methods are called, it's trivially Sync.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TaskObj is just a workaround for Box<Future<Output = ()> + Send + 'static> which throws an error, but if it worked, it wouldn't be be Sync. I think the workaround should match what we really want but can't get as much as possible. IMO it feels wrong to implement Sync. I agree with your reasoning why it can be considered safe, though.

Here's the error for Box<Future<Output = ()> + Send + 'static>:

let boxed: Box<Future<Output = ()> + Send + 'static> = ...
// error[E0038]: the trait `std::future::Future` cannot be made into an object
// note: method `poll` has a non-standard `self` type

Also a playground example that shows Box<Future<Output = ()> + Send + 'static> is not Sync

I believe the more conservative approach would be to leave it out.

Copy link
Member

@cramertj cramertj Jun 13, 2018

Choose a reason for hiding this comment

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

That is certainly the more conservative approach :).

I'm fine with leaving it out if you feel strongly about it-- I don't think it really matters significantly either way. Currently the only &self method is Debug::fmt, which is thread-safe, but if we someday decide to add a thread-locked &self method you're right that we wouldn't be able to do so.

Copy link
Contributor Author

@MajorBreakfast MajorBreakfast Jun 13, 2018

Choose a reason for hiding this comment

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

No strong feelings, because, as you say, it's safe. My recommendation is, however, to leave it out.

For adding impl Sync: We can because it's safe.
Against adding impl Sync: More conservative, we don't need it, and it approximates Box<Future...> which is !Sync more closely
=> My conclusion: Don't add it

@cramertj
Copy link
Member

@MajorBreakfast This looks good to me modulo the nit about removing Sync. Thanks!

@cramertj
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Jun 13, 2018

📌 Commit 2177378 has been approved by cramertj

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 13, 2018
@bors
Copy link
Contributor

bors commented Jun 13, 2018

⌛ Testing commit 2177378 with merge b907d96...

bors added a commit that referenced this pull request Jun 13, 2018
Improve core::task::TaskObj

- Rename `UnsafePoll` to `UnsafeTask` to avoid confusion with `Poll`
- Rename `TaskObj::from_poll_task()` to `TaskObj::new()`
- Rename `TaskObj`'s `poll` and `drop` fields to `poll_fn` and `drop_fn`
- Implement `Future` for `TaskObj`. Reason: It's a custom trait object for a future, so it should implement future
- Remove `unsafe impl Sync` for `TaskObj`. I don't think we need it. Was this safe? `UnsafeTask` only requires to implement `Send`

@cramertj
@aturon
@bors
Copy link
Contributor

bors commented Jun 13, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: cramertj
Pushing b907d96 to master...

@bors bors merged commit 2177378 into rust-lang:master Jun 13, 2018
@MajorBreakfast
Copy link
Contributor Author

🥂My first merged "real" Rust PR :-)

@MajorBreakfast MajorBreakfast deleted the task-future branch June 14, 2018 00:22
@cramertj
Copy link
Member

@MajorBreakfast That's awesome! Congratulations!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants