Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 0 additions & 78 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,7 @@
// Licensed under the MIT license <LICENSE-MIT or http://opensource.org/licenses/MIT>.
// This file may not be copied, modified, or distributed except according to those terms.

#[cfg(not(feature = "alloc"))]
use std::net::{Ipv4Addr, Ipv6Addr};
#[cfg(feature = "alloc")]
pub mod addr {
#[derive(Copy, Clone)]
pub struct Ipv4Addr([u8; 4]);
impl Ipv4Addr {
pub fn new(a1: u8, a2: u8, a3: u8, a4: u8) -> Self {
Ipv4Addr([a1, a2, a3, a4])
}

pub fn from(num: u32) -> Self {
Ipv4Addr([
(num >> 24) as u8,
(num >> 16) as u8,
(num >> 8) as u8,
num as u8,
])
}

pub fn octets(&self) -> [u8; 4] {
self.0
}
}
impl ::std::convert::From<Ipv4Addr> for u32 {
fn from(a: Ipv4Addr) -> u32 {
(a.0[0] as u32) << 24
| (a.0[1] as u32) << 16
| (a.0[2] as u32) << 8
| (a.0[3] as u32) << 0
}
}

#[derive(Copy, Clone)]
pub struct Ipv6Addr([u8; 16]);

impl Ipv6Addr {
pub fn new(a1: u16, a2: u16, a3: u16, a4: u16, a5: u16, a6: u16, a7: u16, a8: u16) -> Self {
Ipv6Addr([
(a1 >> 8) as u8,
a1 as u8,
(a2 >> 8) as u8,
a2 as u8,
(a3 >> 8) as u8,
a3 as u8,
(a4 >> 8) as u8,
a4 as u8,
(a5 >> 8) as u8,
a5 as u8,
(a6 >> 8) as u8,
a6 as u8,
(a7 >> 8) as u8,
a7 as u8,
(a8 >> 8) as u8,
a8 as u8,
])
}

pub fn octets(&self) -> [u8; 16] {
self.0
}

pub fn segments(&self) -> [u16; 8] {
[
(self.0[0] as u16) << 8 | (self.0[1] as u16),
(self.0[2] as u16) << 8 | (self.0[3] as u16),
(self.0[4] as u16) << 8 | (self.0[5] as u16),
(self.0[6] as u16) << 8 | (self.0[7] as u16),
(self.0[8] as u16) << 8 | (self.0[9] as u16),
(self.0[10] as u16) << 8 | (self.0[11] as u16),
(self.0[12] as u16) << 8 | (self.0[13] as u16),
(self.0[14] as u16) << 8 | (self.0[15] as u16),
]
}
}
}

#[cfg(feature = "alloc")]
use self::addr::*;

/// Address trait provides methods required for storing in TreeBitmap trie datastructure.
pub trait Address: Copy {
Expand Down
22 changes: 9 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
//! by W. Eatherton, Z. Dittia, G. Varghes.
//!
#![cfg_attr(feature = "alloc", no_std)]
#![cfg_attr(feature = "alloc", feature(alloc))]

#[cfg(feature = "alloc")]
extern crate alloc;
Expand All @@ -31,9 +30,6 @@ use tree_bitmap::TreeBitmap;
pub mod address;
use address::Address;

#[cfg(feature = "alloc")]
pub use address::addr::*;

/// A fast, compressed IP lookup table.
pub struct IpLookupTable<A, T> {
inner: TreeBitmap<T>,
Expand Down Expand Up @@ -85,7 +81,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -106,7 +102,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -132,7 +128,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -154,7 +150,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -180,7 +176,7 @@ where
/// # Example
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand Down Expand Up @@ -212,7 +208,7 @@ where
/// # Example
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand Down Expand Up @@ -244,7 +240,7 @@ where
/// # Example
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand Down Expand Up @@ -282,7 +278,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let mut table = IpLookupTable::new();
Expand All @@ -308,7 +304,7 @@ where
/// # Examples
///
/// ```
/// use treebitmap::IpLookupTable;
/// use ip_network_table_deps_treebitmap::IpLookupTable;
/// use std::net::Ipv6Addr;
///
/// let x: Ipv6Addr = "2001:db8:100::".parse().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions src/tree_bitmap/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ impl<T: Sized> Allocator<T> {

#[cfg(test)]
mod tests {
extern crate std;
use super::*;
use tree_bitmap::allocator::tests::std::println;

#[test]
fn bucketvec_move_to() {
Expand Down
19 changes: 11 additions & 8 deletions src/tree_bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use alloc::vec;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use std::cmp;
#[cfg(not(feature = "alloc"))]
use std::vec;

mod allocator;
mod node;
Expand Down Expand Up @@ -363,7 +365,7 @@ impl<T: Sized> TreeBitmap<T> {
Some((result_hdl, result_index))
}
_ => None,
}
};
}

match cur_node.match_external(bitmap) {
Expand All @@ -378,15 +380,13 @@ impl<T: Sized> TreeBitmap<T> {
}

pub fn exact_match(&self, nibbles: &[u8], masklen: u32) -> Option<&T> {
self.exact_match_internal(nibbles, masklen).map(move |(result_hdl, result_index)| {
self.results.get(&result_hdl, result_index)
})
self.exact_match_internal(nibbles, masklen)
.map(move |(result_hdl, result_index)| self.results.get(&result_hdl, result_index))
}

pub fn exact_match_mut(&mut self, nibbles: &[u8], masklen: u32) -> Option<&mut T> {
self.exact_match_internal(nibbles, masklen).map(move |(result_hdl, result_index)| {
self.results.get_mut(&result_hdl, result_index)
})
self.exact_match_internal(nibbles, masklen)
.map(move |(result_hdl, result_index)| self.results.get_mut(&result_hdl, result_index))
}

/// Remove prefix. Returns existing value if the prefix previously existed.
Expand Down Expand Up @@ -624,7 +624,7 @@ impl<T> Drop for IntoIter<T> {

pub struct MatchesMut<'a, T: 'a> {
inner: &'a mut TreeBitmap<T>,
path: std::vec::IntoIter<(u32, AllocatorHandle, u32)>,
path: vec::IntoIter<(u32, AllocatorHandle, u32)>,
}

impl<'a, T: 'a> Iterator for MatchesMut<'a, T> {
Expand Down Expand Up @@ -656,7 +656,10 @@ impl<T> Drop for TreeBitmap<T> {

#[cfg(test)]
mod tests {
extern crate std;

use super::*;
use tree_bitmap::tests::std::println;

#[test]
fn len() {
Expand Down
2 changes: 2 additions & 0 deletions src/tree_bitmap/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,9 @@ pub enum MatchResult {

#[cfg(test)]
mod tests {
extern crate std;
use super::*;
use tree_bitmap::node::tests::std::println;

#[test]
fn test_trienode_new() {
Expand Down
8 changes: 4 additions & 4 deletions tests/rand_test.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
extern crate ip_network_table_deps_treebitmap;
extern crate rand;
extern crate treebitmap;

use std::mem;
use std::net::{Ipv4Addr, Ipv6Addr};

use self::rand::{thread_rng, Rng};

use treebitmap::address::Address;
use treebitmap::*;
use ip_network_table_deps_treebitmap::address::Address;
use ip_network_table_deps_treebitmap::*;

const NUMBER_OF_ITERS: usize = 10; // number of times to run each test
const NUMBER_OF_PEERS: usize = 64; // number of distinct values
Expand Down Expand Up @@ -128,7 +128,7 @@ impl<T, A: Address> SlowRouter<T, A> {
}

#[test]
#[cfg(not(miri))] // miri is too slow
#[cfg(not(miri))] // miri is too slow
fn ipv6_random_test() {
for _ in 0..NUMBER_OF_ITERS {
let mut tbl = IpLookupTable::new();
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
// This file may not be copied, modified, or distributed except according to those terms.
//

extern crate treebitmap;
extern crate ip_network_table_deps_treebitmap;

mod rand_test;

use ip_network_table_deps_treebitmap::*;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::str::FromStr;
use treebitmap::*;

#[test]
#[should_panic]
Expand Down