Skip to content

Instantly share code, notes, and snippets.

@gardnervickers
Created April 15, 2020 04:51
Show Gist options
  • Save gardnervickers/f01a743abe2575f7c25a968c2c237b86 to your computer and use it in GitHub Desktop.
Save gardnervickers/f01a743abe2575f7c25a968c2c237b86 to your computer and use it in GitHub Desktop.
use std::{io, net};
trait RawSysCallsTraitOrSomething {
fn bind_udp(&self, addr: net::SocketAddr) -> io::Result<UdpSocket> {}
fn set_ttl_udp(&self, socket: usize, ttl: u32) -> io::Result<()> {}
fn read(&self, socket: usize, buf: &mut [u8]) -> io::Result<usize> {}
}
cfg_syscall! {
type UdpSocket = udp::UdpSocket;
struct SysCalls {
obj: Arc<dyn RawSysCallsTraitOrSomething>
}
impl SysCalls {
fn bind_udp(&self, addr: net::SocketAddr) -> io::Result<UdpSocket> {
self.obj.bind_udp()
}
}
mod udp {
struct UpdSocket {
id: usize,
obj: Arc<dyn RawSysCallsTraitOrSomething>,
}
impl UdpSocket {
/// These methods on UdpSocket must always match the methods on `mio::net::UdpSocket`.
fn set_ttl(&self, ttl: u32) -> io::Result<()> {
self.obj.set_ttl(self.id, ttl)
}
}
impl Read for UdpSocket {...}
impl Write for UdpSocket {...}
impl Evented for UdpSocket {...}
}
}
cfg_not_syscall! {
type UdpSocket = mio::net::UdpSocket;
struct SysCalls;
impl Syscalls {
fn bind_udp(&self, addr: net::SocketAddr) -> io::Result<UdpSocket> {
mio::net::UdpSocket::bind(addr)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment