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
use ;
use ;
use RawValue;
/// A unique internal identifier for a method.
;
/// An object that can be sent over RPC.
///
/// This trait is blanket-implemented for every [`Serialize`] type that
/// satisfies the required bounds. It is used to indicate that a type can
/// be sent in the body of a JSON-RPC message.
///
/// The [`into_raw_value`] method consumes `self` and produces a serialized
/// [`RawValue`]. This consuming interface allows types like iterators to
/// be serialized without intermediate allocation (e.g., collecting into a
/// [`Vec`]).
///
/// Note that this trait does **not** require [`Clone`] or [`Debug`].
///
/// # Custom implementations
///
/// Types that do not implement [`Serialize`] can implement `RpcSend`
/// directly by providing a custom [`into_raw_value`]. This is useful for
/// lazily-produced sequences or pre-serialized data.
///
/// # Example: serializing an iterator without allocation
///
/// ```
/// use ajj::RpcSend;
/// use serde::Serialize;
/// use serde_json::value::RawValue;
///
/// /// Wraps an iterator, serializing its items as a JSON array
/// /// without collecting into a [`Vec`].
/// struct IterResponse<T>(T);
///
/// impl<T, Item> RpcSend for IterResponse<T>
/// where
/// T: Iterator<Item = Item> + Send + Sync + Unpin,
/// Item: Serialize,
/// {
/// fn into_raw_value(self) -> serde_json::Result<Box<RawValue>> {
/// let mut json = String::from("[");
/// for (i, item) in self.0.enumerate() {
/// if i > 0 {
/// json.push(',');
/// }
/// json.push_str(&serde_json::to_string(&item)?);
/// }
/// json.push(']');
/// RawValue::from_string(json)
/// }
/// }
///
/// let data = vec![1usize, 2, 3];
/// let resp = IterResponse(data.into_iter());
///
/// let json = resp.into_raw_value().unwrap();
/// assert_eq!(json.get(), "[1,2,3]");
/// ```
///
/// [`into_raw_value`]: RpcSend::into_raw_value
/// 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.