Into, TryFrom

This commit is contained in:
Vinzenz Schroeter 2024-05-10 19:55:18 +02:00
parent 30c26db9f7
commit 9eaa7462bc
6 changed files with 128 additions and 38 deletions

View file

@ -1,14 +1,10 @@
use std::net::{ToSocketAddrs, UdpSocket};
use crate::{Packet};
use crate::Packet;
pub struct Connection {
socket: UdpSocket,
}
pub trait ToPacket {
fn to_packet(self) -> Packet;
}
impl Connection {
/// Open a new UDP socket and create a display instance
pub fn open(addr: impl ToSocketAddrs) -> std::io::Result<Self> {
@ -18,9 +14,10 @@ impl Connection {
}
/// Send a command to the display
pub fn send(&self, packet: impl ToPacket) -> std::io::Result<()> {
let packet = packet.to_packet();
self.socket.send(&*packet.to_bytes())?;
pub fn send(&self, packet: impl Into<Packet>) -> std::io::Result<()> {
let packet = packet.into();
let data: Vec<u8> = packet.into();
self.socket.send(&*data)?;
Ok(())
}
}