Guest User

Untitled

a guest
Jun 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #![feature(unsize, coerce_unsized, ptr_internals)]
  2.  
  3. use std::fmt::Debug;
  4. use std::sync::{Arc,RwLock};
  5. use std::marker::Unsize;
  6. use std::ops::CoerceUnsized;
  7.  
  8. trait MyTrait: Debug {
  9. //
  10. }
  11.  
  12. #[derive(Debug)]
  13. struct MyStruct {
  14. //
  15. }
  16.  
  17. impl MyTrait for MyStruct {}
  18.  
  19. #[derive(Debug)]
  20. struct MyBox<T: ?Sized> {
  21. data: Box<RwLock<T>>
  22. }
  23.  
  24. impl<T: ?Sized> MyBox<T> {
  25. fn new(t: T) -> MyBox<T> where T: Sized {
  26. MyBox {
  27. data: Box::new(RwLock::new(t))
  28. }
  29. }
  30. }
  31.  
  32. #[derive(Debug)]
  33. struct MyArc<T: ?Sized> {
  34. arc: Arc<MyBox<T>>
  35. }
  36.  
  37. impl<T: ?Sized> MyArc<T> {
  38. fn new(t: T) -> MyArc<T> where T: Sized {
  39. MyArc {
  40. arc: Arc::new(MyBox::new(t))
  41. }
  42. }
  43. }
  44.  
  45. // ok.
  46. impl <U: ?Sized, T: Unsize<U> + ?Sized> CoerceUnsized<MyBox<U>> for MyBox<T> { }
  47.  
  48. // not ok. why?
  49. impl <U: ?Sized, T: Unsize<U> + ?Sized> CoerceUnsized<MyArc<U>> for MyArc<T> { }
  50.  
  51. fn main() {
  52. // I can cast MyStruct to MyTrait here with standard boxes.
  53. let b1 = Box::new(MyStruct {}) as Box<MyTrait>;
  54. // I can still do it here.
  55. let b2 = MyArc::new(MyStruct {}) as MyArc<MyTrait>;
  56. // But now it fails... why? (in fact it fails on the impl of CU).
  57. let b3 = MyArc::new(MyStruct {}) as MyArc<MyTrait>;
  58.  
  59. println!("{:?}", b1);
  60. println!("{:?}", b2);
  61. println!("{:?}", b3);
  62. }
Add Comment
Please, Sign In to add comment