Skip to content

Allow using match with Scheme #789

Description

@anuraaga

While working with the crate, I noticed that many types like Method, HeaderName and others have from_bytes, but Scheme only has from_str. Would it make sense to add from_bytes for consistency with the others? Personally my use case is parsing a scheme from [u8]. Luckily there are only two schemes so I can match myself and this isn't really tedious but wondering if it could make sense from a consistency perspective.

Activity

  1. anuraaga commented on Oct 29, 2025

    @anuraaga
    Author

    Somewhat related but I also noticed that since the struct doesn't have PartialEq or similar, scheme can't be used in match. I guess this type may be a bit old compared to other ones in the crate.

    Without using match, it seems we have to use if/else with dereferencing like this.

            if *scheme == uri::Scheme::HTTP {
                self.set_item(key, intern!(py, "http"))?;
            } else if *scheme == uri::Scheme::HTTPS {
                self.set_item(key, intern!(py, "https"))?;
            } else {
                self.set_item(key, scheme.as_str())?;
            }
  2. seanmonstar commented on Oct 29, 2025

    @seanmonstar
    Member

    There's TryFrom<&[u8]> for Scheme which would do that you want.

    As for the match part, the hard thing is that schemes are consider case-insensitive, but Scheme doesn't force the bytes to lower case, so PartialEq is not able to derive a byte-equivalency.

  3. changed the title [-]Add Scheme::from_bytes (and any others as needed)[/-] [+]Allow using match with Scheme[/+] on Oct 30, 2025
  4. anuraaga commented on Oct 30, 2025

    @anuraaga
    Author

    Thanks @seanmonstar - I had missed TryFrom that works well.

    I renamed the issue to focus on match - do you think it would make sense to have Scheme force the bytes to lower case to support match? In practice, I would be surprised if it causes allocations in any real code, i.e. there are actually non-lowercase schemes being parsed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions