Skip to content

Move Utf8Chunks::new to be inherent on [u8] #190

Closed
@dylni

Description

@dylni

Proposal

Problem statement

Utf8Chunks::new requires importing Utf8Chunks. Adding this method on [u8] would allow it to be used without an import.

This API is unstable, so this is not a breaking change.

Motivation, use-cases

This is similar to other methods treating [u8] as a byte string, such as <[u8]>::is_ascii.

Example

Code using Utf8Chunks::new:

use std::str::Utf8Chunks;

for chunk in Utf8Chunks::new(string) {
    let valid = chunk.valid();
    let invalid = chunk.invalid();
    // formatting for `valid` and `invalid`
}

Code using <[u8]>::utf8_chunks:

for chunk in string.utf8_chunks() {
    let valid = chunk.valid();
    let invalid = chunk.invalid();
    // formatting for `valid` and `invalid`
}

Solution sketches

Current signature:

impl<'a> Utf8Chunks<'a> {
    fn new(bytes: &'a [u8]) -> Self;
}

Proposed signature:

impl [u8] {
    fn utf8_chunks(&self) -> Utf8Chunks<'_>;
}

Links and related work

What happens now?

This issue is part of the libs-api team API change proposal process. Once this issue is filed the libs-api team will review open proposals in its weekly meeting. You should receive feedback within a week or two.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions