prefixset 0.1.0-rc.2

An efficient set container for IP address prefixes
Documentation
use std::ops::{BitAnd, BitOr, Sub};

use crate::prefix::IpPrefix;

use super::Node;

impl<P: IpPrefix> PartialEq for Node<P> {
    fn eq(&self, other: &Self) -> bool {
        self.prefix == other.prefix && self.gluemap == other.gluemap
    }
}

impl<P: IpPrefix> BitAnd for Box<Node<P>> {
    type Output = Option<Self>;

    fn bitand(self, rhs: Self) -> Self::Output {
        self.children()
            .fold(None, |root, node| match rhs.intersect_nodes(node) {
                Some(new) => {
                    if let Some(root) = root {
                        Some(root.add(new))
                    } else {
                        Some(new)
                    }
                }
                None => root,
            })
    }
}

impl<P: IpPrefix> BitOr for Box<Node<P>> {
    type Output = Option<Self>;

    fn bitor(self, rhs: Self) -> Self::Output {
        Some(self.add(rhs))
    }
}

impl<P: IpPrefix> Sub for Box<Node<P>> {
    type Output = Option<Self>;

    fn sub(self, mut rhs: Self) -> Self::Output {
        Some(self.remove(&mut rhs))
    }
}