Browse Source

Fix clippy warnings and no_std build

interface-2
Lev 3 years ago
parent
commit
3b17d437d3
  1. 4
      src/interface.rs
  2. 14
      src/interfaces/ip.rs

4
src/interface.rs

@ -14,7 +14,7 @@ pub(crate) type TargetingData = String;
pub trait InterfaceRequirements {} pub trait InterfaceRequirements {}
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub trait InterfaceRequirements = Send + Sync; pub trait InterfaceRequirements: Send + Sync {}
/// An interface that can be used to /// An interface that can be used to
@ -53,6 +53,8 @@ pub mod test_interface {
messages: Vec<(Vec<u8>, TargetingData)>, messages: Vec<(Vec<u8>, TargetingData)>,
} }
impl InterfaceRequirements for TestInterface {}
impl Interface for TestInterface { impl Interface for TestInterface {
fn main_loop_iteration(&mut self) -> IFResult<()> { fn main_loop_iteration(&mut self) -> IFResult<()> {
Ok(()) Ok(())

14
src/interfaces/ip.rs

@ -1,9 +1,7 @@
use std::net; use std::net;
use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use alloc::string::String; use alloc::string::String;
use crate::interface;
use crate::interface::{Interface, InterfaceRequirements, TargetingData}; use crate::interface::{Interface, InterfaceRequirements, TargetingData};
use crate::message::MessageBytes; use crate::message::MessageBytes;
use crate::res::{IFError, IFResult}; use crate::res::{IFError, IFResult};
@ -18,6 +16,8 @@ pub struct IPInterface {
pub listener: net::TcpListener, pub listener: net::TcpListener,
} }
impl InterfaceRequirements for IPInterface {}
impl Interface for IPInterface { impl Interface for IPInterface {
fn main_loop_iteration(&mut self) -> IFResult<()> { fn main_loop_iteration(&mut self) -> IFResult<()> {
println!("Mainloop {:?}", self.listener.local_addr()); println!("Mainloop {:?}", self.listener.local_addr());
@ -43,10 +43,10 @@ impl Interface for IPInterface {
for mut connection in &self.connections { for mut connection in &self.connections {
let mut size_arr: [u8; 4] = [0, 0, 0, 0]; let mut size_arr: [u8; 4] = [0, 0, 0, 0];
connection.read(&mut size_arr); connection.read_exact(&mut size_arr)?;
let mut size: u32 = 0; let mut size: u32 = 0;
for i in 0..4 { for size_byte in &size_arr {
size = size * 256 + size_arr[i] as u32; size = size * 256 + *size_byte as u32;
} }
println!("Size: {:?}", size); println!("Size: {:?}", size);
@ -63,7 +63,7 @@ impl Interface for IPInterface {
&*self.id &*self.id
} }
fn send(&mut self, message: &[u8], interface_data: Option<TargetingData>) -> IFResult<()> { fn send(&mut self, message: &[u8], interface_data: Option<TargetingData>) -> IFResult<()> {
let mut addr: net::SocketAddr = match interface_data { let addr: net::SocketAddr = match interface_data {
Some(ip_string) => ip_string.parse().expect("Unable to parse address"), Some(ip_string) => ip_string.parse().expect("Unable to parse address"),
None => return Err(IFError::General(String::from("Not enough info to create connection"))) None => return Err(IFError::General(String::from("Not enough info to create connection")))
}; };
@ -79,7 +79,7 @@ impl Interface for IPInterface {
println!("Sending message to {:?}", self.connections[index].peer_addr().unwrap()); println!("Sending message to {:?}", self.connections[index].peer_addr().unwrap());
self.connections[index].write(message); self.connections[index].write_all(message)?;
println!("Sent message"); println!("Sent message");

Loading…
Cancel
Save