     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37

    use std::sync::atomic::{AtomicPtr, Ordering};
    use std::ptr;

    unsafe impl<T: Send> Send for AtomicOption<T> {}
    unsafe impl<T: Send> Sync for AtomicOption<T> {}

    pub struct AtomicOption<T> {
        inner: AtomicPtr<T>,
    }

    impl<T> AtomicOption<T> {
        pub fn new() -> AtomicOption<T> {
            AtomicOption { inner: AtomicPtr::new(ptr::null_mut()) }
        }

        fn swap_inner(&self, ptr: *mut T, order: Ordering) -> Option<Box<T>> {
            let old = self.inner.swap(ptr, order);
            if old.is_null() {
                None
            } else {
                Some(unsafe { Box::from_raw(old) })
            }
        }

        // allows re-use of allocation
        pub fn swap_box(&self, t: Box<T>, order: Ordering) -> Option<Box<T>> {
            self.swap_inner(Box::into_raw(t), order)
        }

        pub fn swap(&self, t: T, order: Ordering) -> Option<T> {
            self.swap_box(Box::new(t), order).map(|old| *old)
        }

        pub fn take(&self, order: Ordering) -> Option<T> {
            self.swap_inner(ptr::null_mut(), order).map(|old| *old)
        }
    }

# Help

## Keyboard Shortcuts

?
  Show this help dialog
S
  Focus the search field
⇤
  Move up in search results
⇥
  Move down in search results
⏎
  Go to active search result

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)