cidranger
cidranger copied to clipboard
Move from net to netip
This PR moves cidranger from using the IP address/prefix values from the "net" module in the Go standard libary to using the equivalents from the newer "net/netip" in the standard library. This allows for some code simplification:
- no need to check for invalid underlying
[]bytelengths or nil pointers netip.Prefixcan be used directly as a map key in bruteranger as opposed to anet.IPNet's.String()value- address comparisons can be done directly with
==/!=as opposed to viabytes.Equal()
The tests were updated to use net/netip, and run successfully. TestInsertError and TestRemoveError were removed, as the error they are testing for is impossible to induce with net/netip
This is a fairly big update, and since it changes a good chunk of the API, likely warrants a version bump, though I am opening this PR to at least see if this is a change that you are interested in and if you have any changes in mind.
net/netip was introduced in 1.18, and, as 1.19 is out now, all officially supported versions of Go (the last two releases) support this, though applications and libraries making use of cidranger would need to be rewritten or at least add wrapper code to handle this, if this version is used.
It would also implicitly fix the issue outlined in PR https://github.com/yl2chen/cidranger/pull/30 and issue https://github.com/yl2chen/cidranger/issues/42, as net/netip handles IPv4-mapped IPv6 addresses far better than net.
This would be a breaking change, and thus would require a 2.x release.
If we're going to break compatibility, we might as well include other valuable changes, such as a switch from use of exposed interfaces (Ranger, RangerEntry) to using concrete types with generics. I believe the Ranger interface itself is just an anti-pattern, while RangerEntry is perhaps intended to allow associating data with a key, which generics would do in a cleaner and cheaper way since Go 1.18.
For example, this PR is an incomplete/WIP adjustment of the API for generics building upon this PR.
FYI to those looking for this, I’m going to try out https://github.com/gaissmai/bart as a replacement, might be useful to others