Removed refcell container from Interface

This commit is contained in:
Jonah Dahlquist 2021-01-19 21:47:08 -08:00
commit d6574b8c23
2 changed files with 10 additions and 12 deletions

View file

@ -9,18 +9,16 @@ use crate::uninitialized_device::{InitializeError, UninitializedDevice};
use crate::{MacAddress, Mode}; use crate::{MacAddress, Mode};
pub struct Interface<SpiBus: ActiveBus, HostImpl: Host> { pub struct Interface<SpiBus: ActiveBus, HostImpl: Host> {
pub device: RefCell<Device<SpiBus, HostImpl>>, pub device: Device<SpiBus, HostImpl>,
} }
impl<SpiBus: ActiveBus, HostImpl: Host> Interface<SpiBus, HostImpl> { impl<SpiBus: ActiveBus, HostImpl: Host> Interface<SpiBus, HostImpl> {
fn new(device: Device<SpiBus, HostImpl>) -> Self { fn new(device: Device<SpiBus, HostImpl>) -> Self {
Self { Self { device }
device: RefCell::new(device),
}
} }
pub fn release(self) -> Device<SpiBus, HostImpl> { pub fn release(self) -> Device<SpiBus, HostImpl> {
self.device.into_inner() self.device
} }
} }

View file

@ -187,7 +187,7 @@ where
type Error = UdpSocketError<SpiBus::Error>; type Error = UdpSocketError<SpiBus::Error>;
fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error> { fn socket(&mut self) -> Result<Self::UdpSocket, Self::Error> {
let mut device = self.device.borrow_mut(); let mut device = &mut self.device;
if let Some(socket) = device.take_socket() { if let Some(socket) = device.take_socket() {
Ok(UdpSocket::new(socket)) Ok(UdpSocket::new(socket))
} else { } else {
@ -200,7 +200,7 @@ where
socket: &mut Self::UdpSocket, socket: &mut Self::UdpSocket,
remote: SocketAddr, remote: SocketAddr,
) -> Result<(), Self::Error> { ) -> Result<(), Self::Error> {
let mut device = self.device.borrow_mut(); let mut device = &mut self.device;
if let SocketAddr::V4(remote) = remote { if let SocketAddr::V4(remote) = remote {
// TODO find a random port // TODO find a random port
socket.open(&mut device.bus, 4000)?; socket.open(&mut device.bus, 4000)?;
@ -211,7 +211,7 @@ where
} }
} }
fn send(&mut self, socket: &mut Self::UdpSocket, buffer: &[u8]) -> nb::Result<(), Self::Error> { fn send(&mut self, socket: &mut Self::UdpSocket, buffer: &[u8]) -> nb::Result<(), Self::Error> {
socket.send(&mut self.device.borrow_mut().bus, buffer)?; socket.send(&mut self.device.bus, buffer)?;
Ok(()) Ok(())
} }
fn receive( fn receive(
@ -219,10 +219,10 @@ where
socket: &mut Self::UdpSocket, socket: &mut Self::UdpSocket,
buffer: &mut [u8], buffer: &mut [u8],
) -> nb::Result<(usize, SocketAddr), Self::Error> { ) -> nb::Result<(usize, SocketAddr), Self::Error> {
Ok(socket.receive(&mut self.device.borrow_mut().bus, buffer)?) Ok(socket.receive(&mut self.device.bus, buffer)?)
} }
fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error> { fn close(&mut self, socket: Self::UdpSocket) -> Result<(), Self::Error> {
let mut device = self.device.borrow_mut(); let mut device = &mut self.device;
socket.close(&mut device.bus)?; socket.close(&mut device.bus)?;
device.release_socket(socket.socket); device.release_socket(socket.socket);
Ok(()) Ok(())
@ -235,7 +235,7 @@ where
HostImpl: Host, HostImpl: Host,
{ {
fn bind(&mut self, socket: &mut Self::UdpSocket, local_port: u16) -> Result<(), Self::Error> { fn bind(&mut self, socket: &mut Self::UdpSocket, local_port: u16) -> Result<(), Self::Error> {
let mut device = self.device.borrow_mut(); let mut device = &mut self.device;
socket.open(&mut device.bus, local_port)?; socket.open(&mut device.bus, local_port)?;
Ok(()) Ok(())
} }
@ -246,7 +246,7 @@ where
buffer: &[u8], buffer: &[u8],
) -> nb::Result<(), Self::Error> { ) -> nb::Result<(), Self::Error> {
if let SocketAddr::V4(remote) = remote { if let SocketAddr::V4(remote) = remote {
socket.send_to(&mut self.device.borrow_mut().bus, remote, buffer)?; socket.send_to(&mut self.device.bus, remote, buffer)?;
Ok(()) Ok(())
} else { } else {
Err(nb::Error::Other(Self::Error::UnsupportedAddress)) Err(nb::Error::Other(Self::Error::UnsupportedAddress))