Skip to content

Commit 9effa73

Browse files
committed
Rollup merge of rust-lang#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
2 parents f2efa79 + 3fff2d9 commit 9effa73

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/libcore/mem.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ pub unsafe fn transmute_copy<T, U>(src: &T) -> U {
836836
///
837837
/// See the `discriminant` function in this module for more information.
838838
#[stable(feature = "discriminant_value", since = "1.21.0")]
839-
pub struct Discriminant<T>(u64, PhantomData<*const T>);
839+
pub struct Discriminant<T>(u64, PhantomData<fn() -> T>);
840840

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

src/libcore/tests/mem.rs

+16
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,19 @@ fn test_transmute() {
121121
}
122122
}
123123

124+
#[test]
125+
#[allow(dead_code)]
126+
fn test_discriminant_send_sync() {
127+
enum Regular {
128+
A,
129+
B(i32)
130+
}
131+
enum NotSendSync {
132+
A(*const i32)
133+
}
134+
135+
fn is_send_sync<T: Send + Sync>() { }
136+
137+
is_send_sync::<Discriminant<Regular>>();
138+
is_send_sync::<Discriminant<NotSendSync>>();
139+
}

0 commit comments

Comments
 (0)