use crate::{frame::AsPtr, CanAnyFrame, CanFrame, Socket, SocketOptions};
use std::{
io,
os::unix::io::{AsRawFd, RawFd},
};
#[cfg(any(feature = "async-io", feature = "async-std"))]
use async_io::Async;
#[cfg(all(
feature = "smol",
not(any(feature = "async-io", feature = "async-std"))
))]
use smol::Async;
#[derive(Debug)]
pub struct CanSocket(Async<crate::CanSocket>);
impl CanSocket {
pub fn open(ifname: &str) -> io::Result<Self> {
crate::CanSocket::open(ifname)?.try_into()
}
pub async fn write_frame<F>(&self, frame: &F) -> io::Result<()>
where
F: Into<CanFrame> + AsPtr,
{
self.0.write_with(|fd| fd.write_frame(frame)).await
}
pub async fn read_frame(&self) -> io::Result<CanFrame> {
self.0.read_with(|fd| fd.read_frame()).await
}
}
impl SocketOptions for CanSocket {}
impl TryFrom<crate::CanSocket> for CanSocket {
type Error = io::Error;
fn try_from(sock: crate::CanSocket) -> Result<Self, Self::Error> {
Ok(Self(Async::new(sock)?))
}
}
impl AsRawFd for CanSocket {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}
#[derive(Debug)]
pub struct CanFdSocket(Async<crate::CanFdSocket>);
impl CanFdSocket {
pub fn open(ifname: &str) -> io::Result<Self> {
crate::CanFdSocket::open(ifname)?.try_into()
}
pub async fn write_frame<F>(&self, frame: &F) -> io::Result<()>
where
F: Into<CanAnyFrame> + AsPtr,
{
self.0.write_with(|fd| fd.write_frame(frame)).await
}
pub async fn read_frame(&self) -> io::Result<CanAnyFrame> {
self.0.read_with(|fd| fd.read_frame()).await
}
}
impl SocketOptions for CanFdSocket {}
impl TryFrom<crate::CanFdSocket> for CanFdSocket {
type Error = io::Error;
fn try_from(sock: crate::CanFdSocket) -> Result<Self, Self::Error> {
Ok(Self(Async::new(sock)?))
}
}
impl AsRawFd for CanFdSocket {
fn as_raw_fd(&self) -> RawFd {
self.0.as_raw_fd()
}
}