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) }
);
101 fn test_transmute() {
105 impl Foo
for isize {}
107 let a
= box 100isize
as Box
<dyn Foo
>;
109 let x
: ::core
::raw
::TraitObject
= transmute(a
);
110 assert
!(*(x
.data
as *const isize) == 100);
111 let _x
: Box
<dyn Foo
> = transmute(x
);
115 assert_eq
!(transmute
::<_
, Vec
<u8>>("L".to_string()), [76]);
121 fn test_discriminant_send_sync() {
130 fn is_send_sync
<T
: Send
+ Sync
>() {}
132 is_send_sync
::<Discriminant
<Regular
>>();
133 is_send_sync
::<Discriminant
<NotSendSync
>>();
137 fn assume_init_good() {
138 const TRUE
: bool
= unsafe { MaybeUninit::<bool>::new(true).assume_init() }
;
144 fn uninit_array_assume_init() {
145 let mut array
: [MaybeUninit
<i16>; 5] = MaybeUninit
::uninit_array();
152 let array
= unsafe { MaybeUninit::array_assume_init(array) }
;
154 assert_eq
!(array
, [3, 1, 4, 1, 5]);
156 let [] = unsafe { MaybeUninit::<!>::array_assume_init([]) }
;
160 fn uninit_write_slice() {
161 let mut dst
= [MaybeUninit
::new(255); 64];
164 assert_eq
!(MaybeUninit
::write_slice(&mut dst
, &src
), &src
);
168 #[should_panic(expected = "source slice length (32) does not match destination slice length (64)")]
169 fn uninit_write_slice_panic_lt() {
170 let mut dst
= [MaybeUninit
::uninit(); 64];
173 MaybeUninit
::write_slice(&mut dst
, &src
);
177 #[should_panic(expected = "source slice length (128) does not match destination slice length (64)")]
178 fn uninit_write_slice_panic_gt() {
179 let mut dst
= [MaybeUninit
::uninit(); 64];
182 MaybeUninit
::write_slice(&mut dst
, &src
);
186 fn uninit_clone_from_slice() {
187 let mut dst
= [MaybeUninit
::new(255); 64];
190 assert_eq
!(MaybeUninit
::write_slice_cloned(&mut dst
, &src
), &src
);
194 #[should_panic(expected = "destination and source slices have different lengths")]
195 fn uninit_write_slice_cloned_panic_lt() {
196 let mut dst
= [MaybeUninit
::uninit(); 64];
199 MaybeUninit
::write_slice_cloned(&mut dst
, &src
);
203 #[should_panic(expected = "destination and source slices have different lengths")]
204 fn uninit_write_slice_cloned_panic_gt() {
205 let mut dst
= [MaybeUninit
::uninit(); 64];
208 MaybeUninit
::write_slice_cloned(&mut dst
, &src
);
212 #[cfg(panic = "unwind")]
213 fn uninit_write_slice_cloned_mid_panic() {
216 enum IncrementOrPanic
{
222 impl Clone
for IncrementOrPanic
{
223 fn clone(&self) -> Self {
225 Self::Increment(rc
) => Self::Increment(rc
.clone()),
226 Self::ExpectedPanic
=> panic
!("expected panic on clone"),
227 Self::UnexpectedPanic
=> panic
!("unexpected panic on clone"),
232 let rc
= Rc
::new(());
235 MaybeUninit
::uninit(),
236 MaybeUninit
::uninit(),
237 MaybeUninit
::uninit(),
238 MaybeUninit
::uninit(),
242 IncrementOrPanic
::Increment(rc
.clone()),
243 IncrementOrPanic
::Increment(rc
.clone()),
244 IncrementOrPanic
::ExpectedPanic
,
245 IncrementOrPanic
::UnexpectedPanic
,
248 let err
= panic
::catch_unwind(panic
::AssertUnwindSafe(|| {
249 MaybeUninit
::write_slice_cloned(&mut dst
, &src
);
255 Ok(_
) => unreachable
!(),
258 .downcast
::<&'
static str>()
259 .and_then(|s
| if *s
== "expected panic on clone" { Ok(s) }
else { Err(s) }
)
260 .unwrap_or_else(|p
| panic
::resume_unwind(p
));
262 assert_eq
!(Rc
::strong_count(&rc
), 1)
268 fn uninit_write_slice_cloned_no_drop() {
274 panic
!("dropped a bomb! kaboom")
278 let mut dst
= [MaybeUninit
::uninit()];
281 MaybeUninit
::write_slice_cloned(&mut dst
, &src
);
287 fn uninit_const_assume_init_read() {
288 const FOO
: u32 = unsafe { MaybeUninit::new(42).assume_init_read() }
;