File size: 3,240 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this

use anyhow::Result;
use turbo_tasks::{State, Vc};
use turbo_tasks_testing::{Registration, register, run};

static REGISTRATION: Registration = register!();

#[tokio::test]
async fn hidden_mutate() {
    run(&REGISTRATION, || async {
        let input = create_input().resolve().await?;
        input.await?.state.set(1);
        let changing_value = compute(input);
        assert_eq!(changing_value.await?.value, 1);

        let changing_value_resolved = changing_value.resolve().await?;
        let read_input = read_input(changing_value_resolved);
        let static_immutable = immutable_fn(changing_value_resolved);
        let read_self = changing_value_resolved.read_self();
        let static_immutable_self = changing_value_resolved.immutable_self_fn();
        assert_eq!(*read_input.await?, 1);
        assert_eq!(*static_immutable.await?, 42);
        assert_eq!(*read_self.await?, 1);
        assert_eq!(*static_immutable_self.await?, 42);

        println!("changing input");
        input.await?.state.set(10);
        assert_eq!(changing_value.strongly_consistent().await?.value, 10);
        assert_eq!(*read_input.strongly_consistent().await?, 10);
        assert_eq!(*static_immutable.strongly_consistent().await?, 42);
        assert_eq!(*read_self.strongly_consistent().await?, 10);
        assert_eq!(*static_immutable_self.strongly_consistent().await?, 42);

        println!("changing input");
        input.await?.state.set(5);
        assert_eq!(changing_value.strongly_consistent().await?.value, 5);
        assert_eq!(*read_input.strongly_consistent().await?, 5);
        assert_eq!(*static_immutable.strongly_consistent().await?, 42);
        assert_eq!(*read_self.strongly_consistent().await?, 5);
        assert_eq!(*static_immutable_self.strongly_consistent().await?, 42);

        anyhow::Ok(())
    })
    .await
    .unwrap();
}

#[turbo_tasks::value]
struct ChangingInput {
    state: State<u32>,
}

#[turbo_tasks::value]
struct Value {
    value: u32,
}

#[turbo_tasks::function]
async fn create_input() -> Result<Vc<ChangingInput>> {
    println!("create_input()");
    Ok(ChangingInput {
        state: State::new(0),
    }
    .cell())
}

#[turbo_tasks::function]
async fn compute(input: Vc<ChangingInput>) -> Result<Vc<Value>> {
    println!("compute()");
    let input = input.await?;
    let value = input.state.get();
    Ok(Value { value: *value }.cell())
}

#[turbo_tasks::function]
async fn read_input(input: Vc<Value>) -> Result<Vc<u32>> {
    println!("read_input()");
    let value = input.await?;
    Ok(Vc::cell(value.value))
}

#[turbo_tasks::function]
fn immutable_fn(input: Vc<Value>) -> Vc<u32> {
    let _ = input;
    println!("immutable_fn()");
    Vc::cell(42)
}

#[turbo_tasks::value_impl]
impl Value {
    #[turbo_tasks::function]
    fn read_self(&self) -> Vc<u32> {
        println!("read_self()");
        Vc::cell(self.value)
    }

    #[turbo_tasks::function]
    fn immutable_self_fn(self: Vc<Value>) -> Vc<u32> {
        let _ = self;
        println!("immutable_self_fn()");
        Vc::cell(42)
    }
}