Same as [`std::string::String::into_boxed_str`] but returns an error on allocation failure.
(mut self)
| 283 | /// Same as [`std::string::String::into_boxed_str`] but returns an error on |
| 284 | /// allocation failure. |
| 285 | pub fn into_boxed_str(mut self) -> Result<Box<str>, OutOfMemory> { |
| 286 | self.shrink_to_fit()?; |
| 287 | |
| 288 | let (ptr, len, cap) = self.into_raw_parts(); |
| 289 | debug_assert_eq!(len, cap); |
| 290 | let ptr = str_ptr_from_raw_parts(ptr, len); |
| 291 | |
| 292 | // SAFETY: The `ptr` is allocated with the global allocator and points |
| 293 | // to a valid block of utf8. |
| 294 | let boxed = unsafe { Box::from_raw(ptr) }; |
| 295 | |
| 296 | Ok(boxed) |
| 297 | } |
| 298 | } |