Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking Issue for const_vec_string_slice #129041

Open
3 tasks
GrigorenkoPV opened this issue Aug 13, 2024 · 1 comment
Open
3 tasks

Tracking Issue for const_vec_string_slice #129041

GrigorenkoPV opened this issue Aug 13, 2024 · 1 comment
Labels
A-str Area: str and String C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@GrigorenkoPV
Copy link
Contributor

Feature gate: #![feature(const_vec_string_slice)]

This is a tracking issue for making a bunch of String and Vec methods const.

Public API

The following methods are now const:

impl String {
    pub const fn into_bytes(self) -> Vec<u8>
    pub const fn as_str(&self) -> &str
    pub const fn capacity(&self) -> usize
    pub const fn as_bytes(&self) -> &[u8]
    pub const fn len(&self) -> usize
    pub const fn is_empty(&self) -> bool
}

impl Vec<T> {
    pub const fn capacity(&self) -> usize
    pub const fn as_slice(&self) -> &[T]
    pub const fn as_ptr(&self) -> *const T
    pub const fn len(&self) -> usize
    pub const fn is_empty(&self) -> bool
}

Steps / History

Unresolved Questions

  • None yet.

@rustbot label A-str

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@GrigorenkoPV GrigorenkoPV added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Aug 13, 2024
@rustbot rustbot added the A-str Area: str and String label Aug 13, 2024
@Esper89
Copy link

Esper89 commented Aug 13, 2024

This would be very useful for const fns that sometimes need to handle runtime data too, such as a const fn method that needs to use a Cow<'static, [T]> field.

For example:

struct Foo(Cow<'static, [u32]>);

impl Foo {
    // Initialize with heap-allocated data at runtime.
    fn heap(data: Vec<u32>) -> Self {
        Foo(Cow::Owned(data))
    }

    // Initialize with static data at compile-time.
    const fn static(data: &'static [u32]) -> Self {
        Foo(Cow::Borrowed(data))
    }

    // Access the data, possibly at compile-time, possibly at runtime.
    const fn data(&self) -> &[u32] {
        match self.0 {
            Cow::Borrowed(slice) => slice,
            Cow::Owned(vec) => vec.as_slice(),
        }
    }
}

mammothbane added a commit to mammothbane/rust that referenced this issue Sep 5, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
mammothbane added a commit to mammothbane/rust that referenced this issue Sep 19, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
mammothbane added a commit to mammothbane/rust that referenced this issue Sep 19, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
mammothbane added a commit to mammothbane/rust that referenced this issue Sep 20, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
mammothbane added a commit to mammothbane/rust that referenced this issue Sep 20, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
mammothbane added a commit to mammothbane/rust that referenced this issue Oct 6, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
mammothbane added a commit to mammothbane/rust that referenced this issue Oct 6, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
mammothbane added a commit to mammothbane/rust that referenced this issue Oct 6, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
mammothbane added a commit to mammothbane/rust that referenced this issue Oct 6, 2024
This change `const`-qualifies many methods on Vec and String, notably
`as_slice`, `as_str`, `len`. These changes are made behind the unstable
feature flag `const_vec_string_slice` with the following tracking issue:

rust-lang#129041
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-str Area: str and String C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants