@@ -252,6 +252,58 @@ unsafe impl SliceIndex<str> for ops::Range<usize> {
252
252
}
253
253
}
254
254
255
+ /// Implements substring slicing for arbitrary bounds.
256
+ ///
257
+ /// Returns a slice of the given string bounded by the byte indices
258
+ /// provided by each bound.
259
+ ///
260
+ /// This operation is *O*(1).
261
+ ///
262
+ /// # Panics
263
+ ///
264
+ /// Panics if `begin` or `end` (if it exists and once adjusted for
265
+ /// inclusion/exclusion) does not point to the starting byte offset of
266
+ /// a character (as defined by `is_char_boundary`), if `begin > end`, or if
267
+ /// `end > len`.
268
+ #[ stable( feature = "slice_index_str_with_ops_bound_pair" , since = "CURRENT_RUSTC_VERSION" ) ]
269
+ unsafe impl SliceIndex < str > for ( ops:: Bound < usize > , ops:: Bound < usize > ) {
270
+ type Output = str ;
271
+
272
+ #[ inline]
273
+ fn get ( self , slice : & str ) -> Option < & str > {
274
+ crate :: slice:: index:: into_range ( slice. len ( ) , self ) ?. get ( slice)
275
+ }
276
+
277
+ #[ inline]
278
+ fn get_mut ( self , slice : & mut str ) -> Option < & mut str > {
279
+ crate :: slice:: index:: into_range ( slice. len ( ) , self ) ?. get_mut ( slice)
280
+ }
281
+
282
+ #[ inline]
283
+ unsafe fn get_unchecked ( self , slice : * const str ) -> * const str {
284
+ let len = ( slice as * const [ u8 ] ) . len ( ) ;
285
+ // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
286
+ unsafe { crate :: slice:: index:: into_range_unchecked ( len, self ) . get_unchecked ( slice) }
287
+ }
288
+
289
+ #[ inline]
290
+ unsafe fn get_unchecked_mut ( self , slice : * mut str ) -> * mut str {
291
+ let len = ( slice as * mut [ u8 ] ) . len ( ) ;
292
+ // SAFETY: the caller has to uphold the safety contract for `get_unchecked_mut`.
293
+ unsafe { crate :: slice:: index:: into_range_unchecked ( len, self ) . get_unchecked_mut ( slice) }
294
+ }
295
+
296
+ #[ inline]
297
+ fn index ( self , slice : & str ) -> & str {
298
+ crate :: slice:: index:: into_slice_range ( slice. len ( ) , self ) . index ( slice)
299
+ }
300
+
301
+ #[ inline]
302
+ fn index_mut ( self , slice : & mut str ) -> & mut str {
303
+ crate :: slice:: index:: into_slice_range ( slice. len ( ) , self ) . index_mut ( slice)
304
+ }
305
+ }
306
+
255
307
/// Implements substring slicing with syntax `&self[.. end]` or `&mut
256
308
/// self[.. end]`.
257
309
///
0 commit comments