summaryrefslogtreecommitdiffstats
path: root/third_party/rust/pin-project/examples/unsafe_unpin-expanded.rs
blob: acb3ee8316fa3d68f289a6db80cd9917b91fd55b (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Original code (./unsafe_unpin.rs):
//
// ```rust
// #![allow(dead_code)]
//
// use pin_project::{pin_project, UnsafeUnpin};
//
// #[pin_project(UnsafeUnpin)]
// pub struct Foo<T, U> {
//     #[pin]
//     pinned: T,
//     unpinned: U,
// }
//
// unsafe impl<T: Unpin, U> UnsafeUnpin for Foo<T, U> {}
//
// fn main() {}
// ```

#![allow(dead_code, unused_imports, unused_parens)]

use pin_project::{pin_project, UnsafeUnpin};

pub struct Foo<T, U> {
    // #[pin]
    pinned: T,
    unpinned: U,
}

#[allow(clippy::mut_mut)] // This lint warns `&mut &mut <ty>`.
#[allow(dead_code)] // This lint warns unused fields/variants.
pub(crate) struct __FooProjection<'pin, T, U> {
    pinned: ::core::pin::Pin<&'pin mut (T)>,
    unpinned: &'pin mut (U),
}
#[allow(dead_code)] // This lint warns unused fields/variants.
pub(crate) struct __FooProjectionRef<'pin, T, U> {
    pinned: ::core::pin::Pin<&'pin (T)>,
    unpinned: &'pin (U),
}

impl<T, U> Foo<T, U> {
    pub(crate) fn project<'pin>(
        self: ::core::pin::Pin<&'pin mut Self>,
    ) -> __FooProjection<'pin, T, U> {
        unsafe {
            let Foo { pinned, unpinned } = self.get_unchecked_mut();
            __FooProjection { pinned: ::core::pin::Pin::new_unchecked(pinned), unpinned }
        }
    }
    pub(crate) fn project_ref<'pin>(
        self: ::core::pin::Pin<&'pin Self>,
    ) -> __FooProjectionRef<'pin, T, U> {
        unsafe {
            let Foo { pinned, unpinned } = self.get_ref();
            __FooProjectionRef { pinned: ::core::pin::Pin::new_unchecked(pinned), unpinned }
        }
    }
}

unsafe impl<T: Unpin, U> UnsafeUnpin for Foo<T, U> {}

#[allow(single_use_lifetimes)]
impl<'pin, T, U> ::core::marker::Unpin for Foo<T, U> where
    ::pin_project::__private::Wrapper<'pin, Self>: ::pin_project::UnsafeUnpin
{
}

// Ensure that struct does not implement `Drop`.
//
// See ./struct-default-expanded.rs for details.
trait FooMustNotImplDrop {}
#[allow(clippy::drop_bounds)]
impl<T: ::core::ops::Drop> FooMustNotImplDrop for T {}
#[allow(single_use_lifetimes)]
impl<T, U> FooMustNotImplDrop for Foo<T, U> {}

// Ensure that it's impossible to use pin projections on a #[repr(packed)] struct.
//
// See ./struct-default-expanded.rs and https://github.com/taiki-e/pin-project/pull/34
// for details.
#[allow(single_use_lifetimes)]
#[allow(non_snake_case)]
#[deny(safe_packed_borrows)]
fn __pin_project_assert_not_repr_packed_Foo<T, U>(val: &Foo<T, U>) {
    &val.pinned;
    &val.unpinned;
}

fn main() {}