-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add LocalTaskObj
to core::task
#51814
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
Conversation
src/libcore/task/mod.rs
Outdated
pub use self::poll::Poll; | ||
|
||
mod spawn_error; | ||
pub use self::spawn_error::{SpawnErrorKind, SpawnObjError}; |
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.
IMO the errors don't deserve their own file-- I'd move them in with the executor (since that's what uses them).
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.
Done
src/libcore/task/spawn_error.rs
Outdated
fn from(error: SpawnObjError) -> SpawnLocalObjError { | ||
unsafe { | ||
// Safety: Both structs have the same memory layout | ||
mem::transmute::<SpawnObjError, SpawnLocalObjError>(error) |
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.
No need to transmute
here-- just move the fields over.
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.
Will it have the same performance?
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.
Yup.
src/libcore/task/spawn_error.rs
Outdated
/// To make this operation safe one has to ensure that the `UnsafeTask` | ||
/// instance from which the `LocalTaskObj` stored inside was created | ||
/// actually implements `Send`. | ||
pub unsafe fn as_spawn_obj_error(self) -> SpawnObjError { |
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.
What is this for? This seems like a pretty big footgun.
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.
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.
You don't need anything unsafe
to implement that. You can implement spawn_obj
by inlining the body of spawn_local_obj
and using into
only if upgrade
succeeds.
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.
You mean copy paste?
src/libcore/task/task.rs
Outdated
@@ -30,7 +30,7 @@ unsafe impl Send for TaskObj {} | |||
impl TaskObj { | |||
/// Create a `TaskObj` from a custom trait object representation. | |||
#[inline] | |||
pub fn new<T: UnsafeTask>(t: T) -> TaskObj { | |||
pub fn new<T: UnsafeTask + Send>(t: T) -> TaskObj { |
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.
Rather than repeating all the logic between TaskObj
and LocalTaskObj
, I'd make TaskObj
into a single-field wrapper around LocalTaskObj
that just adds Send
-ability at the cost of requiring Send
to construct (in new
).
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.
The current way was inspired by the way it is done for LocalWaker
& Waker
. You're right. A single field struct is nicer
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.
Waker
and LocalWaker
are a bit different because they actually call different functions, but it's possible they could be combined further as well.
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.
Ok good point. Anyhow I think the current waker code is good. At closer inspection it really shows that a lot of considerations went into it.
I removed the conversions for |
@MajorBreakfast The fields of Edit: oh, you meant for the errors. I see you kept https://github.com/rust-lang/rust/pull/51814/files#diff-c756fc15162cb40ebeb296a1699a84b5R44 |
r=me with travis passing. Thanks! |
@bors r+ |
📌 Commit b39ea1d has been approved by |
☀️ Test successful - status-appveyor, status-travis |
libcore/task.rs
into submodulesLocalTaskObj
andSpawnLocalObjError
(-> Commit for this)Note: To make reviewing easy, both actions have their own commit
r? @cramertj