-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Comments
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. |
This seems like a cool thing to create as a third party library. |
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. |
This would be extremely easy to do as a macro on stable when functional proc macros land. |
@lfairy here is the same thing implemented using 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.rsextern 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"));
} |
@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. |
Triage ping @clarcharr - do you want to write up an RFC for this? (also consider const fn...) |
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 |
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. |
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()
thanIpv6Addr::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?The text was updated successfully, but these errors were encountered: