Guest User

Untitled

a guest
May 20th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #![feature(pin, arbitrary_self_types, optin_builtin_traits)]
  2.  
  3. use std::marker::Unpin;
  4. use std::mem::PinMut;
  5.  
  6. struct SelfReferential {
  7. foo_ptr: *const u8,
  8. x: u8,
  9. }
  10.  
  11. impl !Unpin for SelfReferential {}
  12.  
  13. impl SelfReferential {
  14. pub fn new(x: u8) -> Self {
  15. SelfReferential {
  16. foo_ptr: ::std::ptr::null(),
  17. x,
  18. }
  19. }
  20. pub fn reference_self(self: PinMut<Self>) {
  21. let foo_ptr = &self.x as *const _;
  22. unsafe { PinMut::get_mut(self) }.foo_ptr = foo_ptr;
  23. }
  24. pub fn deref_ptr(&self) {
  25. if self.foo_ptr != ::std::ptr::null() {
  26. println!("{:?}", unsafe { *self.foo_ptr });
  27. }
  28. }
  29. }
  30.  
  31. fn main() {
  32. let x = SelfReferential::new(x);
  33. x.deref_ptr();
  34. pin!(x);
  35. x.reference_self();
  36. x.deref_ptr();
  37. }
Add Comment
Please, Sign In to add comment