1- use std:: { cell:: OnceCell , marker:: PhantomData , rc :: Rc } ;
1+ use std:: { cell:: OnceCell , marker:: PhantomData } ;
22
3- use crate :: { helpers:: SourceText , work_context:: { PooledVec , WorkContext } } ;
3+ use crate :: {
4+ helpers:: SourceText ,
5+ work_context:: { PooledUsizeVec , WorkContext } ,
6+ } ;
47
58#[ derive( Debug ) ]
6- pub struct WithIndices < ' a , S >
9+ pub struct WithIndices < ' context , ' text , S >
710where
8- S : SourceText < ' a > ,
11+ S : SourceText < ' text > ,
912{
1013 /// line is a string reference
1114 pub line : S ,
1215 /// the byte position of each `char` in `line` string slice .
13- pub indices_indexes : OnceCell < PooledVec > ,
14- work_context : Rc < WorkContext > ,
15- data : PhantomData < & ' a S > ,
16+ pub indices_indexes : OnceCell < PooledUsizeVec < ' context > > ,
17+ work_context : & ' context WorkContext ,
18+ data : PhantomData < & ' text S > ,
1619}
1720
18- impl < ' a , S > WithIndices < ' a , S >
21+ impl < ' context , ' text , S > WithIndices < ' context , ' text , S >
1922where
20- S : SourceText < ' a > ,
23+ S : SourceText < ' text > ,
2124{
22- pub fn new ( work_context : Rc < WorkContext > , line : S ) -> Self {
25+ pub fn new ( work_context : & ' context WorkContext , line : S ) -> Self {
2326 Self {
2427 indices_indexes : OnceCell :: new ( ) ,
2528 line,
3437 return S :: default ( ) ;
3538 }
3639
37- let indices_indexes = & * self . indices_indexes . get_or_init ( || {
38- let mut vec = PooledVec :: new ( self . work_context . clone ( ) , self . line . len ( ) ) ;
39- for ( i, _) in self . line . char_indices ( ) {
40- vec. push ( i) ;
41- }
40+ let indices_indexes = self . indices_indexes . get_or_init ( || {
41+ let mut vec = PooledUsizeVec :: new ( self . work_context , self . line . len ( ) ) ;
42+ vec. extend ( self . line . char_indices ( ) . map ( |( i, _) | i) ) ;
4243 vec
4344 } ) ;
4445
@@ -59,42 +60,55 @@ where
5960/// tests are just copy from `substring` crate
6061#[ cfg( test) ]
6162mod tests {
62- use std:: rc:: Rc ;
63-
64- use crate :: { work_context:: WorkContext , Rope } ;
63+ use crate :: { work_context:: WorkContext , Rope } ;
6564
6665 use super :: WithIndices ;
6766 #[ test]
6867 fn test_substring ( ) {
6968 assert_eq ! (
70- WithIndices :: new( Rc :: new( WorkContext :: default ( ) ) , Rope :: from( "foobar" ) ) . substring( 0 , 3 ) ,
69+ WithIndices :: new( & WorkContext :: default ( ) , Rope :: from( "foobar" ) )
70+ . substring( 0 , 3 ) ,
7171 "foo"
7272 ) ;
7373 }
7474
7575 #[ test]
7676 fn test_out_of_bounds ( ) {
7777 assert_eq ! (
78- WithIndices :: new( Rc :: new( WorkContext :: default ( ) ) , Rope :: from( "foobar" ) ) . substring( 0 , 10 ) ,
78+ WithIndices :: new( & WorkContext :: default ( ) , Rope :: from( "foobar" ) )
79+ . substring( 0 , 10 ) ,
7980 "foobar"
8081 ) ;
81- assert_eq ! ( WithIndices :: new( Rc :: new( WorkContext :: default ( ) ) , Rope :: from( "foobar" ) ) . substring( 6 , 10 ) , "" ) ;
82+ assert_eq ! (
83+ WithIndices :: new( & WorkContext :: default ( ) , Rope :: from( "foobar" ) )
84+ . substring( 6 , 10 ) ,
85+ ""
86+ ) ;
8287 }
8388
8489 #[ test]
8590 fn test_start_less_than_end ( ) {
86- assert_eq ! ( WithIndices :: new( Rc :: new( WorkContext :: default ( ) ) , Rope :: from( "foobar" ) ) . substring( 3 , 2 ) , "" ) ;
91+ assert_eq ! (
92+ WithIndices :: new( & WorkContext :: default ( ) , Rope :: from( "foobar" ) )
93+ . substring( 3 , 2 ) ,
94+ ""
95+ ) ;
8796 }
8897
8998 #[ test]
9099 fn test_start_and_end_equal ( ) {
91- assert_eq ! ( WithIndices :: new( Rc :: new( WorkContext :: default ( ) ) , Rope :: from( "foobar" ) ) . substring( 3 , 3 ) , "" ) ;
100+ assert_eq ! (
101+ WithIndices :: new( & WorkContext :: default ( ) , Rope :: from( "foobar" ) )
102+ . substring( 3 , 3 ) ,
103+ ""
104+ ) ;
92105 }
93106
94107 #[ test]
95108 fn test_multiple_byte_characters ( ) {
96109 assert_eq ! (
97- WithIndices :: new( Rc :: new( WorkContext :: default ( ) ) , Rope :: from( "fõøbα®" ) ) . substring( 2 , 5 ) ,
110+ WithIndices :: new( & WorkContext :: default ( ) , Rope :: from( "fõøbα®" ) )
111+ . substring( 2 , 5 ) ,
98112 "øbα"
99113 ) ;
100114 }
0 commit comments