Skip to content

Commit 966f4a9

Browse files
committed
add ctor/dtors: new_in, from_raw_parts_in, etc
Yes, you could just use `unsafe { from_utf8_unchecked }``, but people get antsy about `unsafe`, so add some Vec ctor/dtor equivalents.
1 parent cf3bd16 commit 966f4a9

1 file changed

Lines changed: 179 additions & 0 deletions

File tree

library/alloc/src/string.rs

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,97 @@ impl String {
938938
}
939939

940940
impl<A: Allocator> generic::String<A> {
941+
/// Creates a new empty `String`.
942+
///
943+
/// Given that the `String` is empty, this will not allocate any initial
944+
/// buffer. While that means that this initial operation is very
945+
/// inexpensive, it may cause excessive allocation later when you add
946+
/// data. If you have an idea of how much data the `String` will hold,
947+
/// consider the [`with_capacity_in`] method to prevent excessive
948+
/// re-allocation.
949+
///
950+
/// [`with_capacity_in`]: String::with_capacity_in
951+
///
952+
/// # Examples
953+
///
954+
/// ```
955+
/// #![feature(allocator_api)]
956+
///
957+
/// use std::alloc::System;
958+
/// use std::string::generic::String;
959+
///
960+
/// # #[allow(unused_mut)]
961+
/// let mut s = String::new_in(System);
962+
/// ```
963+
#[inline]
964+
#[unstable(feature = "allocator_api", issue = "32838")]
965+
#[must_use]
966+
pub const fn new_in(alloc: A) -> Self {
967+
generic::String { vec: Vec::new_in(alloc) }
968+
}
969+
970+
/// Creates a new empty `String` with at least the specified capacity in the specified allocator.
971+
///
972+
/// `String`s have an internal buffer to hold their data. The capacity is
973+
/// the length of that buffer, and can be queried with the [`capacity`]
974+
/// method. This method creates an empty `String`, but one with an initial
975+
/// buffer that can hold at least `capacity` bytes. This is useful when you
976+
/// may be appending a bunch of data to the `String`, reducing the number of
977+
/// reallocations it needs to do.
978+
///
979+
/// [`capacity`]: String::capacity
980+
///
981+
/// If the given capacity is `0`, no allocation will occur, and this method
982+
/// is identical to the [`new_in`] method.
983+
///
984+
/// [`new_in`]: String::new_in
985+
///
986+
/// # Examples
987+
///
988+
/// ```
989+
/// #![feature(allocator_api)]
990+
///
991+
/// use std::alloc::System;
992+
/// use std::string::generic::String;
993+
///
994+
/// let mut s = String::with_capacity_in(10, System);
995+
///
996+
/// // The String contains no chars, even though it has capacity for more
997+
/// assert_eq!(s.len(), 0);
998+
///
999+
/// // These are all done without reallocating...
1000+
/// let cap = s.capacity();
1001+
/// for _ in 0..10 {
1002+
/// s.push('a');
1003+
/// }
1004+
///
1005+
/// assert_eq!(s.capacity(), cap);
1006+
///
1007+
/// // ...but this may make the string reallocate
1008+
/// s.push('a');
1009+
/// ```
1010+
#[inline]
1011+
#[cfg(not(no_global_oom_handling))]
1012+
#[unstable(feature = "allocator_api", issue = "32838")]
1013+
#[must_use]
1014+
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
1015+
generic::String { vec: Vec::with_capacity_in(capacity, alloc) }
1016+
}
1017+
1018+
/// Creates a new empty `String` with at least the specified capacity, in the specified allocator.
1019+
///
1020+
/// # Errors
1021+
///
1022+
/// Returns [`Err`] if the capacity exceeds `isize::MAX` bytes,
1023+
/// or if the memory allocator reports failure.
1024+
///
1025+
#[inline]
1026+
#[unstable(feature = "allocator_api", issue = "32838")]
1027+
// #[unstable(feature = "try_with_capacity", issue = "91913")]
1028+
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
1029+
Ok(generic::String { vec: Vec::try_with_capacity_in(capacity, alloc)? })
1030+
}
1031+
9411032
/// Converts a vector of bytes to a `String`.
9421033
///
9431034
/// A string ([`String`]) is made of bytes ([`u8`]), and a vector of bytes
@@ -2202,6 +2293,94 @@ impl<A: Allocator> generic::String<A> {
22022293
unsafe { from_boxed_utf8_unchecked(slice) }
22032294
}
22042295

2296+
/// Decomposes a `String` into its raw components: `(pointer, length, capacity, allocator)`.
2297+
///
2298+
/// Returns the raw pointer to the underlying data, the length of
2299+
/// the string (in bytes), the allocated capacity of the data
2300+
/// (in bytes), and the allocator storing the bytes. These are the same arguments in the same order as
2301+
/// the arguments to [`from_raw_parts_in`].
2302+
///
2303+
/// After calling this function, the caller is responsible for the
2304+
/// memory previously managed by the `String`. The only way to do
2305+
/// this is to convert the raw pointer, length, and capacity back
2306+
/// into a `String` with the [`from_raw_parts_in`] function, allowing
2307+
/// the destructor to perform the cleanup.
2308+
///
2309+
/// [`from_raw_parts_in`]: String::from_raw_parts_in
2310+
///
2311+
/// # Examples
2312+
///
2313+
/// ```
2314+
/// #![feature(allocator_api)]
2315+
///
2316+
/// use std::alloc::System;
2317+
/// use std::string::generic::String;
2318+
///
2319+
/// let mut s = String::new_in(System);
2320+
/// s.push_str("hello");
2321+
///
2322+
/// let (ptr, len, cap, alloc) = s.into_raw_parts_with_alloc();
2323+
///
2324+
/// let rebuilt = unsafe { String::from_raw_parts_in(ptr, len, cap, alloc) };
2325+
/// assert_eq!(rebuilt, "hello");
2326+
/// ```
2327+
#[inline]
2328+
#[must_use = "losing the pointer will leak memory"]
2329+
#[unstable(feature = "allocator_api", issue = "32838")]
2330+
pub fn into_raw_parts_with_alloc(self) -> (*mut u8, usize, usize, A) {
2331+
self.vec.into_raw_parts_with_alloc()
2332+
}
2333+
2334+
/// Creates a new `String` from a pointer, a length, a capacity, and an allocator.
2335+
///
2336+
/// # Safety
2337+
///
2338+
/// This is highly unsafe, due to the number of invariants that aren't
2339+
/// checked:
2340+
///
2341+
/// * all safety requirements for [`Vec::<u8>::from_raw_parts_in`].
2342+
/// * all safety requirements for [`String::from_utf8_unchecked`].
2343+
///
2344+
/// Violating these may cause problems like corrupting the allocator's
2345+
/// internal data structures. For example, it is normally **not** safe to
2346+
/// build a `String` from a pointer to a C `char` array containing UTF-8
2347+
/// _unless_ you are certain that array is [*currently allocated*] via the given allocator `alloc`.
2348+
///
2349+
/// The ownership of `buf` is effectively transferred to the
2350+
/// `String` which may then deallocate, reallocate or change the
2351+
/// contents of memory pointed to by the pointer at will. Ensure
2352+
/// that nothing else uses the pointer after calling this
2353+
/// function.
2354+
///
2355+
/// # Examples
2356+
///
2357+
/// ```
2358+
/// #![feature(allocator_api)]
2359+
///
2360+
/// use std::alloc::System;
2361+
/// use std::string::generic::String;
2362+
///
2363+
/// let mut s = String::new_in(System);
2364+
/// s.push_str("hello");
2365+
///
2366+
/// // Deconstruct the String into parts.
2367+
/// let (ptr, len, capacity, alloc) = s.into_raw_parts_with_alloc();
2368+
///
2369+
/// let rebuilt = unsafe { String::from_raw_parts_in(ptr, len, capacity, alloc) };
2370+
/// assert_eq!(rebuilt, "hello");
2371+
/// ```
2372+
#[inline]
2373+
#[unstable(feature = "allocator_api", issue = "32838")]
2374+
pub unsafe fn from_raw_parts_in(
2375+
ptr: *mut u8,
2376+
length: usize,
2377+
capacity: usize,
2378+
alloc: A,
2379+
) -> Self {
2380+
let vec = unsafe { Vec::from_raw_parts_in(ptr, length, capacity, alloc) };
2381+
generic::String { vec }
2382+
}
2383+
22052384
/// Consumes and leaks the `String`, returning a mutable reference to the contents,
22062385
/// `&'a mut str`.
22072386
///

0 commit comments

Comments
 (0)