Skip to content

Commit

Permalink
core: Ensure std::mem::Discriminant is Send + Sync
Browse files Browse the repository at this point in the history
`PhantomData<*const T>` has the implication of Send / Syncness following
the *const T type, but the discriminant should always be Send and Sync.

Use `PhantomData<fn() -> T>` which has the same variance in T, but is Send + Sync
  • Loading branch information
bluss committed Oct 7, 2017
1 parent 2146c61 commit 3fff2d9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libcore/mem.rs
Expand Up @@ -836,7 +836,7 @@ pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
///
/// See the `discriminant` function in this module for more information.
#[stable(feature = "discriminant_value", since = "1.21.0")]
pub struct Discriminant<T>(u64, PhantomData<*const T>);
pub struct Discriminant<T>(u64, PhantomData<fn() -> T>);

// N.B. These trait implementations cannot be derived because we don't want any bounds on T.

Expand Down
16 changes: 16 additions & 0 deletions src/libcore/tests/mem.rs
Expand Up @@ -121,3 +121,19 @@ fn test_transmute() {
}
}

#[test]
#[allow(dead_code)]
fn test_discriminant_send_sync() {
enum Regular {
A,
B(i32)
}
enum NotSendSync {
A(*const i32)
}

fn is_send_sync<T: Send + Sync>() { }

is_send_sync::<Discriminant<Regular>>();
is_send_sync::<Discriminant<NotSendSync>>();
}

0 comments on commit 3fff2d9

Please sign in to comment.