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

Implement bitwise operations for Ipv4Addr #2398

Closed
canndrew opened this issue Apr 11, 2018 · 8 comments
Closed

Implement bitwise operations for Ipv4Addr #2398

canndrew opened this issue Apr 11, 2018 · 8 comments
Labels
T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@canndrew
Copy link
Contributor

It's natural and common to apply bitwise operations to IPv4 addresses. For instance if I have an IP address 192.168.1.23 and a netmask 255.255.255.0 I might want to calculate:

192.168.1.23 & 255.255.255.0 => 192.168.1.0

It's possible to do this by converting to u32 and back, but I shouldn't have to.

@SimonSapin
Copy link
Contributor

I don’t think arbitrary bitwise operations make sense. (What does it mean to XOR two addresses?) Is there anything other than masking? Would a dedicated addr.mask(24) method be enough?

@ExpHP
Copy link

ExpHP commented Apr 11, 2018

Would a dedicated addr.mask(24) method be enough?

Probably not unless there's an easy way to get that 24...

@SimonSapin
Copy link
Contributor

Well, how would you get that 255.255.255.0?

@dtolnay dtolnay added the T-libs-api Relevant to the library API team, which will review and decide on the RFC. label Apr 15, 2018
@fstirlitz
Copy link

Configuration file whose format you don't control?

@WiSaGaN
Copy link
Contributor

WiSaGaN commented May 31, 2018

What about creating a concrete type for "255.255.255.0" like "SubnetMask", and define proper constructors to outlaw invalid ones.

In this way we can be sure that only meaningful mask operations are done on IP addresses, in which bitwise operations are more of an implementation detail?

@carneeki
Copy link

carneeki commented Jul 2, 2020

What about creating a concrete type for "255.255.255.0" like "SubnetMask", and define proper constructors to outlaw invalid ones.

In this way we can be sure that only meaningful mask operations are done on IP addresses, in which bitwise operations are more of an implementation detail?

For anyone still looking at this, the ipnet crate builds on the std::net::{IpAddr, Ipv4Addr, Ipv6Addr} types to create subnets. Implementing std::ops::{BitAnd, BitOr} for the Ipv4Addr and Ipv6Addr types looks to be on the todo list, but there are examples for iteration, dividing subnets and aggregating adjacent prefixes.

@WiSaGaN
Copy link
Contributor

WiSaGaN commented Jul 30, 2023

There is currently active pull request related: rust-lang/rust#113747, with related rust-lang/libs-team#235

@clarfonthey
Copy link
Contributor

This should be probably closed, then, in favour of those.

@dtolnay dtolnay closed this as completed Sep 27, 2023
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

8 participants