Rollup merge of #45095 - bluss:discriminant-send-sync, r=alexcrichton

Ensure std::mem::Discriminant is Send + Sync

`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
This commit is contained in:
kennytm 2017-10-10 20:08:23 +08:00
commit 9effa73286
No known key found for this signature in database
GPG key ID: FEF6C8051D0E013C
2 changed files with 17 additions and 1 deletions

View file

@ -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.

View file

@ -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>>();
}