Skip to content

Token::as_index OOB behavior #35

Closed
@asmello

Description

@asmello

More of a discussion than a issue, perhaps, but I find it unexpected that this is true:

assert_eq!(Token::new("-").as_index(1), Ok(1));
assert_eq!(Token::new("1").as_index(1), Ok(1));
assert_eq!(Token::new("2").as_index(1), Err(IndexError::OutOfBounds));

I can see the utility of the first case, but I'd expect that out-of-bounds numerical indices would always produce an error. It's quite error prone that they don't (it even contradicts the docs, as they're currently written).

If we go by the RFC literally, it defines - as an error condition in section 7, but doesn't define how to handle the error. In practice we know from sibling standards like RFC 6902 that using - is only an actual error condition in some contexts, so that puts it into a bit of a grey area.

If we were to prioritize semantic correctness, we should probably produce an error for - (but a separate error from what would result from a numerical out-of-bounds index). Another option would be to have the Ok variant carry an enum value indicating whether the index is internal or refers to the next element:

enum Index {
    Internal(usize),
    Next(usize)
}
fn as_index(...) -> Result<Index, IndexError>

Regardless, I think numerical indices that exceed the given array length should always be considered an error, or never.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions