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
use core::borrow::Borrow;
use core::convert::AsRef;
use core::fmt;
use core::mem::ManuallyDrop;
use core::ops::Deref;
use core::ptr;
use crate::cell::{Block, OnceCell};
pub struct Lazy<T, B, F = fn() -> T> {
cell: OnceCell<T, B>,
init: ManuallyDrop<F>,
}
impl<T, B, F> Lazy<T, B, F> {
#[inline]
pub const fn new(init: F) -> Self {
Self { cell: OnceCell::uninit(), init: ManuallyDrop::new(init) }
}
}
impl<T, B, F> Lazy<T, B, F>
where
B: Block,
F: FnOnce() -> T,
{
#[inline]
pub fn is_initialized(lazy: &Self) -> bool {
lazy.cell.is_initialized()
}
#[inline]
pub fn is_poisoned(lazy: &Self) -> bool {
lazy.cell.is_poisoned()
}
#[inline]
pub fn get_or_init(lazy: &Self) -> &T {
lazy.cell.get_or_init(|| {
let func = unsafe { ptr::read(&*lazy.init) };
func()
})
}
}
impl<T, B, F> AsRef<T> for Lazy<T, B, F>
where
B: Block,
F: FnOnce() -> T,
{
#[inline]
fn as_ref(&self) -> &T {
Lazy::get_or_init(self)
}
}
impl<T, B, F> Borrow<T> for Lazy<T, B, F>
where
B: Block,
F: FnOnce() -> T,
{
#[inline]
fn borrow(&self) -> &T {
Lazy::get_or_init(self)
}
}
impl<T, B, F> fmt::Debug for Lazy<T, B, F>
where
T: fmt::Debug,
B: Block,
F: FnOnce() -> T,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.cell, f)
}
}
impl<T, B, F> fmt::Display for Lazy<T, B, F>
where
T: fmt::Display,
B: Block,
F: FnOnce() -> T,
{
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(Self::get_or_init(self), f)
}
}
impl<T, B, F> Deref for Lazy<T, B, F>
where
B: Block,
F: FnOnce() -> T,
{
type Target = T;
#[inline]
fn deref(&self) -> &Self::Target {
Lazy::get_or_init(self)
}
}