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
//! Utility for enquiry between 2 threads. This means that one thread starts the communication by
//! sending data to the other one (ask a question) and the other thread sending data back (answering
//! the question).
//! Under the hood enquiry uses two tokio channels, first an mpsc for asking the question (so that
//! multiple questions can be asked) and second a oneshot for answering (as only one answer per
//! question is allowed)
//! This is intended to be a two way async communication.
//!
//! **Examples:**
//!
//! ```
//! use diath::enquiry;
//! #[tokio::main]
//! async fn main() {
//! let (q1, q2) = ("Are you innocent?".to_string(), "Did you kill him?".to_string());
//!
//! let (questioner, Responder) = enquiry::new(3);
//!
//! let i1 = tokio::task::spawn(interrogator(questioner.clone(), q1));
//! let i2 = tokio::task::spawn(interrogator(questioner, q2));
//! tokio::task::spawn(suspect(answer));
//!
//! assert!(i1.await.unwrap()); // Assert it is innocent
//! assert!(!i2.await.unwrap()); // Assert it did not kill the victim
//! }
//! async fn interrogator(questioner: enquiry::question::Questioner<String, bool>, question: String) -> bool {
//! questioner.ask_and_listen(question).await.unwrap()
//! }
//! async fn suspect(mut Responder: enquiry::answer::Responder<String, bool>) {
//! while let Some(dialogue) = Responder.listen().await {
//! match dialogue.as_str() {
//! "Did you kill him?" => dialogue.answer(false),
//! "Are you innocent?" => dialogue.answer(true),
//! _ => unreachable!(),
//! }
//! }
//! }
//! ```
use Questioner;
use Responder;
/// Creates a new tuple of (`Question<Q, A>`, `Answer<Q, A>`)