aboutsummaryrefslogtreecommitdiff
path: root/tests/project_replace.rs
blob: 9c8a5aba295508ab14f622b16fb52c4dce1ba19b (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
91
92
93
94
95
96
97
98
#![warn(rust_2018_idioms, single_use_lifetimes)]
#![allow(dead_code)]

use pin_project::{pin_project, project_replace};
use std::{marker::PhantomData, pin::Pin};

#[project_replace] // Nightly does not need a dummy attribute to the function.
#[test]
fn project_replace_stmt_expr() {
    #[pin_project(Replace)]
    struct Struct<T, U> {
        #[pin]
        field1: T,
        field2: U,
    }

    let mut s = Struct { field1: 1, field2: 2 };

    #[project_replace]
    let Struct { field1, field2 } =
        Pin::new(&mut s).project_replace(Struct { field1: 42, field2: 43 });

    let _x: PhantomData<i32> = field1;

    let y: i32 = field2;
    assert_eq!(y, 2);

    // tuple struct

    #[pin_project(Replace)]
    struct TupleStruct<T, U>(#[pin] T, U);

    let mut s = TupleStruct(1, 2);

    #[project_replace]
    let TupleStruct(x, y) = Pin::new(&mut s).project_replace(TupleStruct(42, 43));

    let _x: PhantomData<i32> = x;
    let y: i32 = y;
    assert_eq!(y, 2);

    #[pin_project(Replace)]
    enum Enum<A, B, C, D> {
        Variant1(#[pin] A, B),
        Variant2 {
            #[pin]
            field1: C,
            field2: D,
        },
        None,
    }

    let mut e = Enum::Variant1(1, 2);

    let e = Pin::new(&mut e).project_replace(Enum::None);

    #[project_replace]
    match e {
        Enum::Variant1(x, y) => {
            let _x: PhantomData<i32> = x;
            let y: i32 = y;
            assert_eq!(y, 2);
        }
        Enum::Variant2 { field1, field2 } => {
            let _x: PhantomData<i32> = field1;
            let _y: i32 = field2;
            panic!()
        }
        Enum::None => panic!(),
    }
}

#[project_replace]
#[test]
fn combine() {
    #[pin_project(Replace)]
    enum Enum<A> {
        V1(#[pin] A),
        V2,
    }

    let mut x = Enum::V1(1);
    #[project]
    match Pin::new(&mut x).project() {
        Enum::V1(_) => {}
        Enum::V2 => unreachable!(),
    }
    #[project_ref]
    match Pin::new(&x).project_ref() {
        Enum::V1(_) => {}
        Enum::V2 => unreachable!(),
    }
    #[project_replace]
    match Pin::new(&mut x).project_replace(Enum::V2) {
        Enum::V1(_) => {}
        Enum::V2 => unreachable!(),
    }
}