@@ -29,7 +29,7 @@ use marker::Sized;
29
29
use mem;
30
30
use ops:: { Fn , FnMut , FnOnce } ;
31
31
use option:: Option :: { self , None , Some } ;
32
- use raw:: { Repr , Slice } ;
32
+ use raw:: Repr ;
33
33
use result:: Result :: { self , Ok , Err } ;
34
34
use slice:: { self , SliceExt } ;
35
35
@@ -244,6 +244,41 @@ pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
244
244
Ok ( unsafe { from_utf8_unchecked ( v) } )
245
245
}
246
246
247
+ /// Forms a str from a pointer and a length.
248
+ ///
249
+ /// The `len` argument is the number of bytes in the string.
250
+ ///
251
+ /// # Safety
252
+ ///
253
+ /// This function is unsafe as there is no guarantee that the given pointer is
254
+ /// valid for `len` bytes, nor whether the lifetime inferred is a suitable
255
+ /// lifetime for the returned str.
256
+ ///
257
+ /// The data must be valid UTF-8
258
+ ///
259
+ /// `p` must be non-null, even for zero-length str.
260
+ ///
261
+ /// # Caveat
262
+ ///
263
+ /// The lifetime for the returned str is inferred from its usage. To
264
+ /// prevent accidental misuse, it's suggested to tie the lifetime to whichever
265
+ /// source lifetime is safe in the context, such as by providing a helper
266
+ /// function taking the lifetime of a host value for the str, or by explicit
267
+ /// annotation.
268
+ unsafe fn from_raw_parts < ' a > ( p : * const u8 , len : usize ) -> & ' a str {
269
+ from_utf8_unchecked ( slice:: from_raw_parts ( p, len) )
270
+ }
271
+
272
+ /// Performs the same functionality as `from_raw_parts`, except that a mutable
273
+ /// str is returned.
274
+ ///
275
+ /// This function is unsafe for the same reasons as `from_raw_parts`, as well
276
+ /// as not being able to provide a non-aliasing guarantee of the returned
277
+ /// mutable str.
278
+ unsafe fn from_raw_parts_mut < ' a > ( p : * mut u8 , len : usize ) -> & ' a mut str {
279
+ mem:: transmute :: < & mut [ u8 ] , & mut str > ( slice:: from_raw_parts_mut ( p, len) )
280
+ }
281
+
247
282
/// Converts a slice of bytes to a string slice without checking
248
283
/// that the string contains valid UTF-8.
249
284
///
@@ -1677,18 +1712,13 @@ impl StrExt for str {
1677
1712
1678
1713
#[ inline]
1679
1714
unsafe fn slice_unchecked ( & self , begin : usize , end : usize ) -> & str {
1680
- mem:: transmute ( Slice {
1681
- data : self . as_ptr ( ) . offset ( begin as isize ) ,
1682
- len : end - begin,
1683
- } )
1715
+ from_raw_parts ( self . as_ptr ( ) . offset ( begin as isize ) , end - begin)
1684
1716
}
1685
1717
1686
1718
#[ inline]
1687
1719
unsafe fn slice_mut_unchecked ( & mut self , begin : usize , end : usize ) -> & mut str {
1688
- mem:: transmute ( Slice {
1689
- data : self . as_ptr ( ) . offset ( begin as isize ) ,
1690
- len : end - begin,
1691
- } )
1720
+ from_raw_parts_mut ( self . as_ptr ( ) . offset ( begin as isize ) as * mut u8 ,
1721
+ end - begin)
1692
1722
}
1693
1723
1694
1724
#[ inline]
@@ -1843,10 +1873,10 @@ impl StrExt for str {
1843
1873
// is_char_boundary checks that the index is in [0, .len()]
1844
1874
if self . is_char_boundary ( mid) {
1845
1875
let len = self . len ( ) ;
1876
+ let ptr = self . as_ptr ( ) as * mut u8 ;
1846
1877
unsafe {
1847
- let self2: & mut str = mem:: transmute_copy ( & self ) ;
1848
- ( self . slice_mut_unchecked ( 0 , mid) ,
1849
- self2. slice_mut_unchecked ( mid, len) )
1878
+ ( from_raw_parts_mut ( ptr, mid) ,
1879
+ from_raw_parts_mut ( ptr. offset ( mid as isize ) , len - mid) )
1850
1880
}
1851
1881
} else {
1852
1882
slice_error_fail ( self , 0 , mid)
0 commit comments