@@ -18,14 +18,14 @@ use std::str;
18
18
19
19
// Common data structures
20
20
#[ deriving( Clone ) ]
21
- pub struct Doc {
22
- data : @~ [ u8 ] ,
21
+ pub struct Doc < ' a > {
22
+ data : & ' a [ u8 ] ,
23
23
start : uint ,
24
24
end : uint ,
25
25
}
26
26
27
- impl Doc {
28
- pub fn get ( & self , tag : uint ) -> Doc {
27
+ impl < ' doc > Doc < ' doc > {
28
+ pub fn get < ' a > ( & ' a self , tag : uint ) -> Doc < ' a > {
29
29
reader:: get_doc ( * self , tag)
30
30
}
31
31
@@ -38,9 +38,9 @@ impl Doc {
38
38
}
39
39
}
40
40
41
- pub struct TaggedDoc {
41
+ pub struct TaggedDoc < ' a > {
42
42
priv tag: uint ,
43
- doc : Doc ,
43
+ doc : Doc < ' a > ,
44
44
}
45
45
46
46
pub enum EbmlEncoderTag {
@@ -167,25 +167,25 @@ pub mod reader {
167
167
vuint_at_slow ( data, start)
168
168
}
169
169
170
- pub fn Doc ( data : @~ [ u8 ] ) -> Doc {
170
+ pub fn Doc < ' a > ( data : & ' a [ u8 ] ) -> Doc < ' a > {
171
171
Doc { data : data, start : 0 u, end : data. len ( ) }
172
172
}
173
173
174
- pub fn doc_at ( data : @~ [ u8 ] , start : uint ) -> TaggedDoc {
175
- let elt_tag = vuint_at ( * data, start) ;
176
- let elt_size = vuint_at ( * data, elt_tag. next ) ;
174
+ pub fn doc_at < ' a > ( data : & ' a [ u8 ] , start : uint ) -> TaggedDoc < ' a > {
175
+ let elt_tag = vuint_at ( data, start) ;
176
+ let elt_size = vuint_at ( data, elt_tag. next ) ;
177
177
let end = elt_size. next + elt_size. val ;
178
178
TaggedDoc {
179
179
tag : elt_tag. val ,
180
180
doc : Doc { data : data, start : elt_size. next , end : end }
181
181
}
182
182
}
183
183
184
- pub fn maybe_get_doc ( d : Doc , tg : uint ) -> Option < Doc > {
184
+ pub fn maybe_get_doc < ' a > ( d : Doc < ' a > , tg : uint ) -> Option < Doc < ' a > > {
185
185
let mut pos = d. start ;
186
186
while pos < d. end {
187
- let elt_tag = vuint_at ( * d. data , pos) ;
188
- let elt_size = vuint_at ( * d. data , elt_tag. next ) ;
187
+ let elt_tag = vuint_at ( d. data , pos) ;
188
+ let elt_size = vuint_at ( d. data , elt_tag. next ) ;
189
189
pos = elt_size. next + elt_size. val ;
190
190
if elt_tag. val == tg {
191
191
return Some ( Doc { data : d. data , start : elt_size. next ,
@@ -195,7 +195,7 @@ pub mod reader {
195
195
None
196
196
}
197
197
198
- pub fn get_doc ( d : Doc , tg : uint ) -> Doc {
198
+ pub fn get_doc < ' a > ( d : Doc < ' a > , tg : uint ) -> Doc < ' a > {
199
199
match maybe_get_doc ( d, tg) {
200
200
Some ( d) => d,
201
201
None => {
@@ -205,11 +205,11 @@ pub mod reader {
205
205
}
206
206
}
207
207
208
- pub fn docs ( d : Doc , it : |uint , Doc | -> bool) -> bool {
208
+ pub fn docs < ' a > ( d : Doc < ' a > , it : |uint , Doc < ' a > | -> bool ) -> bool {
209
209
let mut pos = d. start ;
210
210
while pos < d. end {
211
- let elt_tag = vuint_at ( * d. data , pos) ;
212
- let elt_size = vuint_at ( * d. data , elt_tag. next ) ;
211
+ let elt_tag = vuint_at ( d. data , pos) ;
212
+ let elt_size = vuint_at ( d. data , elt_tag. next ) ;
213
213
pos = elt_size. next + elt_size. val ;
214
214
let doc = Doc { data : d. data , start : elt_size. next , end : pos } ;
215
215
if !it ( elt_tag. val , doc) {
@@ -219,11 +219,11 @@ pub mod reader {
219
219
return true ;
220
220
}
221
221
222
- pub fn tagged_docs ( d : Doc , tg : uint , it: |Doc | -> bool) -> bool {
222
+ pub fn tagged_docs < ' a > ( d : Doc < ' a > , tg : uint , it: |Doc < ' a > | -> bool ) -> bool {
223
223
let mut pos = d. start ;
224
224
while pos < d. end {
225
- let elt_tag = vuint_at ( * d. data , pos) ;
226
- let elt_size = vuint_at ( * d. data , elt_tag. next ) ;
225
+ let elt_tag = vuint_at ( d. data , pos) ;
226
+ let elt_size = vuint_at ( d. data , elt_tag. next ) ;
227
227
pos = elt_size. next + elt_size. val ;
228
228
if elt_tag. val == tg {
229
229
let doc = Doc { data : d. data , start : elt_size. next ,
@@ -236,49 +236,49 @@ pub mod reader {
236
236
return true ;
237
237
}
238
238
239
- pub fn with_doc_data < T > ( d : Doc , f: |x: & [ u8 ] | -> T ) -> T {
239
+ pub fn with_doc_data < ' a , T > ( d : Doc < ' a > , f: |x: & ' a [ u8 ] | -> T ) -> T {
240
240
f ( d. data . slice ( d. start , d. end ) )
241
241
}
242
242
243
243
244
244
pub fn doc_as_u8 ( d : Doc ) -> u8 {
245
245
assert_eq ! ( d. end, d. start + 1 u) ;
246
- ( * d. data ) [ d. start ]
246
+ d. data [ d. start ]
247
247
}
248
248
249
249
pub fn doc_as_u16 ( d : Doc ) -> u16 {
250
250
assert_eq ! ( d. end, d. start + 2 u) ;
251
- u64_from_be_bytes ( * d. data , d. start , 2 u) as u16
251
+ u64_from_be_bytes ( d. data , d. start , 2 u) as u16
252
252
}
253
253
254
254
pub fn doc_as_u32 ( d : Doc ) -> u32 {
255
255
assert_eq ! ( d. end, d. start + 4 u) ;
256
- u64_from_be_bytes ( * d. data , d. start , 4 u) as u32
256
+ u64_from_be_bytes ( d. data , d. start , 4 u) as u32
257
257
}
258
258
259
259
pub fn doc_as_u64 ( d : Doc ) -> u64 {
260
260
assert_eq ! ( d. end, d. start + 8 u) ;
261
- u64_from_be_bytes ( * d. data , d. start , 8 u)
261
+ u64_from_be_bytes ( d. data , d. start , 8 u)
262
262
}
263
263
264
264
pub fn doc_as_i8 ( d : Doc ) -> i8 { doc_as_u8 ( d) as i8 }
265
265
pub fn doc_as_i16 ( d : Doc ) -> i16 { doc_as_u16 ( d) as i16 }
266
266
pub fn doc_as_i32 ( d : Doc ) -> i32 { doc_as_u32 ( d) as i32 }
267
267
pub fn doc_as_i64 ( d : Doc ) -> i64 { doc_as_u64 ( d) as i64 }
268
268
269
- pub struct Decoder {
270
- priv parent : Doc ,
269
+ pub struct Decoder < ' a > {
270
+ priv parent : Doc < ' a > ,
271
271
priv pos: uint ,
272
272
}
273
273
274
- pub fn Decoder ( d : Doc ) -> Decoder {
274
+ pub fn Decoder < ' a > ( d : Doc < ' a > ) -> Decoder < ' a > {
275
275
Decoder {
276
276
parent : d,
277
277
pos : d. start
278
278
}
279
279
}
280
280
281
- impl Decoder {
281
+ impl < ' doc > Decoder < ' doc > {
282
282
fn _check_label ( & mut self , lbl : & str ) {
283
283
if self . pos < self . parent . end {
284
284
let TaggedDoc { tag : r_tag, doc : r_doc } =
@@ -294,7 +294,7 @@ pub mod reader {
294
294
}
295
295
}
296
296
297
- fn next_doc ( & mut self , exp_tag : EbmlEncoderTag ) -> Doc {
297
+ fn next_doc ( & mut self , exp_tag : EbmlEncoderTag ) -> Doc < ' doc > {
298
298
debug ! ( ". next_doc(exp_tag={:?})" , exp_tag) ;
299
299
if self . pos >= self . parent . end {
300
300
fail ! ( "no more documents in current node!" ) ;
@@ -321,7 +321,7 @@ pub mod reader {
321
321
}
322
322
323
323
fn push_doc < T > ( & mut self , exp_tag : EbmlEncoderTag ,
324
- f: |& mut Decoder | -> T ) -> T {
324
+ f: |& mut Decoder < ' doc > | -> T ) -> T {
325
325
let d = self . next_doc( exp_tag) ;
326
326
let old_parent = self . parent;
327
327
let old_pos = self . pos;
@@ -338,10 +338,8 @@ pub mod reader {
338
338
debug ! ( "_next_uint exp_tag={:?} result={}" , exp_tag, r) ;
339
339
r as uint
340
340
}
341
- }
342
341
343
- impl Decoder {
344
- pub fn read_opaque < R > ( & mut self , op : |& mut Decoder , Doc | -> R ) -> R {
342
+ pub fn read_opaque < R > ( & mut self , op : |& mut Decoder < ' doc > , Doc | -> R ) -> R {
345
343
let doc = self . next_doc ( EsOpaque ) ;
346
344
347
345
let ( old_parent, old_pos) = ( self . parent , self . pos ) ;
@@ -356,7 +354,7 @@ pub mod reader {
356
354
}
357
355
}
358
356
359
- impl serialize:: Decoder for Decoder {
357
+ impl < ' doc > serialize:: Decoder for Decoder < ' doc > {
360
358
fn read_nil ( & mut self ) -> ( ) { ( ) }
361
359
362
360
fn read_u64 ( & mut self ) -> u64 { doc_as_u64 ( self . next_doc ( EsU64 ) ) }
@@ -412,7 +410,7 @@ pub mod reader {
412
410
}
413
411
414
412
// Compound types:
415
- fn read_enum < T > ( & mut self , name : & str , f: |& mut Decoder | -> T ) -> T {
413
+ fn read_enum < T > ( & mut self , name : & str , f: |& mut Decoder < ' doc > | -> T ) -> T {
416
414
debug!( "read_enum({})" , name) ;
417
415
self . _check_label( name) ;
418
416
@@ -431,7 +429,7 @@ pub mod reader {
431
429
432
430
fn read_enum_variant < T > ( & mut self ,
433
431
_: & [ & str ] ,
434
- f : |& mut Decoder , uint| -> T )
432
+ f : |& mut Decoder < ' doc > , uint| -> T )
435
433
-> T {
436
434
debug ! ( "read_enum_variant()" ) ;
437
435
let idx = self . _next_uint ( EsEnumVid ) ;
@@ -452,14 +450,14 @@ pub mod reader {
452
450
453
451
fn read_enum_variant_arg < T > ( & mut self ,
454
452
idx : uint ,
455
- f: |& mut Decoder | -> T ) -> T {
453
+ f: |& mut Decoder < ' doc > | -> T ) -> T {
456
454
debug!( "read_enum_variant_arg(idx={})" , idx) ;
457
455
f ( self )
458
456
}
459
457
460
458
fn read_enum_struct_variant < T > ( & mut self ,
461
459
_: & [ & str ] ,
462
- f : |& mut Decoder , uint| -> T )
460
+ f : |& mut Decoder < ' doc > , uint| -> T )
463
461
-> T {
464
462
debug ! ( "read_enum_struct_variant()" ) ;
465
463
let idx = self . _next_uint ( EsEnumVid ) ;
@@ -481,7 +479,7 @@ pub mod reader {
481
479
fn read_enum_struct_variant_field < T > ( & mut self ,
482
480
name : & str ,
483
481
idx : uint ,
484
- f: |& mut Decoder | -> T )
482
+ f: |& mut Decoder < ' doc > | -> T )
485
483
-> T {
486
484
debug ! ( "read_enum_struct_variant_arg(name={}, idx={})" , name, idx) ;
487
485
f ( self )
@@ -490,7 +488,7 @@ pub mod reader {
490
488
fn read_struct < T > ( & mut self ,
491
489
name : & str ,
492
490
_: uint ,
493
- f: |& mut Decoder | -> T )
491
+ f: |& mut Decoder < ' doc > | -> T )
494
492
-> T {
495
493
debug ! ( "read_struct(name={})" , name) ;
496
494
f ( self )
@@ -499,41 +497,41 @@ pub mod reader {
499
497
fn read_struct_field < T > ( & mut self ,
500
498
name : & str ,
501
499
idx : uint ,
502
- f: |& mut Decoder | -> T )
500
+ f: |& mut Decoder < ' doc > | -> T )
503
501
-> T {
504
502
debug ! ( "read_struct_field(name={}, idx={})" , name, idx) ;
505
503
self . _check_label ( name) ;
506
504
f ( self )
507
505
}
508
506
509
- fn read_tuple < T > ( & mut self , f : |& mut Decoder , uint| -> T ) -> T {
507
+ fn read_tuple < T > ( & mut self , f : |& mut Decoder < ' doc > , uint| -> T ) -> T {
510
508
debug ! ( "read_tuple()" ) ;
511
509
self . read_seq ( f)
512
510
}
513
511
514
- fn read_tuple_arg < T > ( & mut self , idx : uint , f: |& mut Decoder | -> T )
512
+ fn read_tuple_arg < T > ( & mut self , idx : uint , f: |& mut Decoder < ' doc > | -> T )
515
513
-> T {
516
514
debug ! ( "read_tuple_arg(idx={})" , idx) ;
517
515
self . read_seq_elt ( idx, f)
518
516
}
519
517
520
518
fn read_tuple_struct < T > ( & mut self ,
521
519
name : & str ,
522
- f : |& mut Decoder , uint| -> T )
520
+ f : |& mut Decoder < ' doc > , uint| -> T )
523
521
-> T {
524
522
debug ! ( "read_tuple_struct(name={})" , name) ;
525
523
self . read_tuple ( f)
526
524
}
527
525
528
526
fn read_tuple_struct_arg < T > ( & mut self ,
529
527
idx : uint ,
530
- f: |& mut Decoder | -> T )
528
+ f: |& mut Decoder < ' doc > | -> T )
531
529
-> T {
532
530
debug ! ( "read_tuple_struct_arg(idx={})" , idx) ;
533
531
self . read_tuple_arg ( idx, f)
534
532
}
535
533
536
- fn read_option < T > ( & mut self , f : |& mut Decoder , bool| -> T ) -> T {
534
+ fn read_option < T > ( & mut self , f : |& mut Decoder < ' doc > , bool| -> T ) -> T {
537
535
debug ! ( "read_option()" ) ;
538
536
self . read_enum ( "Option" , |this| {
539
537
this. read_enum_variant ( [ "None" , "Some" ] , |this, idx| {
@@ -546,7 +544,7 @@ pub mod reader {
546
544
} )
547
545
}
548
546
549
- fn read_seq < T > ( & mut self , f : |& mut Decoder , uint| -> T ) -> T {
547
+ fn read_seq < T > ( & mut self , f : |& mut Decoder < ' doc > , uint| -> T ) -> T {
550
548
debug ! ( "read_seq()" ) ;
551
549
self . push_doc ( EsVec , |d| {
552
550
let len = d. _next_uint ( EsVecLen ) ;
@@ -555,13 +553,13 @@ pub mod reader {
555
553
} )
556
554
}
557
555
558
- fn read_seq_elt < T > ( & mut self , idx : uint , f: |& mut Decoder | -> T )
556
+ fn read_seq_elt < T > ( & mut self , idx : uint , f: |& mut Decoder < ' doc > | -> T )
559
557
-> T {
560
558
debug ! ( "read_seq_elt(idx={})" , idx) ;
561
559
self . push_doc ( EsVecElt , f)
562
560
}
563
561
564
- fn read_map < T > ( & mut self , f : |& mut Decoder , uint| -> T ) -> T {
562
+ fn read_map < T > ( & mut self , f : |& mut Decoder < ' doc > , uint| -> T ) -> T {
565
563
debug ! ( "read_map()" ) ;
566
564
self . push_doc ( EsMap , |d| {
567
565
let len = d. _next_uint ( EsMapLen ) ;
@@ -570,13 +568,13 @@ pub mod reader {
570
568
} )
571
569
}
572
570
573
- fn read_map_elt_key < T > ( & mut self , idx : uint , f: |& mut Decoder | -> T )
571
+ fn read_map_elt_key < T > ( & mut self , idx : uint , f: |& mut Decoder < ' doc > | -> T )
574
572
-> T {
575
573
debug ! ( "read_map_elt_key(idx={})" , idx) ;
576
574
self . push_doc ( EsMapKey , f)
577
575
}
578
576
579
- fn read_map_elt_val < T > ( & mut self , idx : uint , f: |& mut Decoder | -> T )
577
+ fn read_map_elt_val < T > ( & mut self , idx : uint , f: |& mut Decoder < ' doc > | -> T )
580
578
-> T {
581
579
debug ! ( "read_map_elt_val(idx={})" , idx) ;
582
580
self . push_doc ( EsMapVal , f)
@@ -953,7 +951,7 @@ mod tests {
953
951
let wr = @mut MemWriter :: new ( ) ;
954
952
let mut ebml_w = writer:: Encoder ( wr) ;
955
953
v. encode ( & mut ebml_w) ;
956
- let ebml_doc = reader:: Doc ( @ wr. inner_ref ( ) . to_owned ( ) ) ;
954
+ let ebml_doc = reader:: Doc ( * wr. inner_ref ( ) ) ;
957
955
let mut deser = reader:: Decoder ( ebml_doc) ;
958
956
let v1 = serialize:: Decodable :: decode ( & mut deser) ;
959
957
debug ! ( "v1 == {:?}" , v1) ;
0 commit comments