1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 use core::nonzero::NonZero;
15 use std::mem::size_of;
19 trait Trait { fn dummy(&self) { } }
23 assert_eq!(size_of::<fn(int)>(), size_of::<Option<fn(int)>>());
24 assert_eq!(size_of::<extern "C" fn(int)>(), size_of::<Option<extern "C" fn(int)>>());
26 // Slices - &str / &[T] / &mut [T]
27 assert_eq!(size_of::<&str>(), size_of::<Option<&str>>());
28 assert_eq!(size_of::<&[int]>(), size_of::<Option<&[int]>>());
29 assert_eq!(size_of::<&mut [int]>(), size_of::<Option<&mut [int]>>());
31 // Traits - Box<Trait> / &Trait / &mut Trait
32 assert_eq!(size_of::<Box<Trait>>(), size_of::<Option<Box<Trait>>>());
33 assert_eq!(size_of::<&Trait>(), size_of::<Option<&Trait>>());
34 assert_eq!(size_of::<&mut Trait>(), size_of::<Option<&mut Trait>>());
37 assert_eq!(size_of::<Box<int>>(), size_of::<Option<Box<int>>>());
39 // The optimization can't apply to raw pointers
40 assert!(size_of::<Option<*const int>>() != size_of::<*const int>());
41 assert!(Some(0 as *const int).is_some()); // Can't collapse None to null
48 // Should apply through structs
49 assert_eq!(size_of::<Foo>(), size_of::<Option<Foo>>());
50 assert_eq!(size_of::<Bar>(), size_of::<Option<Bar>>());
52 assert_eq!(size_of::<(u8, Box<int>)>(), size_of::<Option<(u8, Box<int>)>>());
53 // and fixed-size arrays
54 assert_eq!(size_of::<[Box<int>; 1]>(), size_of::<Option<[Box<int>; 1]>>());
56 // Should apply to NonZero
57 assert_eq!(size_of::<NonZero<uint>>(), size_of::<Option<NonZero<uint>>>());
58 assert_eq!(size_of::<NonZero<*mut i8>>(), size_of::<Option<NonZero<*mut i8>>>());
60 // Should apply to types that use NonZero internally
61 assert_eq!(size_of::<Vec<int>>(), size_of::<Option<Vec<int>>>());
62 assert_eq!(size_of::<Arc<int>>(), size_of::<Option<Arc<int>>>());
63 assert_eq!(size_of::<Rc<int>>(), size_of::<Option<Rc<int>>>());
65 // Should apply to types that have NonZero transitively
66 assert_eq!(size_of::<String>(), size_of::<Option<String>>());