Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![feature(unsize, coerce_unsized, ptr_internals)]
- use std::fmt::Debug;
- use std::sync::{Arc,RwLock};
- use std::marker::Unsize;
- use std::ops::CoerceUnsized;
- trait MyTrait: Debug {
- //
- }
- #[derive(Debug)]
- struct MyStruct {
- //
- }
- impl MyTrait for MyStruct {}
- #[derive(Debug)]
- struct MyBox<T: ?Sized> {
- data: Box<RwLock<T>>
- }
- impl<T: ?Sized> MyBox<T> {
- fn new(t: T) -> MyBox<T> where T: Sized {
- MyBox {
- data: Box::new(RwLock::new(t))
- }
- }
- }
- #[derive(Debug)]
- struct MyArc<T: ?Sized> {
- arc: Arc<MyBox<T>>
- }
- impl<T: ?Sized> MyArc<T> {
- fn new(t: T) -> MyArc<T> where T: Sized {
- MyArc {
- arc: Arc::new(MyBox::new(t))
- }
- }
- }
- // ok.
- impl <U: ?Sized, T: Unsize<U> + ?Sized> CoerceUnsized<MyBox<U>> for MyBox<T> { }
- // not ok. why?
- impl <U: ?Sized, T: Unsize<U> + ?Sized> CoerceUnsized<MyArc<U>> for MyArc<T> { }
- fn main() {
- // I can cast MyStruct to MyTrait here with standard boxes.
- let b1 = Box::new(MyStruct {}) as Box<MyTrait>;
- // I can still do it here.
- let b2 = MyArc::new(MyStruct {}) as MyArc<MyTrait>;
- // But now it fails... why? (in fact it fails on the impl of CU).
- let b3 = MyArc::new(MyStruct {}) as MyArc<MyTrait>;
- println!("{:?}", b1);
- println!("{:?}", b2);
- println!("{:?}", b3);
- }
Add Comment
Please, Sign In to add comment