summaryrefslogtreecommitdiffstats
path: root/third_party/rust/pin-project/tests/unsafe_unpin.rs
blob: 74673ca1262e89b1555c0ef3d37efcce66088025 (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
#![warn(unsafe_code)]
#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(dead_code)]

use pin_project::{pin_project, UnsafeUnpin};
use std::{marker::PhantomPinned, pin::Pin};

fn is_unpin<T: Unpin>() {}

#[pin_project(UnsafeUnpin)]
pub struct Blah<T, U> {
    field1: U,
    #[pin]
    field2: T,
}

#[allow(unsafe_code)]
unsafe impl<T: Unpin, U> UnsafeUnpin for Blah<T, U> {}

#[pin_project(UnsafeUnpin)]
pub struct OverlappingLifetimeNames<'pin, T, U> {
    #[pin]
    field1: T,
    field2: U,
    field3: &'pin (),
}

#[allow(unsafe_code)]
unsafe impl<T: Unpin, U> UnsafeUnpin for OverlappingLifetimeNames<'_, T, U> {}

#[test]
fn unsafe_unpin() {
    is_unpin::<Blah<(), PhantomPinned>>();
    is_unpin::<OverlappingLifetimeNames<'_, (), ()>>();
}

#[test]
fn trivial_bounds() {
    #[pin_project(UnsafeUnpin)]
    pub struct NotImplementUnsafUnpin {
        #[pin]
        field: PhantomPinned,
    }
}

#[test]
fn dst() {
    #[pin_project(UnsafeUnpin)]
    pub struct A<T: ?Sized> {
        x: T,
    }

    #[pin_project(UnsafeUnpin)]
    pub struct B<T: ?Sized> {
        #[pin]
        x: T,
    }

    #[pin_project(UnsafeUnpin)]
    pub struct C<T: ?Sized>(T);

    #[pin_project(UnsafeUnpin)]
    pub struct D<T: ?Sized>(#[pin] T);
}

#[test]
fn test() {
    let mut x = OverlappingLifetimeNames { field1: 0, field2: 1, field3: &() };
    let x = Pin::new(&mut x);
    let y = x.as_ref().project_ref();
    let _: Pin<&u8> = y.field1;
    let _: &u8 = y.field2;
    let y = x.project();
    let _: Pin<&mut u8> = y.field1;
    let _: &mut u8 = y.field2;
}