File size: 3,174 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
108
109
110
111
use std::{
    any::Any,
    fmt::{Display, Formatter},
};

use hyper::{Method, Uri};
use turbo_tasks::{FxIndexSet, InvalidationReason, InvalidationReasonKind, util::StaticOrArc};

/// Computation was caused by a request to the server.
#[derive(PartialEq, Eq, Hash)]
pub struct ServerRequest {
    pub method: Method,
    pub uri: Uri,
}

impl InvalidationReason for ServerRequest {
    fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
        Some(StaticOrArc::Static(&SERVER_REQUEST_KIND))
    }
}

impl Display for ServerRequest {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", self.method, self.uri.path())
    }
}

/// Invalidation kind for [ServerRequest]
#[derive(PartialEq, Eq, Hash)]
struct ServerRequestKind;

static SERVER_REQUEST_KIND: ServerRequestKind = ServerRequestKind;

impl InvalidationReasonKind for ServerRequestKind {
    fn fmt(
        &self,
        reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
        f: &mut Formatter<'_>,
    ) -> std::fmt::Result {
        let example = reasons
            .into_iter()
            .map(|reason| {
                let reason: &dyn InvalidationReason = &**reason;
                (reason as &dyn Any)
                    .downcast_ref::<ServerRequest>()
                    .unwrap()
            })
            .min_by_key(|reason| reason.uri.path().len())
            .unwrap();
        write!(
            f,
            "{} requests ({} {}, ...)",
            reasons.len(),
            example.method,
            example.uri.path()
        )
    }
}

/// Side effect that was caused by a request to the server.
#[derive(PartialEq, Eq, Hash)]
pub struct ServerRequestSideEffects {
    pub method: Method,
    pub uri: Uri,
}

impl InvalidationReason for ServerRequestSideEffects {
    fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
        Some(StaticOrArc::Static(&SERVER_REQUEST_SIDE_EFFECTS_KIND))
    }
}

impl Display for ServerRequestSideEffects {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "side effects of {} {}", self.method, self.uri.path())
    }
}

/// Invalidation kind for [ServerRequestSideEffects]
#[derive(PartialEq, Eq, Hash)]
struct ServerRequestSideEffectsKind;

static SERVER_REQUEST_SIDE_EFFECTS_KIND: ServerRequestSideEffectsKind =
    ServerRequestSideEffectsKind;

impl InvalidationReasonKind for ServerRequestSideEffectsKind {
    fn fmt(
        &self,
        reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
        f: &mut Formatter<'_>,
    ) -> std::fmt::Result {
        let example = reasons
            .into_iter()
            .map(|reason| {
                let reason: &dyn InvalidationReason = &**reason;
                (reason as &dyn Any)
                    .downcast_ref::<ServerRequestSideEffects>()
                    .unwrap()
            })
            .min_by_key(|reason| reason.uri.path().len())
            .unwrap();
        write!(
            f,
            "side effects of {} requests ({} {}, ...)",
            reasons.len(),
            example.method,
            example.uri.path()
        )
    }
}