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
//! # Deferred Vector Library
//!
//! `DeferredVec` is a Rust library providing a generic, lazily-initialized vector structure.
//! This structure is designed for scenarios where the initialization of a large or
//! resource-intensive vector should be deferred until it's actually needed.
//!
//! Developed by Prof. Afonso Miguel - PUCPR, this library is ideal for applications
//! where performance and efficient resource management are critical.
//!
//! ## Features
//!
//! - **Laziness**: The vector is not initialized until it's explicitly accessed. This
//! lazy initialization is beneficial for performance in cases where the vector might not
//! be used immediately or at all.
//! - **Flexibility**: Works with any type `T` that implements the `Clone` trait, allowing
//! for a wide range of applications.
//! - **Custom Initialization**: The vector is initialized using a user-provided function,
//! offering flexibility in how the vector's contents are determined.
//!
//! ## Usage
//!
//! To use `DeferredVec`, define a `fetch_function` that returns the initial state of the
//! vector when called. The `DeferredVec` struct can then be instantiated with this function.
//! The vector remains uninitialized (`None`) until methods like `get` or `len` are called,
//! at which point `fetch_function` is invoked to initialize the vector.
//!
//! ## Examples
//!
//! Basic usage:
//!
//! ```
//! let mut deferred_vector = DeferredVec::new(|| vec![1, 2, 3]);
//! assert_eq!(deferred_vector.is_deferred(), true);
//! let initialized_vector = deferred_vector.get();
//! assert_eq!(deferred_vector.is_deferred(), false);
//! ```
//!
//! ## Testing
//!
//! The module includes unit tests to verify the functionality, especially focusing on
//! the lazy initialization and basic vector operations.
//!
//! ## Disclaimer
//!
//! This code is provided as-is, without warranty. Users should thoroughly test it in their
//! own applications before any production use.
//!
//! ## Author
//!
//! Prof. Afonso Miguel - PUCPR - Original implementation and documentation.
//!
//! ## License
//!
//! This project is licensed under the MIT License - see the LICENSE file for details.
/// A generic struct `DeferredVec` for lazily initializing a vector.
///
/// This struct holds an `Option<Vec<T>>` to store the vector,
/// which may or may not be present initially, and a `fetch_function`
/// of type `fn() -> Vec<T>`, which is a function pointer
/// that returns a vector of the same type when called.
/// Implement methods for `DeferredVec`.
///
/// The `#[allow(dead_code)]` attribute indicates
/// that even if some methods are not used, they should not be considered dead code.
/// The generic type `T` is bound by the trait `std::clone::Clone` to ensure
/// that elements of the vector can be cloned.
/// Unit tests for `DeferredVec`.