pub trait RoutingTable {
type Value: 'static;
// Required methods
fn contains(&self, ip: IpAddr) -> bool;
fn insert(&mut self, prefix: IpNet, val: Self::Value) -> Option<Self::Value>;
fn remove(&mut self, prefix: IpNet) -> Option<Self::Value>;
fn clear(&mut self);
fn lookup(&self, ip: IpAddr) -> Option<&Self::Value>;
fn lookup_all(&self, ip: IpAddr) -> LookupIter<'_, Self::Value> ⓘ;
fn lookup_prefix_exact(&self, prefix: IpNet) -> Option<&Self::Value>;
fn lookup_prefix(&self, prefix: IpNet) -> Option<&Self::Value>;
fn lookup_prefix_lpm(&self, prefix: IpNet) -> Option<(IpNet, &Self::Value)>;
fn size(&self) -> usize;
}Expand description
Abstracts routing table operations.
Required Associated Types§
Required Methods§
Sourcefn contains(&self, ip: IpAddr) -> bool
fn contains(&self, ip: IpAddr) -> bool
Report whether ip is covered by a route in the table.
§Examples
let mut table = Table::default();
let pfx = "1.2.3.0/24".parse().unwrap();
table.insert(pfx, 12);
assert!(table.contains("1.2.3.4".parse().unwrap()));
assert!(!table.contains("1.2.4.4".parse().unwrap()));Sourcefn insert(&mut self, prefix: IpNet, val: Self::Value) -> Option<Self::Value>
fn insert(&mut self, prefix: IpNet, val: Self::Value) -> Option<Self::Value>
Insert a route into the table at prefix.
§Examples
let mut table = Table::default();
let pfx = "0.0.0.0/0".parse().unwrap();
assert_eq!(None, table.insert(pfx, 12));
// Table has the value now
assert_eq!(Some(&12), table.lookup_prefix_exact(pfx));
// Repeated insert returns the removed value
assert_eq!(Some(12), table.insert(pfx, 13));Sourcefn remove(&mut self, prefix: IpNet) -> Option<Self::Value>
fn remove(&mut self, prefix: IpNet) -> Option<Self::Value>
Remove the route from the table with the given prefix.
§Examples
let mut table = Table::default();
let pfx = "0.0.0.0/0".parse().unwrap();
// remove without a route returns `None`
assert_eq!(None, table.remove(pfx));
// insert then remove returns the value that was inserted
assert_eq!(None, table.insert(pfx, 12));
assert_eq!(Some(12), table.remove(pfx));Sourcefn lookup(&self, ip: IpAddr) -> Option<&Self::Value>
fn lookup(&self, ip: IpAddr) -> Option<&Self::Value>
Lookup the route that most closely covers ip.
§Examples
let mut table = Table::default();
table.insert("1.2.0.0/16".parse().unwrap(), "value");
assert_eq!(Some(&"value"), table.lookup("1.2.3.4".parse().unwrap()));Sourcefn lookup_all(&self, ip: IpAddr) -> LookupIter<'_, Self::Value> ⓘ
fn lookup_all(&self, ip: IpAddr) -> LookupIter<'_, Self::Value> ⓘ
Lookup all matches that cover ip.
The iterator yields prefixes in reverse length order (most specific to least).
Sourcefn lookup_prefix_exact(&self, prefix: IpNet) -> Option<&Self::Value>
fn lookup_prefix_exact(&self, prefix: IpNet) -> Option<&Self::Value>
Lookup a route that exactly matches prefix (supernets do not match).
§Examples
let mut table = Table::default();
let pfx = "1.2.0.0/16".parse().unwrap();
table.insert(pfx, "route");
// Exact prefix matches
assert_eq!(Some(&"route"), table.lookup_prefix_exact(pfx));
// Subnet does not
assert_eq!(
None,
table.lookup_prefix_exact("1.2.3.0/24".parse().unwrap())
);Sourcefn lookup_prefix(&self, prefix: IpNet) -> Option<&Self::Value>
fn lookup_prefix(&self, prefix: IpNet) -> Option<&Self::Value>
Lookup the route that most closely covers prefix.
This represents a slight optimization over
lookup_prefix_lpm if the matched prefix
isn’t needed.
§Examples
let mut table = Table::default();
let pfx = "1.2.0.0/16".parse().unwrap();
table.insert(pfx, 1234);
// subnet lookup gets only the value in the table
assert_eq!(
Some(&1234),
table.lookup_prefix("1.2.3.4/30".parse().unwrap())
);Sourcefn lookup_prefix_lpm(&self, prefix: IpNet) -> Option<(IpNet, &Self::Value)>
fn lookup_prefix_lpm(&self, prefix: IpNet) -> Option<(IpNet, &Self::Value)>
Lookup the route that most closely covers prefix, and return that
matching prefix.
§Examples
let mut table = Table::default();
let pfx = "1.2.0.0/16".parse().unwrap();
table.insert(pfx, true);
// subnet lookup returns the matching prefix and route value in the table
assert_eq!(
Some((pfx, &true)),
table.lookup_prefix_lpm("1.2.12.143/32".parse().unwrap())
);Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".