ferrite_session/internal/base/context/
impls.rs

1use super::traits::*;
2use crate::internal::{
3  base::protocol::{
4    ClientEndpoint,
5    Protocol,
6  },
7  functional::nat::{
8    S,
9    Z,
10  },
11};
12
13impl<A> SealedSlot for A where A: Protocol {}
14
15impl<A> Slot for A
16where
17  A: Protocol,
18{
19  type Endpoint = ClientEndpoint<A>;
20}
21
22impl SealedSlot for Empty {}
23
24impl Slot for Empty
25{
26  type Endpoint = ();
27}
28
29impl<C, A1, A2> ContextLens<(A1, C), A1, A2> for Z
30where
31  A1: Slot,
32  A2: Slot,
33  C: Context,
34{
35  type Deleted = C;
36  type Target = (A2, C);
37
38  fn extract_source(
39    ctx: (A1::Endpoint, C::Endpoints)
40  ) -> (A1::Endpoint, C::Endpoints)
41  {
42    ctx
43  }
44
45  fn insert_target(
46    p: A2::Endpoint,
47    r: C::Endpoints,
48  ) -> (A2::Endpoint, C::Endpoints)
49  {
50    (p, r)
51  }
52}
53
54impl<B, A1, A2, C, N> ContextLens<(B, C), A1, A2> for S<N>
55where
56  B: Slot,
57  A1: Slot,
58  A2: Slot,
59  C: Context,
60  N: ContextLens<C, A1, A2>,
61{
62  type Deleted = (B, N::Deleted);
63  type Target = (B, N::Target);
64
65  fn extract_source(
66    (p, r1): (B::Endpoint, C::Endpoints)
67  ) -> (
68    A1::Endpoint,
69    (B::Endpoint, <N::Deleted as Context>::Endpoints),
70  )
71  {
72    let (q, r2) = N::extract_source(r1);
73
74    (q, (p, r2))
75  }
76
77  fn insert_target(
78    q: A2::Endpoint,
79    (p, r1): (B::Endpoint, <N::Deleted as Context>::Endpoints),
80  ) -> (B::Endpoint, <N::Target as Context>::Endpoints)
81  {
82    let r2 = N::insert_target(q, r1);
83
84    (p, r2)
85  }
86}
87
88impl SealedContext for () {}
89
90impl Context for ()
91{
92  type Endpoints = ();
93  type Length = Z;
94}
95
96impl SealedEmptyContext for () {}
97
98impl EmptyContext for ()
99{
100  fn empty_values() {}
101}
102
103impl<R> SealedEmptyContext for (Empty, R) where R: EmptyContext {}
104
105impl<R> EmptyContext for (Empty, R)
106where
107  R: EmptyContext,
108{
109  fn empty_values() -> ((), R::Endpoints)
110  {
111    ((), R::empty_values())
112  }
113}
114
115impl<P, R> SealedContext for (P, R) {}
116
117impl<P, R> Context for (P, R)
118where
119  P: Slot,
120  R: Context,
121{
122  type Endpoints = (P::Endpoint, R::Endpoints);
123  type Length = S<R::Length>;
124}
125
126impl<R: Context> AppendContext<R> for ()
127{
128  type Appended = R;
129
130  fn append_context(
131    _: (),
132    r: <R as Context>::Endpoints,
133  ) -> <R as Context>::Endpoints
134  {
135    r
136  }
137
138  fn split_context(
139    r: <R as Context>::Endpoints
140  ) -> ((), <R as Context>::Endpoints)
141  {
142    ((), r)
143  }
144}
145
146impl<P, R, S> AppendContext<S> for (P, R)
147where
148  P: Slot,
149  R: Context,
150  S: Context,
151  R: AppendContext<S>,
152{
153  type Appended = (P, <R as AppendContext<S>>::Appended);
154
155  fn append_context(
156    (p, r): (P::Endpoint, R::Endpoints),
157    s: <S as Context>::Endpoints,
158  ) -> (<P as Slot>::Endpoint, <R::Appended as Context>::Endpoints)
159  {
160    (p, <R as AppendContext<S>>::append_context(r, s))
161  }
162
163  fn split_context(
164    (p, r): (P::Endpoint, <R::Appended as Context>::Endpoints)
165  ) -> (<(P, R) as Context>::Endpoints, <S as Context>::Endpoints)
166  {
167    let (r2, s) = R::split_context(r);
168
169    ((p, r2), s)
170  }
171}