diff options
| author | Nick Cameron <ncameron@mozilla.com> | 2015-04-20 17:56:25 +1200 |
|---|---|---|
| committer | Nick Cameron <ncameron@mozilla.com> | 2015-05-13 14:19:51 +1200 |
| commit | db1b14a194cbdbba813aeb4773ee7fa203a40fb1 (patch) | |
| tree | cd8fe5d9585145a2b6da5ffdba8c911dda86e342 | |
| parent | 03d4d5f80ed1d9e168869bdb244e4fef67b7d3d0 (diff) | |
| download | rust-db1b14a194cbdbba813aeb4773ee7fa203a40fb1.tar.gz rust-db1b14a194cbdbba813aeb4773ee7fa203a40fb1.zip | |
Tests for custom coercions
| -rw-r--r-- | src/test/run-pass/dst-coerce-custom.rs | 52 | ||||
| -rw-r--r-- | src/test/run-pass/dst-coerce-rc.rs | 39 |
2 files changed, 91 insertions, 0 deletions
diff --git a/src/test/run-pass/dst-coerce-custom.rs b/src/test/run-pass/dst-coerce-custom.rs new file mode 100644 index 00000000000..aa28ae00e27 --- /dev/null +++ b/src/test/run-pass/dst-coerce-custom.rs @@ -0,0 +1,52 @@ +// Copyright 2015 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. + +// Test a very simple custom DST coercion. + +#![feature(core)] + +use std::ops::CoerceUnsized; +use std::marker::Unsize; + +struct Bar<T: ?Sized> { + x: *const T, +} + +impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Bar<U>> for Bar<T> {} + +trait Baz { + fn get(&self) -> i32; +} + +impl Baz for i32 { + fn get(&self) -> i32 { + *self + } +} + +fn main() { + // Arrays. + let a: Bar<[i32; 3]> = Bar { x: &[1, 2, 3] }; + // This is the actual coercion. + let b: Bar<[i32]> = a; + + unsafe { + assert_eq!((*b.x)[0], 1); + assert_eq!((*b.x)[1], 2); + assert_eq!((*b.x)[2], 3); + } + + // Trait objects. + let a: Bar<i32> = Bar { x: &42 }; + let b: Bar<Baz> = a; + unsafe { + assert_eq!((*b.x).get(), 42); + } +} diff --git a/src/test/run-pass/dst-coerce-rc.rs b/src/test/run-pass/dst-coerce-rc.rs new file mode 100644 index 00000000000..32e7a6279c8 --- /dev/null +++ b/src/test/run-pass/dst-coerce-rc.rs @@ -0,0 +1,39 @@ +// Copyright 2015 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. + +// Test a very simple custom DST coercion. + +#![feature(core)] + +use std::rc::Rc; + +trait Baz { + fn get(&self) -> i32; +} + +impl Baz for i32 { + fn get(&self) -> i32 { + *self + } +} + +fn main() { + let a: Rc<[i32; 3]> = Rc::new([1, 2, 3]); + let b: Rc<[i32]> = a; + assert_eq!(b[0], 1); + assert_eq!(b[1], 2); + assert_eq!(b[2], 3); + + let a: Rc<i32> = Rc::new(42); + let b: Rc<Baz> = a.clone(); + assert_eq!(b.get(), 42); + + let _c = b.clone(); +} |
