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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
use crate::{call_pattern::DynResponder, value_chain::ValueChain, MockFn, Responder};
use std::borrow::Borrow;

/// Trait for responding to function calls.
pub trait Respond {
    /// The type of the response, as stored temporarily inside Unimock.
    type Type: 'static;
}

/// Trait for values that can be converted into responses.
///
/// This can be implemented by types that do not implement `Clone`.
pub trait IntoResponseOnce<R: Respond> {
    // Convert this type into the output type.
    #[doc(hidden)]
    fn into_response(self) -> <R as Respond>::Type;

    // Convert this type directly into a responder that can respond (at least) once.
    #[doc(hidden)]
    fn into_once_responder<F: MockFn<Response = R>>(self) -> Responder;
}

/// Trait for `Clone` values which can be converted into a reusable multi-value responder.
pub trait IntoResponseClone<R: Respond>: IntoResponseOnce<R> {
    #[doc(hidden)]
    fn into_clone_responder<F: MockFn<Response = R>>(self) -> Responder;
}

/// Trait that describes the output of a mocked function, and how responses are converted into that type.
///
/// The trait uses the 'u lifetime, which is the lifetime of unimock itself.
/// This way it's possible to borrow values stored inside the instance.
pub trait Output<'u, R: Respond> {
    /// The type of the output compatible with the function signature.
    type Type;

    #[doc(hidden)]
    fn from_response(value: R::Type, value_chain: &'u ValueChain) -> Self::Type;

    #[doc(hidden)]
    fn try_borrow_response(value: &'u R::Type) -> Result<Self::Type, SignatureError>;
}

#[doc(hidden)]
pub enum SignatureError {
    NotOwned,
    NotBorrowed,
}

#[doc(hidden)]
pub struct Owned<T>(std::marker::PhantomData<T>);

// This type describes a function response that is a reference borrowed from `Self`.
#[doc(hidden)]
pub struct Borrowed<T: ?Sized + 'static>(std::marker::PhantomData<T>);

#[doc(hidden)]
pub struct StaticRef<T: ?Sized>(std::marker::PhantomData<T>);

// This type describes a function response that is a mix of owned and borrowed data.
//
// The typical example is `Option<&T>`.
#[doc(hidden)]
pub struct Mixed<T>(std::marker::PhantomData<T>);

type BoxBorrow<T> = Box<dyn Borrow<T> + Send + Sync>;

mod owned {
    use super::*;

    impl<T: 'static> Respond for Owned<T> {
        type Type = T;
    }

    impl<I, T: Send + Sync + 'static> IntoResponseOnce<Owned<T>> for I
    where
        I: Into<T>,
    {
        fn into_response(self) -> <Owned<T> as Respond>::Type {
            self.into()
        }

        fn into_once_responder<F: MockFn<Response = Owned<T>>>(self) -> Responder {
            let output = <I as IntoResponseOnce<Owned<T>>>::into_response(self);
            Responder(DynResponder::new_cell::<F>(output))
        }
    }

    impl<I, T: Clone + Send + Sync + 'static> IntoResponseClone<Owned<T>> for I
    where
        I: Into<T>,
    {
        fn into_clone_responder<F: MockFn<Response = Owned<T>>>(self) -> Responder {
            let output = <I as IntoResponseOnce<Owned<T>>>::into_response(self);
            Responder(DynResponder::new_clone_cell::<F>(output))
        }
    }

    impl<'u, T: 'static> Output<'u, Self> for Owned<T> {
        type Type = T;

        fn from_response(value: <Self as Respond>::Type, _: &'u ValueChain) -> Self::Type {
            value
        }

        fn try_borrow_response(
            _: &'u <Self as Respond>::Type,
        ) -> Result<Self::Type, SignatureError> {
            Err(SignatureError::NotOwned)
        }
    }
}

mod borrowed {
    use super::*;

    impl<T: ?Sized + 'static> Respond for Borrowed<T> {
        type Type = Box<dyn Borrow<T> + Send + Sync>;
    }

    impl<T0, T> IntoResponseOnce<Borrowed<T>> for T0
    where
        T0: Borrow<T> + Send + Sync + 'static,
        T: ?Sized + 'static,
    {
        fn into_response(self) -> <Borrowed<T> as Respond>::Type {
            Box::new(self)
        }

        fn into_once_responder<F: MockFn<Response = Borrowed<T>>>(self) -> Responder {
            let output = <T0 as IntoResponseOnce<Borrowed<T>>>::into_response(self);
            Responder(DynResponder::new_borrow::<F>(output))
        }
    }

    impl<T0, T> IntoResponseClone<Borrowed<T>> for T0
    where
        T0: Borrow<T> + Send + Sync + 'static,
        T: ?Sized + 'static,
    {
        fn into_clone_responder<F: MockFn<Response = Borrowed<T>>>(self) -> Responder {
            <T0 as IntoResponseOnce<Borrowed<T>>>::into_once_responder::<F>(self)
        }
    }

    impl<'u, T: ?Sized + 'static> Output<'u, Self> for Borrowed<T> {
        type Type = &'u T;

        fn from_response(
            value: <Borrowed<T> as Respond>::Type,
            value_chain: &'u ValueChain,
        ) -> Self::Type {
            let value_ref = value_chain.add(value);

            value_ref.as_ref().borrow()
        }

        fn try_borrow_response(
            value: &'u <Borrowed<T> as Respond>::Type,
        ) -> Result<Self::Type, SignatureError> {
            Ok(value.as_ref().borrow())
        }
    }
}

mod static_ref {
    use super::*;

    impl<T: ?Sized + 'static> Respond for StaticRef<T> {
        type Type = &'static T;
    }

    impl<T: ?Sized + Send + Sync + 'static> IntoResponseOnce<StaticRef<T>> for &'static T {
        fn into_response(self) -> <StaticRef<T> as Respond>::Type {
            self
        }

        fn into_once_responder<F: MockFn<Response = StaticRef<T>>>(self) -> Responder {
            let output = <Self as IntoResponseOnce<StaticRef<T>>>::into_response(self);
            Responder(DynResponder::new_borrow::<F>(output))
        }
    }

    impl<T: ?Sized + Send + Sync + 'static> IntoResponseClone<StaticRef<T>> for &'static T {
        fn into_clone_responder<F: MockFn<Response = StaticRef<T>>>(self) -> Responder {
            <Self as IntoResponseOnce<StaticRef<T>>>::into_once_responder::<F>(self)
        }
    }

    impl<'u, T: ?Sized + 'static> Output<'u, Self> for StaticRef<T> {
        type Type = &'static T;

        fn from_response(value: <Self as Respond>::Type, _: &ValueChain) -> Self::Type {
            value
        }

        fn try_borrow_response(
            value: &'u <Self as Respond>::Type,
        ) -> Result<Self::Type, SignatureError> {
            Ok(*value)
        }
    }
}

mod mixed_option {
    use super::*;

    type Mix<T> = Mixed<Option<&'static T>>;

    impl<T: ?Sized + 'static> Respond for Mix<T> {
        type Type = Option<BoxBorrow<T>>;
    }

    impl<T0, T> IntoResponseOnce<Mix<T>> for Option<T0>
    where
        T0: Borrow<T> + Send + Sync + 'static,
        T: ?Sized + 'static,
    {
        fn into_response(self) -> <Mix<T> as Respond>::Type {
            match self {
                Some(value) => Some(Box::new(value)),
                None => None,
            }
        }

        fn into_once_responder<F: MockFn<Response = Mix<T>>>(self) -> Responder {
            let output = <Self as IntoResponseOnce<Mix<T>>>::into_response(self);
            Responder(DynResponder::new_cell::<F>(output))
        }
    }

    impl<T0, T> IntoResponseClone<Mix<T>> for Option<T0>
    where
        T0: Borrow<T> + Clone + Send + Sync + 'static,
        T: ?Sized + 'static,
    {
        fn into_clone_responder<F: MockFn<Response = Mix<T>>>(self) -> Responder {
            Responder(DynResponder::new_clone_factory_cell::<F>(move || {
                Some(<Self as IntoResponseOnce<Mix<T>>>::into_response(
                    self.clone(),
                ))
            }))
        }
    }

    impl<'u, T> Output<'u, Mix<T>> for Mixed<Option<&'u T>>
    where
        T: ?Sized + 'u,
    {
        type Type = Option<&'u T>;

        fn from_response(
            response: <Mix<T> as Respond>::Type,
            value_chain: &'u ValueChain,
        ) -> Self::Type {
            match response {
                Some(value) => Some(value_chain.add(value).as_ref().borrow()),
                None => None,
            }
        }

        fn try_borrow_response(
            _: &'u <Mix<T> as Respond>::Type,
        ) -> Result<Self::Type, SignatureError> {
            Err(SignatureError::NotOwned)
        }
    }
}

mod mixed_result_borrowed_t {
    use super::*;

    type Mix<T, E> = Mixed<Result<&'static T, E>>;

    impl<T: ?Sized + 'static, E: 'static> Respond for Mix<T, E> {
        type Type = Result<BoxBorrow<T>, E>;
    }

    impl<T0, T, E> IntoResponseOnce<Mix<T, E>> for Result<T0, E>
    where
        T0: Borrow<T> + Send + Sync + 'static,
        T: ?Sized + 'static,
        E: Send + Sync + 'static,
    {
        fn into_response(self) -> <Mix<T, E> as Respond>::Type {
            match self {
                Ok(value) => Ok(Box::new(value)),
                Err(e) => Err(e),
            }
        }

        fn into_once_responder<F: MockFn<Response = Mix<T, E>>>(self) -> Responder {
            let output = <Self as IntoResponseOnce<Mix<T, E>>>::into_response(self);
            Responder(DynResponder::new_cell::<F>(output))
        }
    }

    impl<T0, T, E> IntoResponseClone<Mix<T, E>> for Result<T0, E>
    where
        T0: Borrow<T> + Clone + Send + Sync + 'static,
        T: ?Sized + 'static,
        E: Clone + Send + Sync + 'static,
    {
        fn into_clone_responder<F: MockFn<Response = Mix<T, E>>>(self) -> Responder {
            Responder(DynResponder::new_clone_factory_cell::<F>(move || {
                Some(<Self as IntoResponseOnce<Mix<T, E>>>::into_response(
                    self.clone(),
                ))
            }))
        }
    }

    impl<'u, T, E: 'static> Output<'u, Mix<T, E>> for Mixed<Result<&'u T, E>>
    where
        T: ?Sized + 'u,
    {
        type Type = Result<&'u T, E>;

        fn from_response(
            response: <Mix<T, E> as Respond>::Type,
            value_chain: &'u ValueChain,
        ) -> Self::Type {
            match response {
                Ok(value) => Ok(value_chain.add(value).as_ref().borrow()),
                Err(e) => Err(e),
            }
        }

        fn try_borrow_response(
            _: &'u <Mix<T, E> as Respond>::Type,
        ) -> Result<Self::Type, SignatureError> {
            Err(SignatureError::NotOwned)
        }
    }
}