Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge is_valid and validate #50

Merged
merged 1 commit into from
Jun 30, 2024
Merged

merge is_valid and validate #50

merged 1 commit into from
Jun 30, 2024

Conversation

chanced
Copy link
Owner

@chanced chanced commented Jun 30, 2024

This merges is_valid and validate so that the logic is not replicated.

@asmello we could make Pointer::parse into a const fn but it would mean accepting &str:

impl Pointer {
    const fn new(s: &str) -> &Self {
        unsafe { &*(core::ptr::from_ref::<str>(s) as *const Self) }
    }

    pub const fn parse(s: &str) -> Result<&Self, ParseError> {
        match validate(s) {
            Ok(_) => Ok(Self::new(s)),
            Err(err) => Err(err),
        }
    }
}

@asmello
Copy link
Collaborator

asmello commented Jun 30, 2024

Ah nice, I avoided this because I wanted to minimise the risky algorithm changes, but it makes sense to do.

we could make Pointer::parse into a const fn but it would mean accepting &str

Yeah I considered that, but figured from_static would likely be more useful in const contexts (plus validate wasn't const). I'm not sure about the trade-off, I think AsRef<str> is likely more useful than having parse be const. We could add a separate parse_const instead, I'd be totally fine with that.

Copy link
Collaborator

@asmello asmello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@chanced
Copy link
Owner Author

chanced commented Jun 30, 2024

Ah nice, I avoided this because I wanted to minimise the risky algorithm changes, but it makes sense to do.

Yea, same, but our test coverage is solid enough now that I think this is safe enough. Plus its possible now that errors don't allocate.

We could add a separate parse_const instead, I'd be totally fine with that.

Nah, I like from_static more.

@chanced chanced merged commit d8b0f23 into main Jun 30, 2024
1 check passed
@chanced chanced deleted the merge-validation branch June 30, 2024 21:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants