-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
59e29e8
commit a64917f
Showing
12 changed files
with
242 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use core::ops::Deref; | ||
|
||
use crate::{ | ||
output::{Output, Respond}, | ||
MockFn, Unimock, | ||
}; | ||
|
||
/// A borrowed Answering function. | ||
pub struct AnswerFn<'u, F: MockFn> { | ||
pub(crate) answer_fn: &'u F::AnswerFn, | ||
} | ||
|
||
impl<'u, F: MockFn> Deref for AnswerFn<'u, F> { | ||
type Target = F::AnswerFn; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.answer_fn | ||
} | ||
} | ||
|
||
impl<'u, F: MockFn> AnswerFn<'u, F> { | ||
/// Convert the answering function's response to a mock output. | ||
pub fn make_output( | ||
&self, | ||
response: <F::Response as Respond>::Type, | ||
unimock: &'u Unimock, | ||
) -> <F::Output<'u> as Output<'u, F::Response>>::Type { | ||
<F::Output<'u> as Output<'u, F::Response>>::from_response(response, &unimock.value_chain) | ||
} | ||
} | ||
|
||
/// Trait for converting a closure into [MockFn::AnswerFn] | ||
pub trait IntoAnswerFn<Args> { | ||
/// The resulting AnswerFn, some `dyn Fn`. | ||
type AnswerFn: ?Sized + Send + Sync; | ||
|
||
/// Performs the conversion. | ||
fn into_answer_fn(self) -> Box<Self::AnswerFn>; | ||
} | ||
|
||
impl<F, O> IntoAnswerFn<()> for F | ||
where | ||
F: Fn() -> O + Send + Sync + 'static, | ||
{ | ||
type AnswerFn = dyn (Fn() -> O) + Send + Sync; | ||
|
||
fn into_answer_fn(self) -> Box<Self::AnswerFn> { | ||
Box::new(self) | ||
} | ||
} | ||
|
||
macro_rules! arg_tuples { | ||
($($a:ident),+) => { | ||
impl<F, $($a),+, O> IntoAnswerFn<($($a),+,)> for F | ||
where | ||
F: Fn($($a),+) -> O + Send + Sync + 'static, | ||
{ | ||
type AnswerFn = dyn (Fn($($a),+) -> O) + Send + Sync; | ||
|
||
fn into_answer_fn(self) -> Box<Self::AnswerFn> { | ||
Box::new(self) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
arg_tuples!(A0); | ||
arg_tuples!(A0, A1); | ||
arg_tuples!(A0, A1, A2); | ||
arg_tuples!(A0, A1, A2, A3); | ||
arg_tuples!(A0, A1, A2, A3, A4); | ||
arg_tuples!(A0, A1, A2, A3, A4, A5); | ||
arg_tuples!(A0, A1, A2, A3, A4, A5, A6); | ||
arg_tuples!(A0, A1, A2, A3, A4, A5, A6, A7); | ||
arg_tuples!(A0, A1, A2, A3, A4, A5, A6, A7, A8); | ||
arg_tuples!(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.