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
//! Serializes an iterator of serializables into a serde sequence.
//!
//! *This module requires the "seq" feature to be enabled (enabled by default).*
//!
//! # Usage
//! To derive `Serialize` for a struct that contains arbitrary `Iterator` types,
//! simply add `#[serde(with = "serde_iter::seq")]` on the fields using such types.
//!
//! # Example
//! ```
//! #[derive(serde::Serialize)]
//! struct Foo {
//! #[serde(with = "serde_iter::seq")]
//! bar: std::iter::Once<i32>,
//! }
//!
//! let foo = Foo {
//! bar: std::iter::once(2),
//! };
//! assert_eq!(serde_json::to_value(&foo).unwrap(), serde_json::json!({
//! "bar": [2]
//! }));
//! ```
//!
//! # Cloning
//! Since serialization could be called multiple times on the same value,
//! each time the iterator is serialized, `serde_iter` would clone the iterator and only consume
//! the clone.
//! As a result, the Iterator type must implement `Clone`, which is not implemented by all
//! `Iterator` types.
//!
//! For example, `std::vec::Drain` does not implement `Clone`, because consuming the iterator
//! modifies the `Vec`, and doing so multiple times would lead to different semantics:
//!
//! ```compile_fail
//! #[derive(serde::Serialize)]
//! struct Foo<'a> {
//! #[serde(with = "serde_iter::seq")]
//! bar: std::vec::Drain<'a, i32>,
//! }
//!
//! let mut v = vec![];
//! serde_json::to_value(&Foo { bar: v.drain(..) }).unwrap();;
//! ```
//!
//! This fails to compile, because if the `serde_json::to_value` is called twice, unexpected
//! behaviour would occur: should it yield inconsistent results, or otherwise store the drained
//! data somewhere else?
//!
//! Furthermore, since cloning is not cost-free, if cloning the iterator involves cloning the
//! iterated object, it is *advised* that the iterated type be a `Copy`,
//! or at least some low-cost `Clone` (and low-cost `Drop`).
//! For example, `Rc::clone()`/`Rc::drop()` only increments/decrements a refcount value, so it is
//! relatively cheap.
//! On the other hand, `String::clone()` allocates a new buffer on the heap and copies all
//! characters to the new buffer, which is not low-cost, and it is better to borrow the strings.
//!
//! Consider this example:
//!
//! ```
//! # use std::sync::atomic::{AtomicU8, Ordering};
//! # use serde::Serialize;
//! #
//! static counter: AtomicU8 = AtomicU8::new(0);
//!
//! fn do_something_costly() {
//! counter.fetch_add(1, Ordering::Relaxed);
//! }
//!
//! #[derive(serde::Serialize)]
//! struct Costly;
//!
//! impl Clone for Costly {
//! fn clone(&self) -> Self {
//! do_something_costly();
//! Self
//! }
//! }
//!
//! #[derive(serde::Serialize)]
//! struct Foo<T: Iterator<Item = V> + Clone, V: Serialize> {
//! #[serde(with = "serde_iter::seq")]
//! bar: T,
//! }
//!
//! let v = vec![Costly]; // we just have one Costly item
//! let foo = Foo { bar: v.into_iter() };
//! assert_eq!(counter.load(Ordering::Relaxed), 0);
//! serde_json::to_string(&foo).unwrap();
//! assert_eq!(counter.load(Ordering::Relaxed), 1);
//! serde_json::to_string(&foo).unwrap();
//! assert_eq!(counter.load(Ordering::Relaxed), 2);
//! ```
//!
//! The `Costly` type gets cloned once on every serialization.
//! This is because each serialization clones `Costly`.
//! This may result in significant differences for non-`Copy` types,
//! such as `String`.
//!
//! On the other hand, if the iterator only contains references ot the cloned type:
//! ```
//! # use std::sync::atomic::{AtomicU8, Ordering};
//! # use serde::Serialize;
//! #
//! # static counter: AtomicU8 = AtomicU8::new(0);
//! #
//! # fn do_something_costly() {
//! # counter.fetch_add(1, Ordering::Relaxed);
//! # }
//! #
//! # #[derive(serde::Serialize)]
//! # struct Costly;
//! #
//! # impl Clone for Costly {
//! # fn clone(&self) -> Self {
//! # do_something_costly();
//! # Self
//! # }
//! # }
//! #
//! # #[derive(serde::Serialize)]
//! # struct Foo<T: Iterator<Item = V> + Clone, V: Serialize> {
//! # #[serde(with = "serde_iter::seq")]
//! # bar: T,
//! # }
//! #
//! // Previous definitions unchanged
//!
//! let v = vec![Costly]; // we just have one Costly item
//! let foo = Foo { bar: v.iter() };
//! assert_eq!(counter.load(Ordering::Relaxed), 0);
//! serde_json::to_string(&foo).unwrap();
//! assert_eq!(counter.load(Ordering::Relaxed), 0);
//! serde_json::to_string(&foo).unwrap();
//! assert_eq!(counter.load(Ordering::Relaxed), 0);
//! ```
//!
//! Since `v.iter()` only iterates `&Costly` but not `Costly`, no clones are performed.
//!
//! Note that this this distinction mainly applies for pre-existing values.
//! If the clonable value is created during iteration,
//! e.g. if it is `iter.map(|x| x.to_string())`,
//! borrowing would not improve anything;
//! to prevent cloning unnecessarily, it might be desirable to
//! store the mapped data in a `Vec` beforehand.
use ;
/// Refer to the [module-level documentation](index.html).