@@ -6,15 +6,14 @@ use std::{
6
6
collections:: VecDeque ,
7
7
hash:: Hash ,
8
8
ops:: { Bound , RangeBounds } ,
9
- rc:: Rc ,
10
9
} ;
11
10
12
11
use crate :: Error ;
13
12
14
13
#[ derive( Clone , Debug ) ]
15
14
pub ( crate ) enum Repr < ' a > {
16
15
Light ( & ' a str ) ,
17
- Full ( Rc < Vec < ( & ' a str , usize ) > > ) ,
16
+ Full ( Vec < ( & ' a str , usize ) > ) ,
18
17
}
19
18
20
19
/// A rope data structure.
@@ -43,13 +42,13 @@ impl<'a> Rope<'a> {
43
42
match & mut self . repr {
44
43
Repr :: Light ( s) => {
45
44
let vec = Vec :: from_iter ( [ ( * s, 0 ) , ( value, s. len ( ) ) ] ) ;
46
- self . repr = Repr :: Full ( Rc :: new ( vec) ) ;
45
+ self . repr = Repr :: Full ( vec) ;
47
46
}
48
47
Repr :: Full ( data) => {
49
48
let len = data
50
49
. last ( )
51
50
. map_or ( 0 , |( chunk, start_pos) | * start_pos + chunk. len ( ) ) ;
52
- Rc :: make_mut ( data) . push ( ( value, len) ) ;
51
+ data. push ( ( value, len) ) ;
53
52
}
54
53
}
55
54
}
@@ -61,15 +60,15 @@ impl<'a> Rope<'a> {
61
60
match ( & mut self . repr , value. repr ) {
62
61
( Repr :: Light ( s) , Repr :: Light ( other) ) => {
63
62
let raw = Vec :: from_iter ( [ ( * s, 0 ) , ( other, s. len ( ) ) ] ) ;
64
- self . repr = Repr :: Full ( Rc :: new ( raw) ) ;
63
+ self . repr = Repr :: Full ( raw) ;
65
64
}
66
65
( Repr :: Full ( s) , Repr :: Full ( other) ) => {
67
66
if !other. is_empty ( ) {
68
67
let mut len = s
69
68
. last ( )
70
69
. map_or ( 0 , |( chunk, start_pos) | * start_pos + chunk. len ( ) ) ;
71
70
72
- let cur = Rc :: make_mut ( s ) ;
71
+ let cur = s ;
73
72
cur. reserve_exact ( other. len ( ) ) ;
74
73
75
74
for & ( chunk, _) in other. iter ( ) {
@@ -83,7 +82,7 @@ impl<'a> Rope<'a> {
83
82
let len = s
84
83
. last ( )
85
84
. map_or ( 0 , |( chunk, start_pos) | * start_pos + chunk. len ( ) ) ;
86
- Rc :: make_mut ( s ) . push ( ( other, len) ) ;
85
+ s . push ( ( other, len) ) ;
87
86
}
88
87
}
89
88
( Repr :: Light ( s) , Repr :: Full ( other) ) => {
@@ -94,7 +93,7 @@ impl<'a> Rope<'a> {
94
93
raw. push ( ( chunk, len) ) ;
95
94
len += chunk. len ( ) ;
96
95
}
97
- self . repr = Repr :: Full ( Rc :: new ( raw) ) ;
96
+ self . repr = Repr :: Full ( raw) ;
98
97
}
99
98
}
100
99
}
@@ -325,7 +324,7 @@ impl<'a> Rope<'a> {
325
324
} ) ?;
326
325
327
326
Ok ( Rope {
328
- repr : Repr :: Full ( Rc :: new ( raw) ) ,
327
+ repr : Repr :: Full ( raw) ,
329
328
} )
330
329
}
331
330
}
@@ -413,7 +412,7 @@ impl<'a> Rope<'a> {
413
412
} ) ;
414
413
415
414
Rope {
416
- repr : Repr :: Full ( Rc :: new ( raw) ) ,
415
+ repr : Repr :: Full ( raw) ,
417
416
}
418
417
}
419
418
}
@@ -614,7 +613,7 @@ impl<'a> Iterator for Lines<'_, 'a> {
614
613
// Advance the byte index to the end of the line.
615
614
* byte_idx += len;
616
615
Some ( Rope {
617
- repr : Repr :: Full ( Rc :: new ( raw) ) ,
616
+ repr : Repr :: Full ( raw) ,
618
617
} )
619
618
} else {
620
619
// If we did not find a newline in the next few chunks,
@@ -646,7 +645,7 @@ impl<'a> Iterator for Lines<'_, 'a> {
646
645
// Advance the byte index to the end of the rope.
647
646
* byte_idx += len;
648
647
Some ( Rope {
649
- repr : Repr :: Full ( Rc :: new ( raw) ) ,
648
+ repr : Repr :: Full ( raw) ,
650
649
} )
651
650
}
652
651
}
@@ -864,7 +863,7 @@ impl<'a> FromIterator<&'a str> for Rope<'a> {
864
863
. collect :: < Vec < _ > > ( ) ;
865
864
866
865
Self {
867
- repr : Repr :: Full ( Rc :: new ( raw) ) ,
866
+ repr : Repr :: Full ( raw) ,
868
867
}
869
868
}
870
869
}
@@ -889,8 +888,6 @@ fn end_bound_to_range_end(end: Bound<&usize>) -> Option<usize> {
889
888
890
889
#[ cfg( test) ]
891
890
mod tests {
892
- use std:: rc:: Rc ;
893
-
894
891
use crate :: rope:: { Repr , Rope } ;
895
892
896
893
impl < ' a > PartialEq for Repr < ' a > {
@@ -915,19 +912,19 @@ mod tests {
915
912
assert_eq ! ( simple, "abcdef" ) ;
916
913
assert_eq ! (
917
914
simple. repr,
918
- Repr :: Full ( Rc :: new ( Vec :: from_iter( [ ( "abc" , 0 ) , ( "def" , 3 ) ] ) ) )
915
+ Repr :: Full ( Vec :: from_iter( [ ( "abc" , 0 ) , ( "def" , 3 ) ] ) )
919
916
) ;
920
917
assert_eq ! ( simple. len( ) , 6 ) ;
921
918
922
919
simple. add ( "ghi" ) ;
923
920
assert_eq ! ( simple, "abcdefghi" ) ;
924
921
assert_eq ! (
925
922
simple. repr,
926
- Repr :: Full ( Rc :: new ( Vec :: from_iter( [
923
+ Repr :: Full ( Vec :: from_iter( [
927
924
( "abc" , 0 ) ,
928
925
( "def" , 3 ) ,
929
926
( "ghi" , 6 ) ,
930
- ] ) ) )
927
+ ] ) )
931
928
) ;
932
929
assert_eq ! ( simple. len( ) , 9 ) ;
933
930
}
@@ -946,7 +943,7 @@ mod tests {
946
943
assert_eq ! ( append1, "abcdef" ) ;
947
944
assert_eq ! (
948
945
append1. repr,
949
- Repr :: Full ( Rc :: new ( Vec :: from_iter( [ ( "abc" , 0 ) , ( "def" , 3 ) , ] ) ) )
946
+ Repr :: Full ( Vec :: from_iter( [ ( "abc" , 0 ) , ( "def" , 3 ) , ] ) )
950
947
) ;
951
948
952
949
// simple - complex
@@ -955,12 +952,12 @@ mod tests {
955
952
assert_eq ! ( append2, "abc123" ) ;
956
953
assert_eq ! (
957
954
append2. repr,
958
- Repr :: Full ( Rc :: new ( Vec :: from_iter( [
955
+ Repr :: Full ( Vec :: from_iter( [
959
956
( "abc" , 0 ) ,
960
957
( "1" , 3 ) ,
961
958
( "2" , 4 ) ,
962
959
( "3" , 5 ) ,
963
- ] ) ) )
960
+ ] ) )
964
961
) ;
965
962
966
963
// complex - simple
@@ -969,12 +966,12 @@ mod tests {
969
966
assert_eq ! ( append3, "123abc" ) ;
970
967
assert_eq ! (
971
968
append3. repr,
972
- Repr :: Full ( Rc :: new ( Vec :: from_iter( [
969
+ Repr :: Full ( Vec :: from_iter( [
973
970
( "1" , 0 ) ,
974
971
( "2" , 1 ) ,
975
972
( "3" , 2 ) ,
976
973
( "abc" , 3 ) ,
977
- ] ) ) )
974
+ ] ) )
978
975
) ;
979
976
980
977
// complex - complex
@@ -983,14 +980,14 @@ mod tests {
983
980
assert_eq ! ( append4, "123456" ) ;
984
981
assert_eq ! (
985
982
append4. repr,
986
- Repr :: Full ( Rc :: new ( Vec :: from_iter( [
983
+ Repr :: Full ( Vec :: from_iter( [
987
984
( "1" , 0 ) ,
988
985
( "2" , 1 ) ,
989
986
( "3" , 2 ) ,
990
987
( "4" , 3 ) ,
991
988
( "5" , 4 ) ,
992
989
( "6" , 5 ) ,
993
- ] ) ) )
990
+ ] ) )
994
991
) ;
995
992
}
996
993
@@ -1071,7 +1068,7 @@ mod tests {
1071
1068
assert_eq ! ( rope, "abcdef" ) ;
1072
1069
assert_eq ! (
1073
1070
rope. repr,
1074
- Repr :: Full ( Rc :: new ( Vec :: from_iter( [ ( "abc" , 0 ) , ( "def" , 3 ) ] ) ) )
1071
+ Repr :: Full ( Vec :: from_iter( [ ( "abc" , 0 ) , ( "def" , 3 ) ] ) )
1075
1072
) ;
1076
1073
}
1077
1074
0 commit comments