rust/library/alloc/src
Michael Goulet aee4570adf
Rollup merge of #107429 - tgross35:from-bytes-until-null-stabilization, r=dtolnay
Stabilize feature `cstr_from_bytes_until_nul`

This PR seeks to stabilize `cstr_from_bytes_until_nul`.

Partially addresses #95027

This function has only been on nightly for about 10 months, but I think it is simple enough that there isn't harm discussing stabilization. It has also had at least a handful of mentions on both the user forum and the discord, so it seems like it's already in use or at least known.

This needs FCP still.

Comment on potential discussion points:
- eventual conversion of `CStr` to be a single thin pointer: this function will still be useful to provide a safe way to create a `CStr` after this change.
- should this return a length too, to address concerns about the `CStr` change? I don't see it as being particularly useful, and it seems less ergonomic (i.e. returning `Result<(&CStr, usize), FromBytesUntilNulError>`). I think users that also need this length without the additional `strlen` call are likely better off using a combination of other methods, but this is up for discussion
- `CString::from_vec_until_nul`: this is also useful, but it doesn't even have a nightly implementation merged yet. I propose feature gating that separately, as opposed to blocking this `CStr` implementation on that

Possible alternatives:

A user can use `from_bytes_with_nul` on a slice up to `my_slice[..my_slice.iter().find(|c| c == 0).unwrap()]`. However; that is significantly less ergonomic, and is a bit more work for the compiler to optimize compared the direct `memchr` call that this wraps.

## New stable API

```rs
// both in core::ffi

pub struct FromBytesUntilNulError(());

impl CStr {
    pub const fn from_bytes_until_nul(
        bytes: &[u8]
    ) -> Result<&CStr, FromBytesUntilNulError>
}
```

cc ```@ericseppanen``` original author, ```@Mark-Simulacrum``` original reviewer, ```@m-ou-se``` brought up some issues on the thin pointer CStr

```@rustbot``` modify labels: +T-libs-api +needs-fcp
2023-02-08 20:01:24 -08:00
..
alloc run alloc benchmarks in Miri and fix UB 2022-11-07 10:34:04 +01:00
boxed ThinBox: Add intra-doc-links for Metadata 2023-01-20 08:07:45 +01:00
collections Rollup merge of #105641 - Amanieu:btree_cursor, r=m-ou-se 2023-02-08 18:32:41 +01:00
ffi remove cfg(bootstrap) 2022-09-26 10:14:45 +02:00
raw_vec Add a unit test for zero-sized types in RawVec. 2021-11-26 19:30:45 +11:00
rc Use Box::new() instead of box syntax in alloc tests 2022-05-29 00:41:14 +02:00
slice Update rand in the stdlib tests, and remove the getrandom feature from it 2023-01-04 14:52:41 -08:00
sync Add Arc::into_inner for safely discarding Arcs without calling the destructor on the inner type. 2023-01-22 01:43:25 +09:00
testing Share testing utilities with non-btree test cases 2022-05-02 10:07:50 +02:00
vec vec: Use SpecCloneIntoVec::clone_into to implement Vec::clone_from 2023-01-28 20:37:01 +01:00
alloc.rs Remove various double spaces in source comments. 2023-01-14 17:22:04 +01:00
borrow.rs Remove redundant lifetime bound from impl Borrow for Cow 2022-07-22 01:35:39 +02:00
boxed.rs Update bootstrap cfg 2022-12-28 09:18:43 -05:00
fmt.rs Don't re-export private/unstable ArgumentV1 from alloc. 2023-01-29 20:15:02 +01:00
lib.rs Stabilize feature 'cstr_from_bytes_until_nul' 2023-02-01 02:14:07 -05:00
macros.rs Extra documentation for new formatting feature 2022-08-21 15:28:27 -04:00
raw_vec.rs Make ZST checks in core/alloc more readable 2022-09-22 23:12:29 -07:00
rc.rs fix typo in {Rc, Arc}::get_mut_unchecked docs 2023-01-29 20:11:36 +01:00
slice.rs slice: Add a specialization for clone_into when T is Copy 2023-01-28 20:37:01 +01:00
str.rs Avoid unsafe code in to_ascii_[lower/upper]case() 2023-01-16 01:15:06 +02:00
string.rs Docs: Fix format of headings in String::reserve 2023-02-07 21:32:28 +01:00
sync.rs fix typo in {Rc, Arc}::get_mut_unchecked docs 2023-01-29 20:11:36 +01:00
task.rs Document the conditional existence of alloc::sync and alloc::task. 2022-06-17 20:03:23 -07:00
tests.rs Use implicit capture syntax in format_args 2022-03-10 10:23:40 -05:00