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

Add support for std::net::{Ipv4Addr, Ipv6Addr, IpAddr} <-> INET #161

Closed
ivan opened this issue Mar 4, 2016 · 8 comments
Closed

Add support for std::net::{Ipv4Addr, Ipv6Addr, IpAddr} <-> INET #161

ivan opened this issue Mar 4, 2016 · 8 comments

Comments

@ivan
Copy link
Contributor

ivan commented Mar 4, 2016

Would you take a PR that does this? (I am working on one.)

@sfackler
Copy link
Owner

sfackler commented Mar 4, 2016

Postgres stores a netmask in addition to the IP address, which is why these implementations don't exist already.

@ivan
Copy link
Contributor Author

ivan commented Mar 4, 2016

Indeed, that is a bit of a troublesome mismatch. What I'm doing is: always writing 32 bits / 128 bits in ToSql, and doing something like

        if bits != 32 {
            let err: Box<error::Error + Sync + Send> = "INET must have all 32 bits for Ipv4Addr".into();
            return Err(Error::Conversion(err));
        }

in FromSql.

Do you think that is workable? This should at least support the use case where one does not store any netmasks other than the default /32 or /128.

@sfackler
Copy link
Owner

sfackler commented Mar 4, 2016

I would prefer that the implementations in this library be "lossless" in general.

@ivan
Copy link
Contributor Author

ivan commented Mar 4, 2016

I take your point. I guess I might want to make a new crate with types that wrap Ipv4Addr and Ipv6Addr to add a netmask.

@ivan ivan closed this as completed Mar 4, 2016
@ivan
Copy link
Contributor Author

ivan commented Mar 4, 2016

I just figured out that there is a way to insert Ipv4Addr/Ipv6Addr using strings:

let ipv4_s = format!("{}", Ipv4Addr::new(127, 0, 0, 1));
let ipv6_s = format!("{}", Ipv6Addr::new(1, 2, 3, 4, 5, 6, 7, 8));
conn.execute("INSERT INTO sessions (last_seen_ipv4, last_seen_ipv6)
              VALUES (cast($1::text as inet), cast($2::text as inet))", &[&ipv4_s, &ipv6_s]).unwrap();

@zr40
Copy link
Contributor

zr40 commented Mar 4, 2016

@ivan note that you can use $1::text::inet. Both cast syntaxes have the same meaning, you don't need to use both together.

@ivan
Copy link
Contributor Author

ivan commented Mar 4, 2016

Thanks! That does look a lot better.

@newpavlov
Copy link

@sfackler
How about just using a simple aliased tuple type MaskIpAddr = (IpAddr, u8); for lossless operations with inet and cidr?

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

No branches or pull requests

4 participants