@@ -1330,6 +1330,87 @@ impl<T> *mut [T] {
1330
1330
}
1331
1331
}
1332
1332
1333
+ #[ cfg( not( bootstrap) ) ]
1334
+ #[ lang = "mut_str_ptr" ]
1335
+ impl * mut str {
1336
+ /// Returns the length of a raw string slice.
1337
+ ///
1338
+ /// The returned value is the number of **bytes**, not the number of characters.
1339
+ ///
1340
+ /// This function is safe, even when the raw string slice cannot be cast to a slice
1341
+ /// reference because the pointer is null or unaligned.
1342
+ ///
1343
+ /// # Examples
1344
+ ///
1345
+ /// ```rust
1346
+ /// #![feature(str_ptr_len)]
1347
+ /// #![feature(str_from_raw_parts)]
1348
+ ///
1349
+ /// use std::ptr;
1350
+ ///
1351
+ /// let str: *mut str = ptr::str_from_raw_parts_mut(ptr::null_mut(), 3);
1352
+ /// assert_eq!(str.len(), 3);
1353
+ /// ```
1354
+ #[ inline]
1355
+ #[ unstable( feature = "str_ptr_len" , issue = "none" ) ]
1356
+ #[ rustc_const_unstable( feature = "const_str_ptr_len" , issue = "none" ) ]
1357
+ pub const fn len ( self ) -> usize {
1358
+ metadata ( self )
1359
+ }
1360
+
1361
+ /// Returns a raw pointer to the string slice's buffer.
1362
+ ///
1363
+ /// This is equivalent to casting `self` to `*mut u8`, but more type-safe.
1364
+ ///
1365
+ /// # Examples
1366
+ ///
1367
+ /// ```rust
1368
+ /// #![feature(str_ptr_as_ptr)]
1369
+ /// #![feature(str_from_raw_parts)]
1370
+ /// use std::ptr;
1371
+ ///
1372
+ /// let str: *mut str = ptr::str_from_raw_parts_mut(ptr::null_mut(), 3);
1373
+ /// assert_eq!(str.as_mut_ptr(), 0 as *mut u8);
1374
+ /// ```
1375
+ #[ inline]
1376
+ #[ unstable( feature = "str_ptr_as_ptr" , issue = "none" ) ]
1377
+ #[ rustc_const_unstable( feature = "str_ptr_as_ptr" , issue = "none" ) ]
1378
+ pub const fn as_mut_ptr ( self ) -> * mut u8 {
1379
+ self as * mut u8
1380
+ }
1381
+
1382
+ /// Returns a raw pointer to an substring, without doing bounds
1383
+ /// checking.
1384
+ ///
1385
+ /// Calling this method with an out-of-bounds index, index that does not lie on an UTF-8 sequence boundaries or when `self` is not dereferencable
1386
+ /// is *[undefined behavior]* even if the resulting pointer is not used.
1387
+ ///
1388
+ /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1389
+ ///
1390
+ /// # Examples
1391
+ ///
1392
+ /// ```
1393
+ /// #![feature(str_ptr_get)]
1394
+ ///
1395
+ /// let mut x = [b'a', b'b', b'c'];
1396
+ /// let x: &mut str = std::str::from_utf8_mut(&mut x).unwrap();
1397
+ /// let x: *mut str = x as *mut str;
1398
+ ///
1399
+ /// unsafe {
1400
+ /// assert_eq!(&*x.get_unchecked_mut(1..), "bc");
1401
+ /// }
1402
+ /// ```
1403
+ #[ unstable( feature = "str_ptr_get" , issue = "none" ) ]
1404
+ #[ inline]
1405
+ pub unsafe fn get_unchecked_mut < I > ( self , index : I ) -> * mut I :: Output
1406
+ where
1407
+ I : SliceIndex < str > ,
1408
+ {
1409
+ // SAFETY: the caller ensures that `self` is dereferencable, `index` in-bounds and lie on an UTF-8 sequence boundaries.
1410
+ unsafe { index. get_unchecked_mut ( self ) }
1411
+ }
1412
+ }
1413
+
1333
1414
// Equality for pointers
1334
1415
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1335
1416
impl < T : ?Sized > PartialEq for * mut T {
0 commit comments