postgres/
lazy_pin.rs

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
use std::pin::Pin;

pub(crate) struct LazyPin<T> {
    value: Box<T>,
    pinned: bool,
}

impl<T> LazyPin<T> {
    pub fn new(value: T) -> LazyPin<T> {
        LazyPin {
            value: Box::new(value),
            pinned: false,
        }
    }

    pub fn pinned(&mut self) -> Pin<&mut T> {
        self.pinned = true;
        unsafe { Pin::new_unchecked(&mut *self.value) }
    }

    pub fn into_unpinned(self) -> Option<T> {
        if self.pinned {
            None
        } else {
            Some(*self.value)
        }
    }
}