remove SendCommandExt
This commit is contained in:
parent
473bbbc3f9
commit
4e1433b54c
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use servicepoint::{
|
use servicepoint::{
|
||||||
CharGrid, CharGridCommand, ClearCommand, SendCommandExt, TILE_WIDTH,
|
CharGrid, CharGridCommand, ClearCommand, UdpSocketExt, TILE_WIDTH,
|
||||||
};
|
};
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use servicepoint::{
|
use servicepoint::{
|
||||||
Bitmap, BitmapCommand, Brightness, BrightnessGrid, BrightnessGridCommand,
|
Bitmap, BitmapCommand, Brightness, BrightnessGrid, BrightnessGridCommand,
|
||||||
DataRef, Grid, SendCommandExt, TILE_HEIGHT, TILE_WIDTH,
|
DataRef, Grid, UdpSocketExt, TILE_HEIGHT, TILE_WIDTH,
|
||||||
};
|
};
|
||||||
use std::net::UdpSocket;
|
use std::net::UdpSocket;
|
||||||
|
|
||||||
|
@ -28,7 +28,7 @@ fn main() {
|
||||||
let max_brightness: u8 = Brightness::MAX.into();
|
let max_brightness: u8 = Brightness::MAX.into();
|
||||||
let mut brightnesses = BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT);
|
let mut brightnesses = BrightnessGrid::new(TILE_WIDTH, TILE_HEIGHT);
|
||||||
for (index, byte) in brightnesses.data_ref_mut().iter_mut().enumerate() {
|
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();
|
*byte = Brightness::try_from(level).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use servicepoint::{
|
use servicepoint::{
|
||||||
Bitmap, BitmapCommand, Grid, SendCommandExt, FRAME_PACING, PIXEL_HEIGHT,
|
Bitmap, BitmapCommand, Grid, UdpSocketExt, FRAME_PACING, PIXEL_HEIGHT,
|
||||||
PIXEL_WIDTH,
|
PIXEL_WIDTH,
|
||||||
};
|
};
|
||||||
use std::{net::UdpSocket, thread};
|
use std::{net::UdpSocket, thread};
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
|
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use servicepoint::{CharGrid, CharGridCommand, ClearCommand, SendCommandExt};
|
use servicepoint::{CharGrid, CharGridCommand, ClearCommand, UdpSocketExt};
|
||||||
use std::net::{SocketAddr, UdpSocket};
|
use std::net::{SocketAddr, UdpSocket};
|
||||||
|
|
||||||
/// This is the entry point of the example.
|
/// This is the entry point of the example.
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use servicepoint::{
|
use servicepoint::{
|
||||||
Bitmap, BitmapCommand, Grid, SendCommandExt, FRAME_PACING, PIXEL_HEIGHT,
|
Bitmap, BitmapCommand, Grid, UdpSocketExt, FRAME_PACING, PIXEL_HEIGHT,
|
||||||
PIXEL_WIDTH,
|
PIXEL_WIDTH,
|
||||||
};
|
};
|
||||||
use std::{net::UdpSocket, thread, time::Duration};
|
use std::{net::UdpSocket, thread, time::Duration};
|
||||||
|
|
|
@ -68,7 +68,7 @@ pub(crate) fn into_decompressed(
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
Ok(result) => Some(result),
|
Ok(result) => Some(result),
|
||||||
}
|
},
|
||||||
#[cfg(feature = "compression_zstd")]
|
#[cfg(feature = "compression_zstd")]
|
||||||
CompressionCode::Zstd => {
|
CompressionCode::Zstd => {
|
||||||
let mut decoder = match ZstdDecoder::new(&*payload) {
|
let mut decoder = match ZstdDecoder::new(&*payload) {
|
||||||
|
|
|
@ -2,13 +2,22 @@ use crate::Packet;
|
||||||
use std::net::{Ipv4Addr, ToSocketAddrs};
|
use std::net::{Ipv4Addr, ToSocketAddrs};
|
||||||
use std::{convert::TryInto, net::UdpSocket};
|
use std::{convert::TryInto, net::UdpSocket};
|
||||||
|
|
||||||
/// Allows sending commands through more types
|
/// Provides servicepoint specific extensions for `UdpSocket`
|
||||||
pub trait SendCommandExt {
|
pub trait UdpSocketExt {
|
||||||
/// Serializes the command and sends it through the underlying transport
|
/// 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<()>;
|
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<()> {
|
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
|
||||||
let packet = command.try_into().ok()?;
|
let packet = command.try_into().ok()?;
|
||||||
let vec: Vec<_> = packet.into();
|
let vec: Vec<_> = packet.into();
|
||||||
|
@ -20,24 +29,12 @@ impl SendCommandExt for UdpSocket {
|
||||||
/// A fake connection for testing that does not actually send anything.
|
/// A fake connection for testing that does not actually send anything.
|
||||||
pub struct FakeConnection;
|
pub struct FakeConnection;
|
||||||
|
|
||||||
impl SendCommandExt for FakeConnection {
|
impl FakeConnection {
|
||||||
fn send_command(&self, command: impl TryInto<Packet>) -> Option<()> {
|
/// 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()?;
|
let packet = command.try_into().ok()?;
|
||||||
drop(Vec::from(packet));
|
drop(Vec::from(packet));
|
||||||
Some(())
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue