@@ -54,7 +54,7 @@ use core::ops::Add;
5454use core:: ops:: AddAssign ;
5555#[ cfg( not( no_global_oom_handling) ) ]
5656use core:: ops:: Bound :: { Excluded , Included , Unbounded } ;
57- use core:: ops:: { self , Index , IndexMut , Range , RangeBounds } ;
57+ use core:: ops:: { self , Range , RangeBounds } ;
5858use core:: ptr;
5959use core:: slice;
6060use core:: str:: pattern:: Pattern ;
@@ -2433,100 +2433,26 @@ impl AddAssign<&str> for String {
24332433}
24342434
24352435#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2436- impl ops:: Index < ops:: Range < usize > > for String {
2437- type Output = str ;
2436+ impl < I > ops:: Index < I > for String
2437+ where
2438+ I : slice:: SliceIndex < str > ,
2439+ {
2440+ type Output = I :: Output ;
24382441
24392442 #[ inline]
2440- fn index ( & self , index : ops :: Range < usize > ) -> & str {
2441- & self [ .. ] [ index]
2443+ fn index ( & self , index : I ) -> & I :: Output {
2444+ index . index ( self . as_str ( ) )
24422445 }
24432446}
2444- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2445- impl ops:: Index < ops:: RangeTo < usize > > for String {
2446- type Output = str ;
24472447
2448- #[ inline]
2449- fn index ( & self , index : ops:: RangeTo < usize > ) -> & str {
2450- & self [ ..] [ index]
2451- }
2452- }
24532448#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2454- impl ops:: Index < ops:: RangeFrom < usize > > for String {
2455- type Output = str ;
2456-
2457- #[ inline]
2458- fn index ( & self , index : ops:: RangeFrom < usize > ) -> & str {
2459- & self [ ..] [ index]
2460- }
2461- }
2462- #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2463- impl ops:: Index < ops:: RangeFull > for String {
2464- type Output = str ;
2465-
2466- #[ inline]
2467- fn index ( & self , _index : ops:: RangeFull ) -> & str {
2468- unsafe { str:: from_utf8_unchecked ( & self . vec ) }
2469- }
2470- }
2471- #[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
2472- impl ops:: Index < ops:: RangeInclusive < usize > > for String {
2473- type Output = str ;
2474-
2475- #[ inline]
2476- fn index ( & self , index : ops:: RangeInclusive < usize > ) -> & str {
2477- Index :: index ( & * * self , index)
2478- }
2479- }
2480- #[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
2481- impl ops:: Index < ops:: RangeToInclusive < usize > > for String {
2482- type Output = str ;
2483-
2484- #[ inline]
2485- fn index ( & self , index : ops:: RangeToInclusive < usize > ) -> & str {
2486- Index :: index ( & * * self , index)
2487- }
2488- }
2489-
2490- #[ stable( feature = "derefmut_for_string" , since = "1.3.0" ) ]
2491- impl ops:: IndexMut < ops:: Range < usize > > for String {
2492- #[ inline]
2493- fn index_mut ( & mut self , index : ops:: Range < usize > ) -> & mut str {
2494- & mut self [ ..] [ index]
2495- }
2496- }
2497- #[ stable( feature = "derefmut_for_string" , since = "1.3.0" ) ]
2498- impl ops:: IndexMut < ops:: RangeTo < usize > > for String {
2499- #[ inline]
2500- fn index_mut ( & mut self , index : ops:: RangeTo < usize > ) -> & mut str {
2501- & mut self [ ..] [ index]
2502- }
2503- }
2504- #[ stable( feature = "derefmut_for_string" , since = "1.3.0" ) ]
2505- impl ops:: IndexMut < ops:: RangeFrom < usize > > for String {
2506- #[ inline]
2507- fn index_mut ( & mut self , index : ops:: RangeFrom < usize > ) -> & mut str {
2508- & mut self [ ..] [ index]
2509- }
2510- }
2511- #[ stable( feature = "derefmut_for_string" , since = "1.3.0" ) ]
2512- impl ops:: IndexMut < ops:: RangeFull > for String {
2513- #[ inline]
2514- fn index_mut ( & mut self , _index : ops:: RangeFull ) -> & mut str {
2515- unsafe { str:: from_utf8_unchecked_mut ( & mut * self . vec ) }
2516- }
2517- }
2518- #[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
2519- impl ops:: IndexMut < ops:: RangeInclusive < usize > > for String {
2520- #[ inline]
2521- fn index_mut ( & mut self , index : ops:: RangeInclusive < usize > ) -> & mut str {
2522- IndexMut :: index_mut ( & mut * * self , index)
2523- }
2524- }
2525- #[ stable( feature = "inclusive_range" , since = "1.26.0" ) ]
2526- impl ops:: IndexMut < ops:: RangeToInclusive < usize > > for String {
2449+ impl < I > ops:: IndexMut < I > for String
2450+ where
2451+ I : slice:: SliceIndex < str > ,
2452+ {
25272453 #[ inline]
2528- fn index_mut ( & mut self , index : ops :: RangeToInclusive < usize > ) -> & mut str {
2529- IndexMut :: index_mut ( & mut * * self , index )
2454+ fn index_mut ( & mut self , index : I ) -> & mut I :: Output {
2455+ index . index_mut ( self . as_mut_str ( ) )
25302456 }
25312457}
25322458
0 commit comments