Skip to content

[u8] methods in libcore? #45803

Closed
Closed
@SimonSapin

Description

@SimonSapin

Background on [T] methods

Inherent methods for slices are currently defined in liballoc with:

#[lang(slice)]
impl<T> [T] {
    // …
}

#[lang(slice)] is an escape hatch to allow that impl to exist. Normally it wouldn’t be allowed since liballoc is not the crate that defined slices types. (No such crate exists.) The compiler only allows one #[lang(slice)] item to exist, so we have it in liballoc where it can define every method we want slices to have.

So in #![no_std] crates that do not depend on liballoc, slices have no inherent methods. Instead, libcore has a core::slice::SliceExt trait implemented for [T] that has a subset of the methods and needs to be imported into scope to be used.

Background on [u8] methods

#44042 added inherent methods specific to byte slices. They’re also in liballoc, with a new dedicated lang item:

#[lang(slice_u8)]
impl [u8] {
    // …
}

But they’re not available without liballoc, although they don’t all depend on allocation.

Now

It’d be nice to have some of these [u8] methods without liballoc. The simple thing would be to continue this pattern and add a core::slice::SliceU8Ext trait, but that doesn’t seem like very good design.

Maybe it’s time to revisit SliceExt. Could the “only one #[lang(slice)]” restriction be lifted? Or could we add new core_slice and core_slice_u8 lang items?

CC @eddyb

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-acceptedCategory: A feature request that has been accepted pending implementation.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions