3 #[cfg(panic = "unwind")]
8 assert_eq
!(size_of
::<u8>(), 1);
9 assert_eq
!(size_of
::<u16>(), 2);
10 assert_eq
!(size_of
::<u32>(), 4);
11 assert_eq
!(size_of
::<u64>(), 8);
15 #[cfg(target_pointer_width = "16")]
17 assert_eq
!(size_of
::<usize>(), 2);
18 assert_eq
!(size_of
::<*const usize>(), 2);
22 #[cfg(target_pointer_width = "32")]
24 assert_eq
!(size_of
::<usize>(), 4);
25 assert_eq
!(size_of
::<*const usize>(), 4);
29 #[cfg(target_pointer_width = "64")]
31 assert_eq
!(size_of
::<usize>(), 8);
32 assert_eq
!(size_of
::<*const usize>(), 8);
36 fn size_of_val_basic() {
37 assert_eq
!(size_of_val(&1u8), 1);
38 assert_eq
!(size_of_val(&1u16), 2);
39 assert_eq
!(size_of_val(&1u32), 4);
40 assert_eq
!(size_of_val(&1u64), 8);
45 assert_eq
!(align_of
::<u8>(), 1);
46 assert_eq
!(align_of
::<u16>(), 2);
47 assert_eq
!(align_of
::<u32>(), 4);
51 #[cfg(target_pointer_width = "16")]
53 assert_eq
!(align_of
::<usize>(), 2);
54 assert_eq
!(align_of
::<*const usize>(), 2);
58 #[cfg(target_pointer_width = "32")]
60 assert_eq
!(align_of
::<usize>(), 4);
61 assert_eq
!(align_of
::<*const usize>(), 4);
65 #[cfg(target_pointer_width = "64")]
67 assert_eq
!(align_of
::<usize>(), 8);
68 assert_eq
!(align_of
::<*const usize>(), 8);
72 fn align_of_val_basic() {
73 assert_eq
!(align_of_val(&1u8), 1);
74 assert_eq
!(align_of_val(&1u16), 2);
75 assert_eq
!(align_of_val(&1u32), 4);
89 let mut x
= Some("test".to_string());
90 let y
= replace(&mut x
, None
);
96 fn test_transmute_copy() {
97 assert_eq
!(1, unsafe { transmute_copy(&1) }
);
102 fn test_discriminant_send_sync() {
111 fn is_send_sync
<T
: Send
+ Sync
>() {}
113 is_send_sync
::<Discriminant
<Regular
>>();
114 is_send_sync
::<Discriminant
<NotSendSync
>>();
118 fn assume_init_good() {
119 const TRUE
: bool
= unsafe { MaybeUninit::<bool>::new(true).assume_init() }
;
125 fn uninit_array_assume_init() {
126 let mut array
: [MaybeUninit
<i16>; 5] = MaybeUninit
::uninit_array();
133 let array
= unsafe { MaybeUninit::array_assume_init(array) }
;
135 assert_eq
!(array
, [3, 1, 4, 1, 5]);
137 let [] = unsafe { MaybeUninit::<!>::array_assume_init([]) }
;
141 fn uninit_write_slice() {
142 let mut dst
= [MaybeUninit
::new(255); 64];
145 assert_eq
!(MaybeUninit
::write_slice(&mut dst
, &src
), &src
);
149 #[should_panic(expected = "source slice length (32) does not match destination slice length (64)")]
150 fn uninit_write_slice_panic_lt() {
151 let mut dst
= [MaybeUninit
::uninit(); 64];
154 MaybeUninit
::write_slice(&mut dst
, &src
);
158 #[should_panic(expected = "source slice length (128) does not match destination slice length (64)")]
159 fn uninit_write_slice_panic_gt() {
160 let mut dst
= [MaybeUninit
::uninit(); 64];
163 MaybeUninit
::write_slice(&mut dst
, &src
);
167 fn uninit_clone_from_slice() {
168 let mut dst
= [MaybeUninit
::new(255); 64];
171 assert_eq
!(MaybeUninit
::write_slice_cloned(&mut dst
, &src
), &src
);
175 #[should_panic(expected = "destination and source slices have different lengths")]
176 fn uninit_write_slice_cloned_panic_lt() {
177 let mut dst
= [MaybeUninit
::uninit(); 64];
180 MaybeUninit
::write_slice_cloned(&mut dst
, &src
);
184 #[should_panic(expected = "destination and source slices have different lengths")]
185 fn uninit_write_slice_cloned_panic_gt() {
186 let mut dst
= [MaybeUninit
::uninit(); 64];
189 MaybeUninit
::write_slice_cloned(&mut dst
, &src
);
193 #[cfg(panic = "unwind")]
194 fn uninit_write_slice_cloned_mid_panic() {
197 enum IncrementOrPanic
{
203 impl Clone
for IncrementOrPanic
{
204 fn clone(&self) -> Self {
206 Self::Increment(rc
) => Self::Increment(rc
.clone()),
207 Self::ExpectedPanic
=> panic
!("expected panic on clone"),
208 Self::UnexpectedPanic
=> panic
!("unexpected panic on clone"),
213 let rc
= Rc
::new(());
216 MaybeUninit
::uninit(),
217 MaybeUninit
::uninit(),
218 MaybeUninit
::uninit(),
219 MaybeUninit
::uninit(),
223 IncrementOrPanic
::Increment(rc
.clone()),
224 IncrementOrPanic
::Increment(rc
.clone()),
225 IncrementOrPanic
::ExpectedPanic
,
226 IncrementOrPanic
::UnexpectedPanic
,
229 let err
= panic
::catch_unwind(panic
::AssertUnwindSafe(|| {
230 MaybeUninit
::write_slice_cloned(&mut dst
, &src
);
236 Ok(_
) => unreachable
!(),
239 .downcast
::<&'
static str>()
240 .and_then(|s
| if *s
== "expected panic on clone" { Ok(s) }
else { Err(s) }
)
241 .unwrap_or_else(|p
| panic
::resume_unwind(p
));
243 assert_eq
!(Rc
::strong_count(&rc
), 1)
249 fn uninit_write_slice_cloned_no_drop() {
255 panic
!("dropped a bomb! kaboom")
259 let mut dst
= [MaybeUninit
::uninit()];
262 MaybeUninit
::write_slice_cloned(&mut dst
, &src
);
268 fn uninit_const_assume_init_read() {
269 const FOO
: u32 = unsafe { MaybeUninit::new(42).assume_init_read() }
;