@@ -49,81 +49,58 @@ use crate::{
49
49
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
50
50
/// );
51
51
/// ```
52
-
53
- pub struct CachedSource < T : ' static > {
54
- inner : CachedSourceInner < T > ,
55
- }
56
-
57
- #[ ouroboros:: self_referencing]
58
- pub struct CachedSourceInner < T : ' static > {
52
+ pub struct CachedSource < T > {
59
53
inner : Arc < T > ,
60
- #[ not_covariant]
61
- #[ borrows( inner) ]
62
- cached_rope : Arc < OnceLock < Rope < ' this > > > ,
63
54
cached_hash : Arc < OnceLock < u64 > > ,
64
55
cached_maps :
65
56
Arc < DashMap < MapOptions , Option < SourceMap > , BuildHasherDefault < FxHasher > > > ,
66
57
}
67
58
68
- impl < T : Source > CachedSource < T > {
69
- fn get_rope ( & self ) -> & Rope < ' _ > {
70
- self
71
- . inner
72
- . with ( |cache| cache. cached_rope . get_or_init ( || cache. inner . rope ( ) ) )
73
- }
74
- }
75
-
76
59
impl < T > CachedSource < T > {
77
60
/// Create a [CachedSource] with the original [Source].
78
61
pub fn new ( inner : T ) -> Self {
79
62
Self {
80
- inner : CachedSourceInner :: new (
81
- Arc :: new ( inner) ,
82
- |_| Default :: default ( ) ,
83
- Default :: default ( ) ,
84
- Default :: default ( ) ,
85
- ) ,
63
+ inner : Arc :: new ( inner) ,
64
+ cached_hash : Default :: default ( ) ,
65
+ cached_maps : Default :: default ( ) ,
86
66
}
87
67
}
88
68
89
69
/// Get the original [Source].
90
70
pub fn original ( & self ) -> & T {
91
- self . inner . borrow_inner ( )
71
+ & self . inner
92
72
}
93
73
}
94
74
95
75
impl < T : Source + Hash + PartialEq + Eq + ' static > Source for CachedSource < T > {
96
76
fn source ( & self ) -> Cow < str > {
97
- Cow :: Owned ( self . get_rope ( ) . to_string ( ) )
77
+ self . inner . source ( )
98
78
}
99
79
100
80
fn rope ( & self ) -> Rope < ' _ > {
101
- self . get_rope ( ) . clone ( )
81
+ self . inner . rope ( )
102
82
}
103
83
104
84
fn buffer ( & self ) -> Cow < [ u8 ] > {
105
- self . inner . borrow_inner ( ) . buffer ( )
85
+ self . inner . buffer ( )
106
86
}
107
87
108
88
fn size ( & self ) -> usize {
109
89
self . source ( ) . len ( )
110
90
}
111
91
112
92
fn map ( & self , options : & MapOptions ) -> Option < SourceMap > {
113
- if let Some ( map) = self . inner . borrow_cached_maps ( ) . get ( options) {
93
+ if let Some ( map) = self . cached_maps . get ( options) {
114
94
map. clone ( )
115
95
} else {
116
- let map = self . inner . borrow_inner ( ) . map ( options) ;
117
- self
118
- . inner
119
- . borrow_cached_maps ( )
120
- . insert ( options. clone ( ) , map. clone ( ) ) ;
96
+ let map = self . inner . map ( options) ;
97
+ self . cached_maps . insert ( options. clone ( ) , map. clone ( ) ) ;
121
98
map
122
99
}
123
100
}
124
101
125
102
fn to_writer ( & self , writer : & mut dyn std:: io:: Write ) -> std:: io:: Result < ( ) > {
126
- self . inner . borrow_inner ( ) . to_writer ( writer)
103
+ self . inner . to_writer ( writer)
127
104
}
128
105
}
129
106
@@ -137,7 +114,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
137
114
on_source : crate :: helpers:: OnSource < ' _ , ' a > ,
138
115
on_name : crate :: helpers:: OnName < ' _ , ' a > ,
139
116
) -> crate :: helpers:: GeneratedInfo {
140
- let cached_map = self . inner . borrow_cached_maps ( ) . entry ( options. clone ( ) ) ;
117
+ let cached_map = self . cached_maps . entry ( options. clone ( ) ) ;
141
118
match cached_map {
142
119
Entry :: Occupied ( entry) => {
143
120
let source = self . rope ( ) ;
@@ -161,7 +138,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
161
138
}
162
139
Entry :: Vacant ( entry) => {
163
140
let ( generated_info, map) = stream_and_get_source_and_map (
164
- self . inner . borrow_inner ( ) as & T ,
141
+ & self . inner as & T ,
165
142
options,
166
143
on_chunk,
167
144
on_source,
@@ -176,21 +153,19 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
176
153
177
154
impl < T > Clone for CachedSource < T > {
178
155
fn clone ( & self ) -> Self {
179
- // Self {
180
- // inner: self.inner.clone(),
181
- // cached_rope: Default::default(),
182
- // cached_hash: self.cached_hash.clone(),
183
- // cached_maps: self.cached_maps.clone(),
184
- // }
185
- todo ! ( )
156
+ Self {
157
+ inner : self . inner . clone ( ) ,
158
+ cached_hash : self . cached_hash . clone ( ) ,
159
+ cached_maps : self . cached_maps . clone ( ) ,
160
+ }
186
161
}
187
162
}
188
163
189
164
impl < T : Source + Hash + PartialEq + Eq + ' static > Hash for CachedSource < T > {
190
165
fn hash < H : std:: hash:: Hasher > ( & self , state : & mut H ) {
191
- ( self . inner . borrow_cached_hash ( ) . get_or_init ( || {
166
+ ( self . cached_hash . get_or_init ( || {
192
167
let mut hasher = FxHasher :: default ( ) ;
193
- self . original ( ) . hash ( & mut hasher) ;
168
+ self . inner . hash ( & mut hasher) ;
194
169
hasher. finish ( )
195
170
} ) )
196
171
. hash ( state) ;
@@ -199,7 +174,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Hash for CachedSource<T> {
199
174
200
175
impl < T : PartialEq > PartialEq for CachedSource < T > {
201
176
fn eq ( & self , other : & Self ) -> bool {
202
- self . inner . borrow_inner ( ) == other. inner . borrow_inner ( )
177
+ self . inner == other. inner
203
178
}
204
179
}
205
180
@@ -211,12 +186,9 @@ impl<T: std::fmt::Debug> std::fmt::Debug for CachedSource<T> {
211
186
f : & mut std:: fmt:: Formatter < ' _ > ,
212
187
) -> Result < ( ) , std:: fmt:: Error > {
213
188
f. debug_struct ( "CachedSource" )
214
- . field ( "inner" , self . inner . borrow_inner ( ) . as_ref ( ) )
215
- . field ( "cached_hash" , self . inner . borrow_cached_hash ( ) . as_ref ( ) )
216
- . field (
217
- "cached_maps" ,
218
- & ( !self . inner . borrow_cached_maps ( ) . is_empty ( ) ) ,
219
- )
189
+ . field ( "inner" , self . inner . as_ref ( ) )
190
+ . field ( "cached_hash" , self . cached_hash . as_ref ( ) )
191
+ . field ( "cached_maps" , & ( !self . cached_maps . is_empty ( ) ) )
220
192
. finish ( )
221
193
}
222
194
}
@@ -264,12 +236,7 @@ mod tests {
264
236
source. map ( & map_options) ;
265
237
266
238
assert_eq ! (
267
- * clone
268
- . inner
269
- . borrow_cached_maps( )
270
- . get( & map_options)
271
- . unwrap( )
272
- . value( ) ,
239
+ * clone. cached_maps. get( & map_options) . unwrap( ) . value( ) ,
273
240
source. map( & map_options)
274
241
) ;
275
242
}
0 commit comments