@@ -1346,6 +1346,93 @@ impl<T> *mut [T] {
1346
1346
}
1347
1347
}
1348
1348
1349
+ #[ cfg( not( bootstrap) ) ]
1350
+ #[ lang = "mut_str_ptr" ]
1351
+ impl * mut str {
1352
+ /// Returns the length of a raw string slice.
1353
+ ///
1354
+ /// The returned value is the number of **bytes**, not the number of characters.
1355
+ ///
1356
+ /// This function is safe, even when the raw string slice cannot be cast to a slice
1357
+ /// reference because the pointer is null or unaligned.
1358
+ ///
1359
+ /// # Examples
1360
+ ///
1361
+ /// ```rust
1362
+ /// #![feature(str_ptr_len)]
1363
+ ///
1364
+ /// let mut arr = [b'a', b'b', b'c'];
1365
+ /// let s: &mut str = std::str::from_utf8_mut(&mut arr).unwrap();
1366
+ /// let s: *mut str = s as *mut str;
1367
+ ///
1368
+ /// assert_eq!(s.len(), 3);
1369
+ /// ```
1370
+ #[ inline]
1371
+ #[ unstable( feature = "str_ptr_len" , issue = "71146" ) ]
1372
+ #[ rustc_const_unstable( feature = "const_str_ptr_len" , issue = "71146" ) ]
1373
+ pub const fn len ( self ) -> usize {
1374
+ metadata ( self )
1375
+ }
1376
+
1377
+ /// Returns a raw pointer to the string slice's buffer.
1378
+ ///
1379
+ /// This is equivalent to casting `self` to `*mut u8`, but more type-safe.
1380
+ ///
1381
+ /// # Examples
1382
+ ///
1383
+ /// ```rust
1384
+ /// #![feature(str_ptr_as_ptr)]
1385
+ ///
1386
+ /// let mut arr = [b'a', b'b', b'c'];
1387
+ /// let s: &mut str = std::str::from_utf8_mut(&mut arr).unwrap();
1388
+ /// let s: *mut str = s as *mut str;
1389
+ ///
1390
+ /// assert_eq!(s.as_mut_ptr(), arr.as_mut_ptr());
1391
+ /// ```
1392
+ #[ inline]
1393
+ #[ unstable( feature = "str_ptr_as_ptr" , issue = "74265" ) ]
1394
+ #[ rustc_const_unstable( feature = "str_ptr_as_ptr" , issue = "74265" ) ]
1395
+ pub const fn as_mut_ptr ( self ) -> * mut u8 {
1396
+ self as * mut u8
1397
+ }
1398
+
1399
+ /// Returns a raw pointer to an substring, without doing bounds
1400
+ /// checking.
1401
+ ///
1402
+ /// # Safety
1403
+ ///
1404
+ /// Calling this method with an out-of-bounds index or when `self` is not dereferenceable
1405
+ /// is *[undefined behavior]* even if the resulting pointer is not used.
1406
+ ///
1407
+ /// [undefined behavior]: https://doc.rust-lang.org/reference/behavior-considered-undefined.html
1408
+ ///
1409
+ /// Note that calling this function with an index that does not lie on an UTF-8 sequence boundaries
1410
+ /// is safe, but dereferencing the pointer returned by such call is unsound.
1411
+ ///
1412
+ /// # Examples
1413
+ ///
1414
+ /// ```
1415
+ /// #![feature(str_ptr_get)]
1416
+ ///
1417
+ /// let mut x = [b'a', b'b', b'c'];
1418
+ /// let x: &mut str = std::str::from_utf8_mut(&mut x).unwrap();
1419
+ /// let x: *mut str = x as *mut str;
1420
+ ///
1421
+ /// unsafe {
1422
+ /// assert_eq!(&*x.get_unchecked_mut(1..), "bc");
1423
+ /// }
1424
+ /// ```
1425
+ #[ unstable( feature = "str_ptr_get" , issue = "74265" ) ]
1426
+ #[ inline]
1427
+ pub unsafe fn get_unchecked_mut < I > ( self , index : I ) -> * mut I :: Output
1428
+ where
1429
+ I : SliceIndex < str > ,
1430
+ {
1431
+ // SAFETY: the caller ensures that `self` is dereferenceable and `index` is in-bounds.
1432
+ unsafe { index. get_unchecked_mut ( self ) }
1433
+ }
1434
+ }
1435
+
1349
1436
// Equality for pointers
1350
1437
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1351
1438
impl < T : ?Sized > PartialEq for * mut T {
0 commit comments