@@ -18,7 +18,6 @@ use serde_bytes::ByteBuf;
18
18
use tokio:: io:: { AsyncRead , ReadBuf } ;
19
19
use triomphe:: Arc ;
20
20
use turbo_tasks_hash:: { DeterministicHash , DeterministicHasher } ;
21
- use unsize:: { CoerceUnsize , Coercion } ;
22
21
23
22
static EMPTY_BUF : & [ u8 ] = & [ ] ;
24
23
@@ -42,7 +41,7 @@ pub struct Rope {
42
41
/// An Arc container for ropes. This indirection allows for easily sharing the
43
42
/// contents between Ropes (and also RopeBuilders/RopeReaders).
44
43
#[ derive( Clone , Debug ) ]
45
- struct InnerRope ( Arc < [ RopeElem ] > ) ;
44
+ struct InnerRope ( Arc < Vec < RopeElem > > ) ;
46
45
47
46
/// Differentiates the types of stored bytes in a rope.
48
47
#[ derive( Clone , Debug ) ]
@@ -111,6 +110,10 @@ impl Rope {
111
110
pub fn to_bytes ( & self ) -> Result < Cow < ' _ , [ u8 ] > > {
112
111
self . data . to_bytes ( self . length )
113
112
}
113
+
114
+ pub fn into_bytes ( self ) -> Result < Cow < ' static , [ u8 ] > > {
115
+ self . data . into_bytes ( self . length )
116
+ }
114
117
}
115
118
116
119
impl From < Vec < u8 > > for Rope {
@@ -139,7 +142,7 @@ impl From<Cow<'static, [u8]>> for Rope {
139
142
} else {
140
143
Rope {
141
144
length : bytes. len ( ) ,
142
- data : InnerRope ( Arc :: from ( [ Local ( bytes) ] ) . unsize ( Coercion :: to_slice ( ) ) ) ,
145
+ data : InnerRope ( Arc :: new ( vec ! [ Local ( bytes) ] ) ) ,
143
146
}
144
147
}
145
148
}
@@ -494,11 +497,30 @@ impl InnerRope {
494
497
}
495
498
}
496
499
}
500
+
501
+ fn into_bytes ( mut self , len : usize ) -> Result < Cow < ' static , [ u8 ] > > {
502
+ if self . 0 . is_empty ( ) {
503
+ return Ok ( Cow :: Borrowed ( EMPTY_BUF ) ) ;
504
+ } else if self . 0 . len ( ) == 1 {
505
+ let data = Arc :: try_unwrap ( self . 0 ) ;
506
+ match data {
507
+ Ok ( data) => return data. into_iter ( ) . next ( ) . unwrap ( ) . into_bytes ( len) ,
508
+ Err ( data) => {
509
+ self . 0 = data;
510
+ }
511
+ }
512
+ }
513
+
514
+ let mut read = RopeReader :: new ( & self , 0 ) ;
515
+ let mut buf = Vec :: with_capacity ( len) ;
516
+ read. read_to_end ( & mut buf) ?;
517
+ Ok ( Cow :: Owned ( buf) )
518
+ }
497
519
}
498
520
499
521
impl Default for InnerRope {
500
522
fn default ( ) -> Self {
501
- InnerRope ( Arc :: new ( [ ] ) . unsize ( Coercion :: to_slice ( ) ) )
523
+ InnerRope ( Arc :: new ( vec ! [ ] ) )
502
524
}
503
525
}
504
526
@@ -537,7 +559,7 @@ impl From<Vec<RopeElem>> for InnerRope {
537
559
}
538
560
539
561
impl Deref for InnerRope {
540
- type Target = Arc < [ RopeElem ] > ;
562
+ type Target = Arc < Vec < RopeElem > > ;
541
563
542
564
fn deref ( & self ) -> & Self :: Target {
543
565
& self . 0
@@ -568,6 +590,13 @@ impl RopeElem {
568
590
_ => None ,
569
591
}
570
592
}
593
+
594
+ fn into_bytes ( self , len : usize ) -> Result < Cow < ' static , [ u8 ] > > {
595
+ match self {
596
+ Local ( bytes) => Ok ( bytes) ,
597
+ Shared ( inner) => inner. into_bytes ( len) ,
598
+ }
599
+ }
571
600
}
572
601
573
602
impl DeterministicHash for RopeElem {
0 commit comments