Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ptr;
- use std::pin::Pin;
- use std::mem::ManuallyDrop;
- struct IntrusiveList {
- prev: *mut IntrusiveList,
- next: *mut IntrusiveList,
- }
- impl IntrusiveList {
- fn new() -> IntrusiveList {
- IntrusiveList {
- prev: ptr::null_mut(),
- next: ptr::null_mut(),
- }
- }
- fn append(mut self: Pin<&mut IntrusiveList>, mut other: Pin<&mut IntrusiveList>) {
- self.next = &mut *other;
- other.prev = &mut *self;
- }
- fn pin(&mut self) -> Pin<&mut IntrusiveList> {
- unsafe {
- Pin::new_unchecked(self)
- }
- }
- }
- impl Drop for IntrusiveList {
- fn drop(&mut self) {
- unsafe {
- if let Some(prev) = self.prev.as_mut() {
- prev.next = self.next;
- }
- if let Some(next) = self.next.as_mut() {
- next.prev = self.prev;
- }
- }
- }
- }
- fn foo(pinned_a: Pin<&mut IntrusiveList>) {
- let mut b = IntrusiveList::new();
- // Uncomment this line and run in Miri
- // let mut b = ManuallyDrop::new(b);
- let pinned_b = b.pin();
- pinned_a.append(pinned_b)
- }
- fn main() {
- let mut a = IntrusiveList::new();
- let pinned_a = a.pin();
- foo(pinned_a);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement