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
//!
//! ## Basic use-case
//!
//! Demonstrates the usage of `clone_dyn` to enable cloning for trait objects.
//!
//! By default, Rust does not support cloning for trait objects due to the `Clone` trait
//! requiring compile-time knowledge of the type's size. The `clone_dyn` crate addresses
//! this limitation through procedural macros, allowing for cloning collections of trait objects.
//!
//! ##### Overview
//!
//! This example shows how to use the `clone_dyn` crate to enable cloning for trait objects,
//! specifically for iterators. It defines a custom trait, `IterTrait`, that encapsulates
//! an iterator with specific characteristics and demonstrates how to use `CloneDyn` to
//! overcome the object safety constraints of the `Clone` trait.
//!
//! ##### The `IterTrait` Trait
//!
//! The `IterTrait` trait is designed to represent iterators that yield references to items (`&'a T`).
//! These iterators must also implement the `ExactSizeIterator` and `DoubleEndedIterator` traits.
//! Additionally, the iterator must implement the `CloneDyn` trait, which allows cloning of trait objects.
//!
//! The trait is implemented for any type that meets the specified requirements.
//!
//! ##### Cloning Trait Objects
//!
//! Rust's type system does not allow trait objects to implement the `Clone` trait directly due to object safety constraints.
//! Specifically, the `Clone` trait requires knowledge of the concrete type at compile time, which is not available for trait objects.
//!
//! The `CloneDyn` trait from the `clone_dyn` crate provides a workaround for this limitation by allowing trait objects to be cloned.
//! Procedural macros generates the necessary code for cloning trait objects, making it possible to clone collections of trait objects.
//!
//! The example demonstrates how to implement `Clone` for boxed `IterTrait` trait objects.
//!
//! ##### `get_iter` Function
//!
//! The `get_iter` function returns a boxed iterator that implements the `IterTrait` trait.
//! If the input is `Some`, it returns an iterator over the vector.
//! If the input is `None`, it returns an empty iterator.
//!
//! It's not possible to use `impl Iterator` here because the code returns iterators of two different types :
//! - `std ::slice ::Iter` when the input is `Some`.
//! - `std ::iter ::Empty` when the input is `None`.
//!
//! To handle this, the function returns a trait object ( `Box< dyn IterTrait >` ).
//! However, Rust's `Clone` trait cannot be implemented for trait objects due to object safety constraints.
//! The `CloneDyn` trait addresses this problem by enabling cloning of trait objects.
//!
//! ##### `use_iter` Function
//!
//! The `use_iter` function demonstrates the use of the `CloneDyn` trait by cloning the iterator.
//! It then iterates over the cloned iterator and prints each element.
//!
//! ##### Main Function
//!
//! The main function demonstrates the overall usage by creating a vector, obtaining an iterator, and using the iterator to print elements.
//!