2018-09-06 12:41:12 +00:00
|
|
|
//@ run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(dead_code)]
|
2018-09-06 12:41:12 +00:00
|
|
|
|
2019-09-20 10:22:06 +00:00
|
|
|
use std::mem::ManuallyDrop;
|
|
|
|
|
|
|
|
union MaybeItem<T: Iterator> {
|
|
|
|
elem: ManuallyDrop<T::Item>,
|
2016-08-26 16:23:42 +00:00
|
|
|
none: (),
|
|
|
|
}
|
|
|
|
|
2019-07-03 10:35:02 +00:00
|
|
|
union U<A, B> where A: Copy, B: Copy {
|
2016-08-26 16:23:42 +00:00
|
|
|
a: A,
|
|
|
|
b: B,
|
|
|
|
}
|
|
|
|
|
2019-07-03 10:35:02 +00:00
|
|
|
unsafe fn union_transmute<A, B>(a: A) -> B where A: Copy, B: Copy {
|
2020-10-04 20:24:14 +00:00
|
|
|
U { a }.b
|
2016-08-26 16:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
unsafe {
|
|
|
|
let b = union_transmute::<(u8, u8), u16>((1, 1));
|
|
|
|
assert_eq!(b, (1 << 8) + 1);
|
|
|
|
|
|
|
|
let v: Vec<u8> = vec![1, 2, 3];
|
|
|
|
let mut i = v.iter();
|
|
|
|
i.next();
|
2019-09-20 10:22:06 +00:00
|
|
|
let mi = MaybeItem::<std::slice::Iter<_>> { elem: ManuallyDrop::new(i.next().unwrap()) };
|
|
|
|
assert_eq!(**mi.elem, 2);
|
2016-08-26 16:23:42 +00:00
|
|
|
}
|
|
|
|
}
|