2014-07-04 16:54:23 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-03-22 15:13:15 -05:00
|
|
|
|
2015-03-05 20:33:58 -06:00
|
|
|
#![feature(core)]
|
2014-07-04 16:54:23 -05:00
|
|
|
|
2014-12-11 21:29:24 -06:00
|
|
|
extern crate core;
|
|
|
|
|
|
|
|
use core::nonzero::NonZero;
|
2014-07-04 16:54:23 -05:00
|
|
|
use std::mem::size_of;
|
2014-12-05 13:52:38 -06:00
|
|
|
use std::rc::Rc;
|
|
|
|
use std::sync::Arc;
|
2014-07-04 16:54:23 -05:00
|
|
|
|
2015-02-12 09:29:52 -06:00
|
|
|
trait Trait { fn dummy(&self) { } }
|
2014-07-04 16:54:23 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Functions
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<fn(isize)>(), size_of::<Option<fn(isize)>>());
|
|
|
|
assert_eq!(size_of::<extern "C" fn(isize)>(), size_of::<Option<extern "C" fn(isize)>>());
|
2014-07-04 16:54:23 -05:00
|
|
|
|
|
|
|
// Slices - &str / &[T] / &mut [T]
|
|
|
|
assert_eq!(size_of::<&str>(), size_of::<Option<&str>>());
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<&[isize]>(), size_of::<Option<&[isize]>>());
|
|
|
|
assert_eq!(size_of::<&mut [isize]>(), size_of::<Option<&mut [isize]>>());
|
2014-07-04 16:54:23 -05:00
|
|
|
|
|
|
|
// Traits - Box<Trait> / &Trait / &mut Trait
|
|
|
|
assert_eq!(size_of::<Box<Trait>>(), size_of::<Option<Box<Trait>>>());
|
|
|
|
assert_eq!(size_of::<&Trait>(), size_of::<Option<&Trait>>());
|
|
|
|
assert_eq!(size_of::<&mut Trait>(), size_of::<Option<&mut Trait>>());
|
|
|
|
|
2014-10-02 00:10:09 -05:00
|
|
|
// Pointers - Box<T>
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<Box<isize>>(), size_of::<Option<Box<isize>>>());
|
2014-07-04 16:54:23 -05:00
|
|
|
|
2014-09-11 20:58:01 -05:00
|
|
|
// The optimization can't apply to raw pointers
|
2015-03-25 19:06:52 -05:00
|
|
|
assert!(size_of::<Option<*const isize>>() != size_of::<*const isize>());
|
|
|
|
assert!(Some(0 as *const isize).is_some()); // Can't collapse None to null
|
2014-09-11 20:58:01 -05:00
|
|
|
|
2014-12-05 13:41:28 -06:00
|
|
|
struct Foo {
|
2015-03-25 19:06:52 -05:00
|
|
|
_a: Box<isize>
|
2014-12-05 13:41:28 -06:00
|
|
|
}
|
2015-03-25 19:06:52 -05:00
|
|
|
struct Bar(Box<isize>);
|
2014-12-05 13:41:28 -06:00
|
|
|
|
|
|
|
// Should apply through structs
|
|
|
|
assert_eq!(size_of::<Foo>(), size_of::<Option<Foo>>());
|
|
|
|
assert_eq!(size_of::<Bar>(), size_of::<Option<Bar>>());
|
|
|
|
// and tuples
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<(u8, Box<isize>)>(), size_of::<Option<(u8, Box<isize>)>>());
|
2014-12-05 13:41:28 -06:00
|
|
|
// and fixed-size arrays
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<[Box<isize>; 1]>(), size_of::<Option<[Box<isize>; 1]>>());
|
2014-12-05 13:41:28 -06:00
|
|
|
|
2014-12-05 13:52:38 -06:00
|
|
|
// Should apply to NonZero
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<NonZero<usize>>(), size_of::<Option<NonZero<usize>>>());
|
2014-12-05 13:52:38 -06:00
|
|
|
assert_eq!(size_of::<NonZero<*mut i8>>(), size_of::<Option<NonZero<*mut i8>>>());
|
|
|
|
|
|
|
|
// Should apply to types that use NonZero internally
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(size_of::<Vec<isize>>(), size_of::<Option<Vec<isize>>>());
|
|
|
|
assert_eq!(size_of::<Arc<isize>>(), size_of::<Option<Arc<isize>>>());
|
|
|
|
assert_eq!(size_of::<Rc<isize>>(), size_of::<Option<Rc<isize>>>());
|
2014-12-05 13:52:38 -06:00
|
|
|
|
|
|
|
// Should apply to types that have NonZero transitively
|
|
|
|
assert_eq!(size_of::<String>(), size_of::<Option<String>>());
|
|
|
|
|
2014-07-04 16:54:23 -05:00
|
|
|
}
|