remove SendCommandExt

This commit is contained in:
Vinzenz Schroeter 2025-05-03 10:05:58 +02:00
parent 473bbbc3f9
commit 4e1433b54c
7 changed files with 24 additions and 27 deletions

View file

@ -2,7 +2,7 @@
use clap::Parser;
use servicepoint::{
CharGrid, CharGridCommand, ClearCommand, SendCommandExt, TILE_WIDTH,
CharGrid, CharGridCommand, ClearCommand, UdpSocketExt, TILE_WIDTH,
};
use std::net::UdpSocket;

View file

@ -3,7 +3,7 @@
use clap::Parser;
use servicepoint::{
Bitmap, BitmapCommand, Brightness, BrightnessGrid, BrightnessGridCommand,
DataRef, Grid, SendCommandExt, TILE_HEIGHT, TILE_WIDTH,
DataRef, Grid, UdpSocketExt, TILE_HEIGHT, TILE_WIDTH,
};
use std::net::UdpSocket;
@ -28,7 +28,7 @@ fn main() {
let max_brightness: u8 = Brightness::MAX.into();
let mut brightnesses = BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT);
for (index, byte) in brightnesses.data_ref_mut().iter_mut().enumerate() {
let level = index as u8 % max_brightness;
let level = (index % u8::MAX as usize) as u8 % max_brightness;
*byte = Brightness::try_from(level).unwrap();
}

View file

@ -2,7 +2,7 @@
use clap::Parser;
use servicepoint::{
Bitmap, BitmapCommand, Grid, SendCommandExt, FRAME_PACING, PIXEL_HEIGHT,
Bitmap, BitmapCommand, Grid, UdpSocketExt, FRAME_PACING, PIXEL_HEIGHT,
PIXEL_WIDTH,
};
use std::{net::UdpSocket, thread};

View file

@ -21,7 +21,7 @@
#![no_main]
use servicepoint::{CharGrid, CharGridCommand, ClearCommand, SendCommandExt};
use servicepoint::{CharGrid, CharGridCommand, ClearCommand, UdpSocketExt};
use std::net::{SocketAddr, UdpSocket};
/// This is the entry point of the example.

View file

@ -2,7 +2,7 @@
use clap::Parser;
use servicepoint::{
Bitmap, BitmapCommand, Grid, SendCommandExt, FRAME_PACING, PIXEL_HEIGHT,
Bitmap, BitmapCommand, Grid, UdpSocketExt, FRAME_PACING, PIXEL_HEIGHT,
PIXEL_WIDTH,
};
use std::{net::UdpSocket, thread, time::Duration};

View file

@ -68,7 +68,7 @@ pub(crate) fn into_decompressed(
None
}
Ok(result) => Some(result),
}
},
#[cfg(feature = "compression_zstd")]
CompressionCode::Zstd => {
let mut decoder = match ZstdDecoder::new(&*payload) {

View file

@ -2,13 +2,22 @@ use crate::Packet;
use std::net::{Ipv4Addr, ToSocketAddrs};
use std::{convert::TryInto, net::UdpSocket};
/// Allows sending commands through more types
pub trait SendCommandExt {
/// Serializes the command and sends it through the underlying transport
/// Provides servicepoint specific extensions for `UdpSocket`
pub trait UdpSocketExt {
/// Creates a `UdpSocket` that can be used so send to the specified addr.
fn bind_connect(addr: impl ToSocketAddrs) -> std::io::Result<UdpSocket>;
/// Serializes the command and sends it through the socket
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()>;
}
impl SendCommandExt for UdpSocket {
impl UdpSocketExt for UdpSocket {
fn bind_connect(addr: impl ToSocketAddrs) -> std::io::Result<UdpSocket> {
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
socket.connect(addr)?;
Ok(socket)
}
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
let packet = command.try_into().ok()?;
let vec: Vec<_> = packet.into();
@ -20,24 +29,12 @@ impl SendCommandExt for UdpSocket {
/// A fake connection for testing that does not actually send anything.
pub struct FakeConnection;
impl SendCommandExt for FakeConnection {
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
impl FakeConnection {
/// Serializes the command, but does not actually send it as this is the fake connection
pub fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
_ = self; // suppress unused warning
let packet = command.try_into().ok()?;
drop(Vec::from(packet));
Some(())
}
}
/// Provides servicepoint specific extensions for `UdpSocket`
pub trait UdpSocketExt {
/// Creates a `UdpSocket` that can be used so send to the specified addr.
fn bind_connect(addr: impl ToSocketAddrs) -> std::io::Result<UdpSocket>;
}
impl UdpSocketExt for UdpSocket {
fn bind_connect(addr: impl ToSocketAddrs) -> std::io::Result<UdpSocket> {
let socket = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0))?;
socket.connect(addr)?;
Ok(socket)
}
}