use drived Ord instead of custom ordinal trait

This commit is contained in:
Daniella / Tove 2022-08-15 14:04:22 +02:00
parent 5c14ed53ac
commit b6b782bd1d
2 changed files with 10 additions and 23 deletions

BIN
logo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 162 KiB

After

Width:  |  Height:  |  Size: 172 KiB

View file

@ -8,24 +8,11 @@ use std::{
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
trait Ordinal { #[derive(PartialOrd, PartialEq)]
fn ordinal(&self) -> isize;
}
enum SafeReadWritePacket { enum SafeReadWritePacket {
WRITE, Write,
ACK, Ack,
END, End,
}
impl Ordinal for SafeReadWritePacket {
fn ordinal(&self) -> isize {
match self {
SafeReadWritePacket::WRITE => 0,
SafeReadWritePacket::ACK => 1,
SafeReadWritePacket::END => 2,
}
}
} }
struct SafeReadWrite { struct SafeReadWrite {
@ -55,7 +42,7 @@ impl SafeReadWrite {
self.packet_count_out += 1; self.packet_count_out += 1;
let mut buf = Vec::from(buf); let mut buf = Vec::from(buf);
buf.insert(0, SafeReadWritePacket::WRITE.ordinal() as u8); buf.insert(0, SafeReadWritePacket::Write as u8);
buf.insert(0, id); buf.insert(0, id);
let buf = buf.as_slice(); let buf = buf.as_slice();
@ -75,7 +62,7 @@ impl SafeReadWrite {
if x == 0 { if x == 0 {
continue; continue;
} }
if buf[1] == SafeReadWritePacket::ACK.ordinal() as u8 && buf[0] == id { if buf[1] == SafeReadWritePacket::Ack as u8 && buf[0] == id {
resend = false; resend = false;
} }
} }
@ -109,7 +96,7 @@ impl SafeReadWrite {
} }
if buf[0] <= self.packet_count_in as u8 { if buf[0] <= self.packet_count_in as u8 {
self.socket self.socket
.send(&[buf[0], SafeReadWritePacket::ACK.ordinal() as u8]) .send(&[buf[0], SafeReadWritePacket::Ack as u8])
.expect("send error"); .expect("send error");
} }
if buf[0] == self.packet_count_in as u8 { if buf[0] == self.packet_count_in as u8 {
@ -117,7 +104,7 @@ impl SafeReadWrite {
self.packet_count_in += 1; self.packet_count_in += 1;
r.1 = x - 2; r.1 = x - 2;
} }
if buf[1] == SafeReadWritePacket::END.ordinal() as u8 { if buf[1] == SafeReadWritePacket::End as u8 {
return Ok((vec![], 0)); return Ok((vec![], 0));
} }
} }
@ -135,7 +122,7 @@ impl SafeReadWrite {
self.packet_count_out += 1; self.packet_count_out += 1;
let mut buf = vec![]; let mut buf = vec![];
buf.insert(0, SafeReadWritePacket::END.ordinal() as u8); buf.insert(0, SafeReadWritePacket::End as u8);
buf.insert(0, id); buf.insert(0, id);
let buf = buf.as_slice(); let buf = buf.as_slice();
@ -155,7 +142,7 @@ impl SafeReadWrite {
if x == 0 { if x == 0 {
continue; continue;
} }
if buf[1] == SafeReadWritePacket::ACK.ordinal() as u8 && buf[0] == id { if buf[1] == SafeReadWritePacket::Ack as u8 && buf[0] == id {
resend = false; resend = false;
} }
} }