Reduce number of type parameters in router

Merge multiple related type parameters into trait,
allowing for easier refactoring and better maintainability.
This commit is contained in:
Mathias Hall-Andersen
2019-08-31 20:25:16 +02:00
parent c823af1a9c
commit 46d76b80c6
8 changed files with 137 additions and 72 deletions

View File

@@ -13,7 +13,44 @@ use std::net::SocketAddr;
use std::sync::Arc;
use std::time::Duration;
use types::{Bind, KeyPair};
use types::{Bind, KeyPair, Tun};
#[derive(Debug)]
enum TunError {}
impl Error for TunError {
fn description(&self) -> &str {
"Generic Tun Error"
}
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
impl fmt::Display for TunError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Not Possible")
}
}
struct TunTest {}
impl Tun for TunTest {
type Error = TunError;
fn mtu(&self) -> usize {
1500
}
fn read(&self, buf: &mut [u8], offset: usize) -> Result<usize, Self::Error> {
Ok(0)
}
fn write(&self, src: &[u8]) -> Result<(), Self::Error> {
Ok(())
}
}
struct Test {}
@@ -73,6 +110,7 @@ fn main() {
{
let router = router::Device::new(
4,
TunTest {},
|t: &PeerTimer, data: bool, sent: bool| t.a.reset(Duration::from_millis(1000)),
|t: &PeerTimer, data: bool, sent: bool| t.b.reset(Duration::from_millis(1000)),
|t: &PeerTimer| println!("new key requested"),