Skip to content

proposal for str::from_bytes_with_error_cb #1675

Closed
@killerswan

Description

@killerswan

On the pull request for str::from_bytes (#1670), @kevina had a good suggestion for what I think would make a good additional function: "a version of from_bytes which doesn't fail on invalid input but rather offers some sort of error recovery options. Like maybe it can return the partly converted string with an index to the invalid position. Or perhaps, a callback function to handle the error, etc."

At a glance the easy way to do this would be with a callback to, e.g., drop, replace, or coerce offending sequences into UTF-8 or else return a result::err...

fn from_bytes_with_error_cb <TT> (
    [u8],                                 // input bytes
    fn ([u8]) -> result::t<str, TT>       // callback 
) -> result::t<str, TT> {
...
}

But is this a recipe for complex callbacks? Perhaps providing sensible callback functions like str::encode::drop_invalid_bytes, str::encode::replace_invalid_bytes_with_..., str::encode::error_with_detail, and so on would make this simple enough to use.

Maybe those should just be simple functions for whole byte vector conversion, anyways.

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions