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
use ;
use ;
/// A unique internal identifier for a method.
;
/// An object that can be sent over RPC.
///
/// This marker trait is blanket-implemented for every qualifying type. It is
/// used to indicate that a type can be sent in the body of a JSON-RPC message.
///
/// Note that this trait does **not** require [`Clone`] or [`Debug`]. Types
/// that serialize computed or lazily-produced sequences can implement
/// [`Serialize`] directly and be returned from handlers without collecting
/// into a [`Vec`] first.
///
/// # Example: serializing an iterator without allocation
///
/// ```
/// use ajj::RpcSend;
/// use serde::{Serialize, ser::SerializeSeq, Serializer};
/// use std::sync::Mutex;
///
/// /// Wraps an iterator, serializing its items as a JSON array
/// /// without collecting into a [`Vec`].
/// struct IterResponse<T>(Mutex<Option<T>>);
///
/// impl<'a, T> Serialize for IterResponse<T>
/// where
/// T: Iterator<Item = &'a usize>,
/// {
/// fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
/// let mut seq = serializer.serialize_seq(None)?;
/// if let Some(iter) = self.0.lock().unwrap().take() {
/// for item in iter {
/// seq.serialize_element(item)?;
/// }
/// }
/// seq.end()
/// }
/// }
///
/// let data = vec![1usize, 2, 3];
/// let resp = IterResponse(Mutex::new(Some(data.iter())));
///
/// // IterResponse satisfies RpcSend without implementing Clone or Debug.
/// fn is_rpc_send(_: &impl RpcSend) {}
/// is_rpc_send(&resp);
///
/// let json = serde_json::to_string(&resp).unwrap();
/// assert_eq!(json, "[1,2,3]");
/// ```
/// An object that can be received over RPC.
///
/// This marker trait is blanket-implemented for every qualifying type. It is
/// used to indicate that a type can be received in the body of a JSON-RPC
/// message.
///
/// # Note
///
/// We add the `'static` lifetime to the supertraits to indicate that the type
/// can't borrow. This is a simplification that makes it easier to use the
/// types in client code. Servers may prefer borrowing, using the [`RpcBorrow`]
/// trait.
/// An object that can be received over RPC, borrowing from the the
/// deserialization context.
///
/// This marker trait is blanket-implemented for every qualifying type. It is
/// used to indicate that a type can be borrowed from the body of a wholly or
/// partially serialized JSON-RPC message.
/// An object that can be both sent and received over RPC.
///
/// This marker trait is blanket-implemented for every qualifying type. It is
/// used to indicate that a type can be both sent and received in the body of a
/// JSON-RPC message.
///
/// # Note
///
/// We add the `'static` lifetime to the supertraits to indicate that the type
/// can't borrow. This is a simplification that makes it easier to use the
/// types in client code. Servers may prefer borrowing, using the
/// [`BorrowedRpcObject`] trait.
/// An object that can be both sent and received over RPC, borrowing from the
/// the deserialization context.
///
/// This marker trait is blanket-implemented for every qualifying type. It is
/// used to indicate that a type can be both sent and received in the body of a
/// JSON-RPC message, and can borrow from the deserialization context.