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

Tracking Issue for addr_parse_ascii feature #101035

Open
1 of 3 tasks
marmeladema opened this issue Aug 26, 2022 · 5 comments
Open
1 of 3 tasks

Tracking Issue for addr_parse_ascii feature #101035

marmeladema opened this issue Aug 26, 2022 · 5 comments
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@marmeladema
Copy link
Contributor

Feature gate: #![feature(addr_parse_ascii)]

This is a tracking issue for parsing network addresses (IP, Socket, etc) from a slice of bytes.

Internally the parser already works on bytes, this feature exposes publicly the ability to parse network address from a slice of bytes and therefor avoid the need to do utf8 validation in case one has only bytes.

Public API

impl IpAddr {
    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
}
impl Ipv4Addr {
    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
}
impl Ipv6Addr {
    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
}
impl SocketAddrV4 {
    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
}
impl SocketAddrV6 {
    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
}
impl SocketAddr {
    pub fn parse_ascii(b: &[u8]) -> Result<Self, AddrParseError> {
}

Steps / History

Unresolved Questions

  • None yet.

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

@marmeladema marmeladema added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Aug 26, 2022
@niklasf
Copy link
Contributor

niklasf commented Nov 27, 2022

Bikeshedding: Usually .parse() is called on the input. Perhaps something like IpAddr::from_ascii() is more consistent with FromStr::from_str() and u32::from_str_radix() etc.

@shahn
Copy link
Contributor

shahn commented Mar 9, 2023

Anything blocking an FCP for this feature? Very happy about more non-string APIs getting added

@Turbo87
Copy link
Member

Turbo87 commented Oct 25, 2023

I think I support what @niklasf has said above. Function names beginning with from_ for such cases seem to be more common in the std lib for these situations. I would however propose naming it from_bytes() since the function operates on u8 and not ASCII, which is limited to values of 0-127. This would also be consistent with the naming of https://doc.rust-lang.org/stable/std/ffi/struct.OsStr.html#method.from_bytes.

@tgross35
Copy link
Contributor

tgross35 commented Oct 26, 2023

I have also come across need for this a lot, but not just for these types - almost everything we parse in std is strictly ASCII (e.g. numbers). If this is going to be a common thing, I am thinking that maybe we should have a separate trait like FromStr rather than one off methods, which also gives an nicely usable inherent method on &[u8]

pub trait FromBytes: Sized {
    type Err;

    // Required method
    fn from_bytes(s: &str) -> Result<Self, Self::Err>;
}

impl<T> FromStr for T where T: FromBytes { /* ... */ }

impl<[u8]> {
    pub fn parse<F>(&self) -> Result<F, <F as FromStr>::Err>
        where F: FromBytes,
}

@BurntSushi I know you have had some ideas about improving ascii support in the language, any thoughts here?

Edit: opened an ACP at rust-lang/libs-team#287 to discuss this further

@jmillikin
Copy link
Contributor

I think I support what @niklasf has said above. Function names beginning with from_ for such cases seem to be more common in the std lib for these situations. I would however propose naming it from_bytes() since the function operates on u8 and not ASCII

Parsing a network address "from bytes" usually implies that the bytes are used directly as the network address, which is a fixed-size and non-failable conversion (aka impl From).

struct Ipv4Addr {
  // already implemented as: impl From<[u8; 4]> for Ipv4Addr and IpAddr
  fn from_bytes(bytes: [u8; 4]) -> Ipv4Addr;
}

struct Ipv6Addr {
  // already implemented as: impl From<[u8; 16]> for Ipv6Addr and IpAddr
  fn from_bytes(bytes: [u8; 16]) -> Ipv6Addr;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants