Skip to content

Commit 09dfd2e

Browse files
committed
liballoc: introduce String, Vec const-slicing
1 parent 1ddedba commit 09dfd2e

File tree

3 files changed

+139
-8
lines changed

3 files changed

+139
-8
lines changed

library/alloc/src/raw_vec.rs

+8
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ impl<T, A: Allocator> RawVec<T, A> {
277277
self.ptr.as_ptr()
278278
}
279279

280+
/// Gets a const raw pointer to the start of the allocation. Note that this is
281+
/// `Unique::dangling()` if `capacity == 0` or `T` is zero-sized. In the former case, you must
282+
/// be careful.
283+
#[inline]
284+
pub const fn ptr_const(&self) -> *mut T {
285+
self.ptr.as_ptr()
286+
}
287+
280288
#[inline]
281289
pub fn non_null(&self) -> NonNull<T> {
282290
NonNull::from(self.ptr)

library/alloc/src/string.rs

+35-4
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,38 @@ impl String {
10231023
#[must_use]
10241024
#[stable(feature = "string_as_str", since = "1.7.0")]
10251025
pub fn as_str(&self) -> &str {
1026-
self
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 {
1057+
unsafe { str::from_utf8_unchecked(self.vec.as_slice_const()) }
10271058
}
10281059

10291060
/// Converts a `String` into a mutable string slice.
@@ -1042,7 +1073,7 @@ impl String {
10421073
#[must_use]
10431074
#[stable(feature = "string_as_str", since = "1.7.0")]
10441075
pub fn as_mut_str(&mut self) -> &mut str {
1045-
self
1076+
unsafe { str::from_utf8_unchecked_mut(&mut self.vec) }
10461077
}
10471078

10481079
/// Appends a given string slice onto the end of this `String`.
@@ -2484,7 +2515,7 @@ impl ops::Deref for String {
24842515

24852516
#[inline]
24862517
fn deref(&self) -> &str {
2487-
unsafe { str::from_utf8_unchecked(&self.vec) }
2518+
self.as_str()
24882519
}
24892520
}
24902521

@@ -2495,7 +2526,7 @@ unsafe impl ops::DerefPure for String {}
24952526
impl ops::DerefMut for String {
24962527
#[inline]
24972528
fn deref_mut(&mut self) -> &mut str {
2498-
unsafe { str::from_utf8_unchecked_mut(&mut *self.vec) }
2529+
self.as_mut_str()
24992530
}
25002531
}
25012532

library/alloc/src/vec/mod.rs

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

12581288
/// Extracts a mutable slice of the entire vector.
@@ -1269,7 +1299,7 @@ impl<T, A: Allocator> Vec<T, A> {
12691299
#[inline]
12701300
#[stable(feature = "vec_as_slice", since = "1.7.0")]
12711301
pub fn as_mut_slice(&mut self) -> &mut [T] {
1272-
self
1302+
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
12731303
}
12741304

12751305
/// Returns a raw pointer to the vector's buffer, or a dangling raw pointer
@@ -1332,6 +1362,68 @@ impl<T, A: Allocator> Vec<T, A> {
13321362
self.buf.ptr()
13331363
}
13341364

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

28272919
#[inline]
28282920
fn deref(&self) -> &[T] {
2829-
unsafe { slice::from_raw_parts(self.as_ptr(), self.len) }
2921+
self.as_slice()
28302922
}
28312923
}
28322924

28332925
#[stable(feature = "rust1", since = "1.0.0")]
28342926
impl<T, A: Allocator> ops::DerefMut for Vec<T, A> {
28352927
#[inline]
28362928
fn deref_mut(&mut self) -> &mut [T] {
2837-
unsafe { slice::from_raw_parts_mut(self.as_mut_ptr(), self.len) }
2929+
self.as_mut_slice()
28382930
}
28392931
}
28402932

0 commit comments

Comments
 (0)