SHARE
TWEET

Untitled

a guest Sep 17th, 2019 99 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::ptr;
  2. use std::pin::Pin;
  3. use std::mem::ManuallyDrop;
  4.  
  5. struct IntrusiveList {
  6.     prev: *mut IntrusiveList,
  7.     next: *mut IntrusiveList,
  8. }
  9.  
  10. impl IntrusiveList {
  11.     fn new() -> IntrusiveList {
  12.         IntrusiveList {
  13.             prev: ptr::null_mut(),
  14.             next: ptr::null_mut(),
  15.         }
  16.     }
  17.    
  18.     fn append(mut self: Pin<&mut IntrusiveList>, mut other: Pin<&mut IntrusiveList>) {
  19.         self.next = &mut *other;
  20.         other.prev = &mut *self;
  21.     }
  22.    
  23.     fn pin(&mut self) -> Pin<&mut IntrusiveList> {
  24.         unsafe {
  25.             Pin::new_unchecked(self)
  26.         }
  27.     }
  28. }
  29.  
  30. impl Drop for IntrusiveList {
  31.     fn drop(&mut self) {
  32.         unsafe {
  33.             if let Some(prev) = self.prev.as_mut() {
  34.                 prev.next = self.next;
  35.             }
  36.             if let Some(next) = self.next.as_mut() {
  37.                 next.prev = self.prev;
  38.             }
  39.         }
  40.     }
  41. }
  42.  
  43. fn foo(pinned_a: Pin<&mut IntrusiveList>) {
  44.     let mut b = IntrusiveList::new();
  45.     // Uncomment this line and run in Miri
  46.     // let mut b = ManuallyDrop::new(b);
  47.     let pinned_b = b.pin();
  48.     pinned_a.append(pinned_b)
  49. }
  50.  
  51. fn main() {
  52.     let mut a = IntrusiveList::new();
  53.     let pinned_a = a.pin();
  54.     foo(pinned_a);
  55. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top