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
//! # What is Refcon?
//!
//! This crate provides the [`Refcon`] enum, which is used to wrap either a
//! concrete instance of a type `T`, or a reference to one.
//!
//! By implementing [`AsRef<T>`], [`Refcon`] lets either variant seamlessly
//! behave as a reference to `T`.
//!
//! ```ignore
//! const ZERO = ABigStruct::costly_initializer();
//! let v: Vec<ABigStruct> = (0..10_000)
//! .map(|i|
//! if i < 1_000 {
//! Refcon::from(&ZERO)
//! } else {
//! Refcon::from(ABigStruct::new(i))
//! })
//! .collect();
//!
//! // [...]
//!
//! for x in v.iter() {
//! x.do_something();
//! }
//! ```
//!
//! ## When **not** to use Refcon
//! - `T` is smaller than two machine words – you should just copy everything
//! in this case anyways;
//! - you can not afford/do not want to pay for the double indirection price
//! while accessing the inner value;
//! - you do not want to pay for the price of the additional memory copy while
//! constructing the concrete variant;
//!
//! ## When to use Refcon
//! - You want to mix values & references: _e.g._ if you have a vector
//! containing either reference to pre-computed values or ad-hoc ones or if an
//! iterator in a trait may iterate over immediate or reference values
//! depending on the implementation.