Skip to content

Commit b077aa4

Browse files
committed
liballoc/{String,Vec}: constify existing functions instead
1 parent c4af1ae commit b077aa4

File tree

3 files changed

+25
-145
lines changed

3 files changed

+25
-145
lines changed

library/alloc/src/raw_vec.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -293,16 +293,8 @@ impl<T, A: Allocator> RawVec<T, A> {
293293
/// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must
294294
/// be careful.
295295
#[inline]
296-
pub fn ptr(&self) -> *mut T {
297-
self.inner.ptr()
298-
}
299-
300-
/// Gets a const raw pointer to the start of the allocation. Note that this is
301-
/// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must
302-
/// be careful.
303-
#[inline]
304-
pub const fn ptr_const(&self) -> *mut T {
305-
self.ptr.as_ptr()
296+
pub const fn ptr(&self) -> *mut T {
297+
self.inner.as_ptr()
306298
}
307299

308300
#[inline]
@@ -314,7 +306,7 @@ impl<T, A: Allocator> RawVec<T, A> {
314306
///
315307
/// This will always be `usize::MAX` if `T` is zero-sized.
316308
#[inline]
317-
pub fn capacity(&self) -> usize {
309+
pub const fn capacity(&self) -> usize {
318310
self.inner.capacity(size_of::<T>())
319311
}
320312

library/alloc/src/string.rs

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,8 @@ impl String {
10061006
#[inline]
10071007
#[must_use = "`self` will be dropped if the result is not used"]
10081008
#[stable(feature = "rust1", since = "1.0.0")]
1009-
pub fn into_bytes(self) -> Vec<u8> {
1009+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
1010+
pub const fn into_bytes(self) -> Vec<u8> {
10101011
self.vec
10111012
}
10121013

@@ -1022,38 +1023,8 @@ impl String {
10221023
#[inline]
10231024
#[must_use]
10241025
#[stable(feature = "string_as_str", since = "1.7.0")]
1025-
pub fn as_str(&self) -> &str {
1026-
self.as_str_const()
1027-
}
1028-
1029-
/// Extracts a const string slice containing the entire `String`.
1030-
///
1031-
/// # Examples
1032-
///
1033-
/// ```
1034-
/// # use std::borrow::Cow;
1035-
/// // `Deref`, `AsRef`, and `as_str` are not available in `const` contexts, so this doesn't otherwise work.
1036-
/// const fn cow_str<'c, 's>(c: &Cow<'s, str>) -> &'c str
1037-
/// where 's: 'c {
1038-
/// match c {
1039-
/// Cow::Borrowed(s) => s,
1040-
/// Cow::Owned(s) => s.as_str_const(),
1041-
/// }
1042-
/// }
1043-
///
1044-
/// const STRING: Cow<'static, str> = Cow::Owned(String::new());
1045-
/// const STR: Cow<'static, str> = Cow::Borrowed("foo");
1046-
///
1047-
/// const SLICED_STRING: &'static str = cow_str(&STRING);
1048-
/// const SLICED_STR: &'static str = cow_str(&STR);
1049-
///
1050-
/// assert_eq!(SLICED_STRING, "");
1051-
/// assert_eq!(SLICED_STR, "foo");
1052-
/// ```
1053-
#[inline]
1054-
#[must_use]
1055-
#[unstable(feature = "const_vec_string_slice", issue = "none")]
1056-
pub const fn as_str_const(&self) -> &str {
1026+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
1027+
pub const fn as_str(&self) -> &str {
10571028
unsafe { str::from_utf8_unchecked(self.vec.as_slice_const()) }
10581029
}
10591030

@@ -1143,7 +1114,8 @@ impl String {
11431114
#[inline]
11441115
#[must_use]
11451116
#[stable(feature = "rust1", since = "1.0.0")]
1146-
pub fn capacity(&self) -> usize {
1117+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
1118+
pub const fn capacity(&self) -> usize {
11471119
self.vec.capacity()
11481120
}
11491121

@@ -1406,7 +1378,8 @@ impl String {
14061378
#[inline]
14071379
#[must_use]
14081380
#[stable(feature = "rust1", since = "1.0.0")]
1409-
pub fn as_bytes(&self) -> &[u8] {
1381+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
1382+
pub const fn as_bytes(&self) -> &[u8] {
14101383
&self.vec
14111384
}
14121385

@@ -1779,8 +1752,9 @@ impl String {
17791752
#[inline]
17801753
#[must_use]
17811754
#[stable(feature = "rust1", since = "1.0.0")]
1755+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
17821756
#[rustc_confusables("length", "size")]
1783-
pub fn len(&self) -> usize {
1757+
pub const fn len(&self) -> usize {
17841758
self.vec.len()
17851759
}
17861760

@@ -1798,7 +1772,8 @@ impl String {
17981772
#[inline]
17991773
#[must_use]
18001774
#[stable(feature = "rust1", since = "1.0.0")]
1801-
pub fn is_empty(&self) -> bool {
1775+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
1776+
pub const fn is_empty(&self) -> bool {
18021777
self.len() == 0
18031778
}
18041779

library/alloc/src/vec/mod.rs

Lines changed: 10 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,8 @@ impl<T, A: Allocator> Vec<T, A> {
946946
/// ```
947947
#[inline]
948948
#[stable(feature = "rust1", since = "1.0.0")]
949-
pub fn capacity(&self) -> usize {
949+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
950+
pub const fn capacity(&self) -> usize {
950951
self.buf.capacity()
951952
}
952953

@@ -1253,37 +1254,8 @@ impl<T, A: Allocator> Vec<T, A> {
12531254
/// ```
12541255
#[inline]
12551256
#[stable(feature = "vec_as_slice", since = "1.7.0")]
1256-
pub fn as_slice(&self) -> &[T] {
1257-
self.as_slice_const()
1258-
}
1259-
1260-
/// Extracts a slice containing the entire vector.
1261-
///
1262-
/// # Examples
1263-
///
1264-
/// ```
1265-
/// # use std::borrow::Cow;
1266-
/// // `Deref`, `AsRef`, and `as_slice` are not available in `const` contexts, so this doesn't otherwise work.
1267-
/// const fn cow_slice<'c, 's>(c: &Cow<'s, [u8]>) -> &'c [u8]
1268-
/// where 's: 'c {
1269-
/// match c {
1270-
/// Cow::Borrowed(s) => s,
1271-
/// Cow::Owned(s) => s.as_slice_const(),
1272-
/// }
1273-
/// }
1274-
///
1275-
/// const VEC: Cow<'static, [u8]> = Cow::Owned(Vec::new());
1276-
/// const SLICE: Cow<'static, [u8]> = Cow::Borrowed(b"foo");
1277-
///
1278-
/// const SLICED_VEC: &'static [u8] = cow_slice(&VEC);
1279-
/// const SLICED_SLICE: &'static [u8] = cow_slice(&SLICE);
1280-
///
1281-
/// assert_eq!(SLICED_VEC, b"");
1282-
/// assert_eq!(SLICED_SLICE, b"foo");
1283-
/// ```
1284-
#[inline]
1285-
#[unstable(feature = "const_vec_string_slice", issue = "none")]
1286-
pub const fn as_slice_const(&self) -> &[T] {
1257+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
1258+
pub const fn as_slice(&self) -> &[T] {
12871259
unsafe { slice::from_raw_parts(self.as_ptr_const(), self.len) }
12881260
}
12891261

@@ -1356,76 +1328,15 @@ impl<T, A: Allocator> Vec<T, A> {
13561328
/// [`as_mut_ptr`]: Vec::as_mut_ptr
13571329
/// [`as_ptr`]: Vec::as_ptr
13581330
#[stable(feature = "vec_as_ptr", since = "1.37.0")]
1331+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
13591332
#[rustc_never_returns_null_ptr]
13601333
#[inline]
1361-
pub fn as_ptr(&self) -> *const T {
1334+
pub const fn as_ptr(&self) -> *const T {
13621335
// We shadow the slice method of the same name to avoid going through
13631336
// `deref`, which creates an intermediate reference.
13641337
self.buf.ptr()
13651338
}
13661339

1367-
/// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
1368-
/// valid for zero sized reads if the vector didn't allocate.
1369-
///
1370-
/// This is a `const` version of [`as_ptr`].
1371-
///
1372-
/// The caller must ensure that the vector outlives the pointer this
1373-
/// function returns, or else it will end up pointing to garbage.
1374-
/// Modifying the vector may cause its buffer to be reallocated,
1375-
/// which would also make any pointers to it invalid.
1376-
///
1377-
/// The caller must also ensure that the memory the pointer (non-transitively) points to
1378-
/// is never written to (except inside an `UnsafeCell`) using this pointer or any pointer
1379-
/// derived from it. If you need to mutate the contents of the slice, use [`as_mut_ptr`].
1380-
///
1381-
/// This method guarantees that for the purpose of the aliasing model, this method
1382-
/// does not materialize a reference to the underlying slice, and thus the returned pointer
1383-
/// will remain valid when mixed with other calls to [`as_ptr`] and [`as_mut_ptr`].
1384-
/// Note that calling other methods that materialize mutable references to the slice,
1385-
/// or mutable references to specific elements you are planning on accessing through this pointer,
1386-
/// as well as writing to those elements, may still invalidate this pointer.
1387-
/// See the second example below for how this guarantee can be used.
1388-
///
1389-
///
1390-
/// # Examples
1391-
///
1392-
/// ```
1393-
/// let x = vec![1, 2, 4];
1394-
/// let x_ptr = x.as_ptr_const();
1395-
///
1396-
/// unsafe {
1397-
/// for i in 0..x.len() {
1398-
/// assert_eq!(*x_ptr.add(i), 1 << i);
1399-
/// }
1400-
/// }
1401-
/// ```
1402-
///
1403-
/// Due to the aliasing guarantee, the following code is legal:
1404-
///
1405-
/// ```rust
1406-
/// unsafe {
1407-
/// let mut v = vec![0, 1, 2];
1408-
/// let ptr1 = v.as_ptr_const();
1409-
/// let _ = ptr1.read();
1410-
/// let ptr2 = v.as_mut_ptr().offset(2);
1411-
/// ptr2.write(2);
1412-
/// // Notably, the write to `ptr2` did *not* invalidate `ptr1`
1413-
/// // because it mutated a different element:
1414-
/// let _ = ptr1.read();
1415-
/// }
1416-
/// ```
1417-
///
1418-
/// [`as_mut_ptr`]: Vec::as_mut_ptr
1419-
/// [`as_ptr_const`]: Vec::as_ptr
1420-
#[rustc_never_returns_null_ptr]
1421-
#[unstable(feature = "const_vec_string_slice", issue = "none")]
1422-
#[inline]
1423-
pub const fn as_ptr_const(&self) -> *const T {
1424-
// We shadow the slice method of the same name to avoid going through
1425-
// `deref`, which creates an intermediate reference.
1426-
self.buf.ptr_const()
1427-
}
1428-
14291340
/// Returns an unsafe mutable pointer to the vector's buffer, or a dangling
14301341
/// raw pointer valid for zero sized reads if the vector didn't allocate.
14311342
///
@@ -2354,8 +2265,9 @@ impl<T, A: Allocator> Vec<T, A> {
23542265
/// ```
23552266
#[inline]
23562267
#[stable(feature = "rust1", since = "1.0.0")]
2268+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
23572269
#[rustc_confusables("length", "size")]
2358-
pub fn len(&self) -> usize {
2270+
pub const fn len(&self) -> usize {
23592271
self.len
23602272
}
23612273

@@ -2371,7 +2283,8 @@ impl<T, A: Allocator> Vec<T, A> {
23712283
/// assert!(!v.is_empty());
23722284
/// ```
23732285
#[stable(feature = "rust1", since = "1.0.0")]
2374-
pub fn is_empty(&self) -> bool {
2286+
#[rustc_const_unstable(feature = "const_vec_string_slice")]
2287+
pub const fn is_empty(&self) -> bool {
23752288
self.len() == 0
23762289
}
23772290

0 commit comments

Comments
 (0)