Skip to content

type that converts to u64 or usize without error #3593

@larseggert

Description

@larseggert

Here's a thought, somewhat unrelated to this change: could we wrap this expect in a function:

fn u64_as_len(x: u64) -> usize {
    usize::try_from(x).expect("u64 used as length fits usize")
}

I hate seeing this whole construct everywhere, because it's line noise.

If you wanted to go further, it might even be possible to build this into a type that converts to u64 or usize without error. That way, we could have functions to parse out that type (providing the necessary checks) and all the nasty panic-inducing actions are sealed away.

(As always, feel free to open an issue for this.)

Originally posted by @martinthomson in #3591 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions