Add tests with *const Rc<Self> and similar self types

This commit is contained in:
Michael Hewson 2017-12-14 18:43:27 +01:00
parent bc0439b388
commit 4cae2c087d
2 changed files with 28 additions and 4 deletions

View File

@ -10,15 +10,28 @@
#![feature(arbitrary_self_types)]
use std::rc::Rc;
struct Foo(String);
impl Foo {
unsafe fn foo(self: *const Self) -> *const str {
(*self).0.as_ref()
}
fn complicated_1(self: *const Rc<Self>) -> &'static str {
"Foo::complicated_1"
}
unsafe fn complicated_2(self: Rc<*const Self>) -> *const str {
(**self).0.as_ref()
}
}
fn main() {
let foo = Foo("abc123".into());
assert_eq!("abc123", unsafe { &*(&foo as *const Foo).foo() });
assert_eq!("Foo::complicated_1", std::ptr::null::<Rc<Foo>>().complicated_1());
let rc = Rc::new(&foo as *const Foo);
assert_eq!("abc123", unsafe { &*rc.complicated_2()});
}

View File

@ -16,6 +16,10 @@ trait Foo {
fn foo(self: *const Self) -> &'static str;
unsafe fn bar(self: *const Self) -> i64;
unsafe fn complicated(self: *const *const Self) -> i64 where Self: Sized {
(*self).bar()
}
}
impl Foo for i32 {
@ -39,21 +43,28 @@ impl Foo for u32 {
}
fn main() {
let foo_i32 = ptr::null::<i32>() as *const Foo;
let foo_u32 = ptr::null::<u32>() as *const Foo;
let null_i32 = ptr::null::<i32>() as *const Foo;
let null_u32 = ptr::null::<u32>() as *const Foo;
assert_eq!("I'm an i32!", foo_i32.foo());
assert_eq!("I'm a u32!", foo_u32.foo());
assert_eq!("I'm an i32!", null_i32.foo());
assert_eq!("I'm a u32!", null_u32.foo());
let bar_i32 = 5i32;
let bar_i32_thin = &bar_i32 as *const i32;
assert_eq!("I'm an i32!", bar_i32_thin.foo());
assert_eq!(5, unsafe { bar_i32_thin.bar() });
assert_eq!(5, unsafe { (&bar_i32_thin as *const *const i32).complicated() });
let bar_i32_fat = bar_i32_thin as *const Foo;
assert_eq!("I'm an i32!", bar_i32_fat.foo());
assert_eq!(5, unsafe { bar_i32_fat.bar() });
let bar_u32 = 18u32;
let bar_u32_thin = &bar_u32 as *const u32;
assert_eq!("I'm a u32!", bar_u32_thin.foo());
assert_eq!(18, unsafe { bar_u32_thin.bar() });
assert_eq!(18, unsafe { (&bar_u32_thin as *const *const u32).complicated() });
let bar_u32_fat = bar_u32_thin as *const Foo;
assert_eq!("I'm a u32!", bar_u32_fat.foo());
assert_eq!(18, unsafe { bar_u32_fat.bar() });
}