1 // Copyright 2012-2015 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.
11 //! Test for `boxed` mod.
15 use core::result::Result::{Err, Ok};
16 use core::clone::Clone;
23 fn test_owned_clone() {
25 let b: Box<i32> = a.clone();
29 #[derive(PartialEq, Eq)]
34 let a = Box::new(8) as Box<dyn Any>;
35 let b = Box::new(Test) as Box<dyn Any>;
37 match a.downcast::<i32>() {
39 assert!(a == Box::new(8));
43 match b.downcast::<Test>() {
45 assert!(a == Box::new(Test));
50 let a = Box::new(8) as Box<dyn Any>;
51 let b = Box::new(Test) as Box<dyn Any>;
53 assert!(a.downcast::<Box<Test>>().is_err());
54 assert!(b.downcast::<Box<i32>>().is_err());
59 let a = Box::new(8) as Box<dyn Any>;
60 let b = Box::new(Test) as Box<dyn Any>;
61 let a_str = format!("{:?}", a);
62 let b_str = format!("{:?}", b);
63 assert_eq!(a_str, "Any");
64 assert_eq!(b_str, "Any");
66 static EIGHT: usize = 8;
67 static TEST: Test = Test;
68 let a = &EIGHT as &dyn Any;
69 let b = &TEST as &dyn Any;
70 let s = format!("{:?}", a);
72 let s = format!("{:?}", b);
78 fn homura<T: Deref<Target = i32>>(_: T) {}
79 homura(Box::new(765));
85 let p = Box::into_raw(x);
89 let y = Box::from_raw(p);
98 fn set(&mut self, value: u32);
104 fn get(&self) -> u32 {
108 fn set(&mut self, value: u32) {
113 let x: Box<dyn Foo> = Box::new(Bar(17));
114 let p = Box::into_raw(x);
116 assert_eq!(17, (*p).get());
118 let y: Box<dyn Foo> = Box::from_raw(p);
119 assert_eq!(19, y.get());
125 let slice: &[f64] = &[-1.0, 0.0, 1.0, f64::INFINITY];
126 let boxed: Box<[f64]> = Box::from(slice);
127 assert_eq!(&*boxed, slice)
132 let slice: &[i64] = &[i64::MIN, -2, -1, 0, 1, 2, i64::MAX];
133 let boxed: Box<[i64]> = Box::from(slice);
134 assert_eq!(&*boxed, slice)
139 let s = "Hello, world!";
140 let boxed: Box<str> = Box::from(s);
141 assert_eq!(&*boxed, s)