Skip to content

Commit 89dd48c

Browse files
committed
Tests for custom coercions
1 parent 9378e3a commit 89dd48c

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 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.
4+
//
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.
10+
11+
// Test a very simple custom DST coercion.
12+
13+
#![feature(core)]
14+
15+
use std::ops::CoerceUnsized;
16+
use std::marker::Unsize;
17+
18+
struct Bar<T: ?Sized> {
19+
x: *const T,
20+
}
21+
22+
impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<Bar<U>> for Bar<T> {}
23+
24+
trait Baz {
25+
fn get(&self) -> i32;
26+
}
27+
28+
impl Baz for i32 {
29+
fn get(&self) -> i32 {
30+
*self
31+
}
32+
}
33+
34+
fn main() {
35+
// Arrays.
36+
let a: Bar<[i32; 3]> = Bar { x: &[1, 2, 3] };
37+
// This is the actual coercion.
38+
let b: Bar<[i32]> = a;
39+
40+
unsafe {
41+
assert_eq!((*b.x)[0], 1);
42+
assert_eq!((*b.x)[1], 2);
43+
assert_eq!((*b.x)[2], 3);
44+
}
45+
46+
// Trait objects.
47+
let a: Bar<i32> = Bar { x: &42 };
48+
let b: Bar<Baz> = a;
49+
unsafe {
50+
assert_eq!((*b.x).get(), 42);
51+
}
52+
}

src/test/run-pass/dst-coerce-rc.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 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.
4+
//
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.
10+
11+
// Test a very simple custom DST coercion.
12+
13+
#![feature(core)]
14+
15+
use std::rc::Rc;
16+
17+
trait Baz {
18+
fn get(&self) -> i32;
19+
}
20+
21+
impl Baz for i32 {
22+
fn get(&self) -> i32 {
23+
*self
24+
}
25+
}
26+
27+
fn main() {
28+
let a: Rc<[i32; 3]> = Rc::new([1, 2, 3]);
29+
let b: Rc<[i32]> = a;
30+
assert_eq!(b[0], 1);
31+
assert_eq!(b[1], 2);
32+
assert_eq!(b[2], 3);
33+
34+
let a: Rc<i32> = Rc::new(42);
35+
let b: Rc<Baz> = a.clone();
36+
assert_eq!(b.get(), 42);
37+
38+
let _c = b.clone();
39+
}

0 commit comments

Comments
 (0)