@@ -49,81 +49,58 @@ use crate::{
4949/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
5050/// );
5151/// ```
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 > {
5953 inner : Arc < T > ,
60- #[ not_covariant]
61- #[ borrows( inner) ]
62- cached_rope : Arc < OnceLock < Rope < ' this > > > ,
6354 cached_hash : Arc < OnceLock < u64 > > ,
6455 cached_maps :
6556 Arc < DashMap < MapOptions , Option < SourceMap > , BuildHasherDefault < FxHasher > > > ,
6657}
6758
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-
7659impl < T > CachedSource < T > {
7760 /// Create a [CachedSource] with the original [Source].
7861 pub fn new ( inner : T ) -> Self {
7962 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 ( ) ,
8666 }
8767 }
8868
8969 /// Get the original [Source].
9070 pub fn original ( & self ) -> & T {
91- self . inner . borrow_inner ( )
71+ & self . inner
9272 }
9373}
9474
9575impl < T : Source + Hash + PartialEq + Eq + ' static > Source for CachedSource < T > {
9676 fn source ( & self ) -> Cow < str > {
97- Cow :: Owned ( self . get_rope ( ) . to_string ( ) )
77+ self . inner . source ( )
9878 }
9979
10080 fn rope ( & self ) -> Rope < ' _ > {
101- self . get_rope ( ) . clone ( )
81+ self . inner . rope ( )
10282 }
10383
10484 fn buffer ( & self ) -> Cow < [ u8 ] > {
105- self . inner . borrow_inner ( ) . buffer ( )
85+ self . inner . buffer ( )
10686 }
10787
10888 fn size ( & self ) -> usize {
10989 self . source ( ) . len ( )
11090 }
11191
11292 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) {
11494 map. clone ( )
11595 } 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 ( ) ) ;
12198 map
12299 }
123100 }
124101
125102 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)
127104 }
128105}
129106
@@ -137,7 +114,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
137114 on_source : crate :: helpers:: OnSource < ' _ , ' a > ,
138115 on_name : crate :: helpers:: OnName < ' _ , ' a > ,
139116 ) -> 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 ( ) ) ;
141118 match cached_map {
142119 Entry :: Occupied ( entry) => {
143120 let source = self . rope ( ) ;
@@ -161,7 +138,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
161138 }
162139 Entry :: Vacant ( entry) => {
163140 let ( generated_info, map) = stream_and_get_source_and_map (
164- self . inner . borrow_inner ( ) as & T ,
141+ & self . inner as & T ,
165142 options,
166143 on_chunk,
167144 on_source,
@@ -176,21 +153,19 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks
176153
177154impl < T > Clone for CachedSource < T > {
178155 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+ }
186161 }
187162}
188163
189164impl < T : Source + Hash + PartialEq + Eq + ' static > Hash for CachedSource < T > {
190165 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 ( || {
192167 let mut hasher = FxHasher :: default ( ) ;
193- self . original ( ) . hash ( & mut hasher) ;
168+ self . inner . hash ( & mut hasher) ;
194169 hasher. finish ( )
195170 } ) )
196171 . hash ( state) ;
@@ -199,7 +174,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Hash for CachedSource<T> {
199174
200175impl < T : PartialEq > PartialEq for CachedSource < T > {
201176 fn eq ( & self , other : & Self ) -> bool {
202- self . inner . borrow_inner ( ) == other. inner . borrow_inner ( )
177+ self . inner == other. inner
203178 }
204179}
205180
@@ -211,12 +186,9 @@ impl<T: std::fmt::Debug> std::fmt::Debug for CachedSource<T> {
211186 f : & mut std:: fmt:: Formatter < ' _ > ,
212187 ) -> Result < ( ) , std:: fmt:: Error > {
213188 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 ( ) ) )
220192 . finish ( )
221193 }
222194}
@@ -264,12 +236,7 @@ mod tests {
264236 source. map ( & map_options) ;
265237
266238 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( ) ,
273240 source. map( & map_options)
274241 ) ;
275242 }
0 commit comments