achanda / ipnetwork

A library to work with CIDRs in rust
Apache License 2.0
121 stars 38 forks source link

Implement subtraction between `IpNetwork`s #126

Open jvff opened 4 years ago

jvff commented 4 years ago

This PR implements the Sub trait for IpNetwork, Ipv4Network and Ipv6Network. There are two possibilities: either a single IP network item is subtracted from the original IP network, or multiple IP network items are subtracted from the original IP network.

The result of the subtraction operation is always an iterator, which can contain zero or more IP network items. For subtracting a single item, the iterator has a rather simple representation in memory and does implement Copy. When subtracting multiple items, a linked list of Box<dyn Iterator> is returned, so the resulting type has a more complex representation.

With this PR it is still not currently possible to subtract between IP networks with different protocols (for example, subtract an IPv4 network from an IPv6 network or vice-versa). Attempting to do so will lead the code to panic.

ctrlcctrlv commented 1 year ago

this pr is cool