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

Macros for creating IPs #1926

Closed
clarfonthey opened this issue Feb 24, 2017 · 9 comments
Closed

Macros for creating IPs #1926

clarfonthey opened this issue Feb 24, 2017 · 9 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@clarfonthey
Copy link
Contributor

A lot of code uses FromStr to parse IPs in code just because it's more readable to see "2607:f8b0:4009:80b::200e".parse().unwrap() than Ipv6Addr::new(0x2607, 0xf8b0, 0x4009, 0x80b, 0, 0, 0, 0x200e).

Perhaps there could be an ip! macro that would take in a string and parse it into an IP at compile time?

@golddranks
Copy link

golddranks commented Feb 24, 2017

The usage of such a macro would be quite limited – most use cases need to have configurable IPs so compile-time conversion wouldn't do.

@sfackler
Copy link
Member

This seems like a cool thing to create as a third party library.

@lambda-fairy
Copy link
Contributor

lambda-fairy commented Feb 26, 2017

This seems like a cool thing to create as a third party library.

Challenge accepted!

It only supports IPv6 addresses at the moment, but it should be clear how to extend this to Ipv4Addr and IpAddr as well. And when procedural macros are stable, we won't need nightly to use it either.

EDIT: It supports IPv4 addresses now! Still requires nightly though.

EDIT DEUX: Andrew Cann has implemented a better version: net-literals. Please use that instead.

@aldanor
Copy link

aldanor commented Mar 9, 2017

This would be extremely easy to do as a macro on stable when functional proc macros land.

@dtolnay
Copy link
Member

dtolnay commented Mar 23, 2017

@lfairy here is the same thing implemented using syn and quote and proc-macro-hack so it works on stable Rust 1.15:

ip-macro/src/lib.rs

#[macro_use]
extern crate proc_macro_hack;

#[allow(unused_imports)]
#[macro_use]
extern crate ip_macro_impl;
pub use ip_macro_impl::*;

proc_macro_expr_decl!(ipv6! => ipv6_impl);

ip-macro-impl/src/lib.rs

extern crate syn;

#[macro_use]
extern crate quote;

#[macro_use]
extern crate proc_macro_hack;

use std::net::Ipv6Addr;

proc_macro_expr_impl! {
    pub fn ipv6_impl(input: &str) -> String {
        let strlit = syn::parse::string(input).expect("IP string");
        let segments = strlit.value.parse::<Ipv6Addr>().unwrap().segments();
        quote!(::std::net::Ipv6Addr::new(#(#segments),*)).to_string()
    }
}

the user's crate

#[macro_use]
extern crate ip_macro;

fn main() {
    println!("{:?}", ipv6!("2607:f8b0:4009:80b::200e"));
}

@sanmai-NL
Copy link

@clarcharr: I disagree with you on the readability argument in your opening post. I think it's the other way around. Also, constructing a value statically should be preferred over runtime parsing to produce the value, the former is more fail-fast.

@Centril Centril added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Dec 6, 2017
@Centril
Copy link
Contributor

Centril commented Apr 26, 2018

Triage ping @clarcharr - do you want to write up an RFC for this? (also consider const fn...)

@nielsle
Copy link

nielsle commented Apr 26, 2018

I don't know if it is worth changing, but it could be nice to have a trait for objects that can be parsed into an IPadress. This would allow library functions like the following

pub fn foo<I: Parsed<output=IpAdress>>(input: I) -> Result<(), ...>  {
    let ip_addr: IpAddress = input.parsed()?;
    //..
 }

AFAICS Present rust requires you to write the following.

pub fn foo<I: Into<String>>(input: I) -> Result<(), String>  {
    let ip_addr = IpAddress::parse(input)?;
    //..
}

The present version is little less general because you cannot parse an IpAddress as an IpAddress

@Centril
Copy link
Contributor

Centril commented Oct 8, 2018

This now exists as a crate and the libs team doesn't seem inclined to move this into the standard library so I'll close this issue.

@Centril Centril closed this as completed Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

9 participants