cargo clippy --fix

This commit is contained in:
Vinzenz Schroeter 2025-03-25 19:19:51 +01:00
parent 3384cc4ee9
commit 5ba01ec4cc
24 changed files with 97 additions and 73 deletions

View file

@ -1,7 +1,10 @@
//! An example for how to send text to the display.
use clap::Parser;
use servicepoint::*;
use servicepoint::{
CharGrid, CharGridCommand, ClearCommand, Connection, Origin, UdpConnection,
TILE_WIDTH,
};
#[derive(Parser, Debug)]
struct Cli {

View file

@ -1,7 +1,11 @@
//! Show a brightness level test pattern on screen
use clap::Parser;
use servicepoint::*;
use servicepoint::{
Bitmap, BitmapCommand, Brightness, BrightnessGrid, BrightnessGridCommand,
CompressionCode, Connection, DataRef, Grid, Origin, UdpConnection,
TILE_HEIGHT, TILE_WIDTH,
};
#[derive(Parser, Debug)]
struct Cli {

View file

@ -1,7 +1,10 @@
//! A simple example for how to send pixel data to the display.
use clap::Parser;
use servicepoint::*;
use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, Connection, Grid, Origin,
UdpConnection, FRAME_PACING, PIXEL_HEIGHT, PIXEL_WIDTH,
};
use std::thread;
#[derive(Parser, Debug)]

View file

@ -1,7 +1,10 @@
//! An example on how to modify the image on screen without knowing the current content.
use clap::Parser;
use servicepoint::*;
use servicepoint::{
Bitmap, BitmapCommand, CompressionCode, Connection, Grid, Origin,
UdpConnection, FRAME_PACING, PIXEL_HEIGHT, PIXEL_WIDTH,
};
use std::thread;
use std::time::Duration;

View file

@ -205,14 +205,17 @@ mod tests {
} = p;
// mangle it
for byte in payload.iter_mut() {
for byte in &mut payload {
*byte -= *byte / 2;
}
let p = Packet { header, payload };
let result = TypedCommand::try_from(p);
if *compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
assert_eq!(
result,
Err(TryFromPacketError::DecompressionFailed)
);
} else {
assert!(result.is_ok());
}

View file

@ -27,9 +27,9 @@ pub enum BinaryOperation {
///
/// `new_bit = old_bit op sent_bit`
///
/// For example, [BinaryOperation::Or] can be used to turn on some pixels without affecting other pixels.
/// For example, [`BinaryOperation::Or`] can be used to turn on some pixels without affecting other pixels.
///
/// The contained [BitVec] is always uncompressed.
/// The contained [`BitVec`] is always uncompressed.
#[derive(Clone, PartialEq, Debug, Eq)]
pub struct BitVecCommand {
/// the pixels to send to the display as one long row
@ -87,7 +87,7 @@ impl TryFrom<Packet> for BitVecCommand {
payload,
} = packet;
let command_code = CommandCode::try_from(command_code)
.map_err(|_| TryFromPacketError::InvalidCommand(command_code))?;
.map_err(|()| TryFromPacketError::InvalidCommand(command_code))?;
let operation = match command_code {
CommandCode::BitmapLinear => BinaryOperation::Overwrite,
CommandCode::BitmapLinearAnd => BinaryOperation::And,
@ -205,14 +205,17 @@ mod tests {
} = p;
// mangle it
for byte in payload.iter_mut() {
for byte in &mut payload {
*byte -= *byte / 2;
}
let p = Packet { header, payload };
let result = TypedCommand::try_from(p);
if *compression != CompressionCode::Uncompressed {
assert_eq!(result, Err(TryFromPacketError::DecompressionFailed))
assert_eq!(
result,
Err(TryFromPacketError::DecompressionFailed)
);
} else {
// when not compressing, there is no way to detect corrupted data
assert!(result.is_ok());

View file

@ -101,7 +101,7 @@ mod tests {
brightness: Brightness::MAX
},
Brightness::MAX.into()
)
);
}
#[test]
@ -130,7 +130,7 @@ mod tests {
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
));
}
#[test]

View file

@ -69,7 +69,7 @@ mod tests {
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
));
}
#[test]

View file

@ -1,7 +1,7 @@
use crate::LoadBitmapError;
use std::num::TryFromIntError;
/// Err values for [crate::TypedCommand::try_from].
/// Err values for [`crate::TypedCommand::try_from`].
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum TryFromPacketError {
/// the contained command code does not correspond to a known command

View file

@ -47,7 +47,7 @@ mod tests {
use crate::command_code::CommandCode;
use crate::commands::errors::TryFromPacketError;
use crate::commands::tests::{round_trip, TestImplementsCommand};
use crate::{ClearCommand, FadeOutCommand, Header, Packet, TypedCommand};
use crate::{FadeOutCommand, Header, Packet, TypedCommand};
impl TestImplementsCommand for FadeOutCommand {}
@ -72,7 +72,7 @@ mod tests {
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
));
}
#[test]
@ -91,6 +91,6 @@ mod tests {
assert!(matches!(
result,
Err(TryFromPacketError::UnexpectedPayloadSize(0, 2))
))
));
}
}

View file

@ -72,6 +72,6 @@ mod test {
assert!(matches!(
result,
Err(TryFromPacketError::ExtraneousHeaderValues)
))
));
}
}

View file

@ -12,7 +12,7 @@ mod hard_reset;
mod typed;
use crate::command_code::CommandCode;
use crate::*;
use crate::{Header, Packet};
use std::fmt::Debug;
pub use bitmap::*;
@ -34,20 +34,20 @@ pub use typed::*;
///
/// # Available commands
///
/// To send text, take a look at [Cp437GridCommand].
/// To send text, take a look at [`Cp437GridCommand`].
///
/// To draw pixels, the easiest command to use is [BitmapCommand].
/// To draw pixels, the easiest command to use is [`BitmapCommand`].
///
/// The other BitmapLinear-Commands operate on a region of pixel memory directly.
/// [BitVecCommand] overwrites a region.
/// [BitmapLinearOr], [BitmapLinearAnd] and [BitmapLinearXor] apply logical operations per pixel.
/// [`BitVecCommand`] overwrites a region.
/// [`BitmapLinearOr`], [`BitmapLinearAnd`] and [`BitmapLinearXor`] apply logical operations per pixel.
///
/// Out of bounds operations may be truncated or ignored by the display.
///
/// # Compression
///
/// Some commands can contain compressed payloads.
/// To get started, use [CompressionCode::default].
/// To get started, use [`CompressionCode::default`].
///
/// If you want to archive the best performance (e.g. latency),
/// you can try the different compression algorithms for your hardware and use case.

View file

@ -141,6 +141,6 @@ mod tests {
assert!(matches!(
result,
Err(TryFromPacketError::InvalidCommand(0xFF))
))
));
}
}

View file

@ -37,6 +37,6 @@ mod tests {
fn send_fake() {
let data: &[u8] = &[0u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let packet = Packet::try_from(data).unwrap();
FakeConnection.send(packet).unwrap()
FakeConnection.send(packet).unwrap();
}
}

View file

@ -5,7 +5,7 @@ use std::{error::Error, fmt::Debug, net::UdpSocket};
///
/// Use this when sending commands directly to the display.
///
/// Requires the feature "protocol_udp" which is enabled by default.
/// Requires the feature "`protocol_udp`" which is enabled by default.
#[derive(Debug)]
pub struct UdpConnection {
socket: UdpSocket,

View file

@ -1,7 +1,7 @@
/// A byte-packed vector of booleans.
///
/// The implementation is provided by [bitvec].
/// This is an alias for the specific type of [bitvec::BitVec] used in this crate.
/// This is an alias for the specific type of [`bitvec::BitVec`] used in this crate.
pub type BitVec = bitvec::BitVec<u8, bitvec::Msb0>;
pub mod bitvec {

View file

@ -1,4 +1,4 @@
use crate::*;
use crate::{BitVec, DataRef, Grid, ValueGrid, PIXEL_HEIGHT, PIXEL_WIDTH};
use ::bitvec::{order::Msb0, prelude::BitSlice, slice::IterMut};
/// A fixed-size 2D grid of booleans.
@ -25,7 +25,7 @@ impl Bitmap {
/// Creates a new [Bitmap] with the specified dimensions.
/// The initial state of the contained pixels is false.
///
/// The width has to be a multiple of [TILE_SIZE], otherwise this function returns None.
/// The width has to be a multiple of [`TILE_SIZE`], otherwise this function returns None.
///
/// # Arguments
///
@ -86,7 +86,7 @@ impl Bitmap {
})
}
/// Creates a [Bitmap] with the specified width from the provided [BitVec] without copying it.
/// Creates a [Bitmap] with the specified width from the provided [`BitVec`] without copying it.
///
/// The data cannot be loaded on the following cases:
/// - when the data size is not divisible by the width (incomplete rows)
@ -182,7 +182,7 @@ impl Grid<bool> for Bitmap {
/// When accessing `x` or `y` out of bounds.
fn set(&mut self, x: usize, y: usize, value: bool) {
self.assert_in_bounds(x, y);
self.bit_vec.set(x + y * self.width, value)
self.bit_vec.set(x + y * self.width, value);
}
fn get(&self, x: usize, y: usize) -> bool {
@ -227,7 +227,7 @@ impl From<Bitmap> for Vec<u8> {
}
impl From<Bitmap> for BitVec {
/// Turns a [Bitmap] into the underlying [BitVec].
/// Turns a [Bitmap] into the underlying [`BitVec`].
fn from(value: Bitmap) -> Self {
value.bit_vec
}
@ -358,7 +358,7 @@ mod tests {
#[test]
fn iter() {
let grid = Bitmap::new(8, 2).unwrap();
assert_eq!(16, grid.iter().count())
assert_eq!(16, grid.iter().count());
}
#[test]
@ -415,7 +415,7 @@ mod tests {
assert_eq!(
Bitmap::load(7, 3, &data),
Err(LoadBitmapError::InvalidWidth)
)
);
}
#[test]
@ -424,7 +424,7 @@ mod tests {
assert_eq!(
Bitmap::load(8, 3, &data),
Err(LoadBitmapError::InvalidDataSize)
)
);
}
#[test]
@ -433,7 +433,7 @@ mod tests {
assert_eq!(
Bitmap::from_bitvec(7, data),
Err(LoadBitmapError::InvalidWidth)
)
);
}
#[test]
@ -442,7 +442,7 @@ mod tests {
assert_eq!(
Bitmap::from_bitvec(8, data),
Err(LoadBitmapError::InvalidDataSize)
)
);
}
#[test]
@ -453,6 +453,6 @@ mod tests {
#[test]
fn new_invalid_width() {
assert_eq!(Bitmap::new(7, 2), None)
assert_eq!(Bitmap::new(7, 2), None);
}
}

View file

@ -19,7 +19,8 @@ use crate::{Brightness, ByteGrid, Grid, ValueGrid};
pub type BrightnessGrid = ValueGrid<Brightness>;
impl BrightnessGrid {
/// Like [Self::load], but ignoring any out-of-range brightness values
/// Like [`Self::load`], but ignoring any out-of-range brightness values
#[must_use]
pub fn saturating_load(
width: usize,
height: usize,

View file

@ -1,4 +1,4 @@
use crate::ValueGrid;
/// A 2d grid of bytes - see [ValueGrid].
/// A 2d grid of bytes - see [`ValueGrid`].
pub type ByteGrid = ValueGrid<u8>;

View file

@ -3,9 +3,9 @@ use std::string::FromUtf8Error;
/// A grid containing UTF-8 characters.
///
/// To send a CharGrid to the display, use a [crate::CharGridCommand].
/// To send a `CharGrid` to the display, use a [`crate::CharGridCommand`].
///
/// Also see [ValueGrid] for the non-specialized operations and examples.
/// Also see [`ValueGrid`] for the non-specialized operations and examples.
///
/// # Examples
///
@ -21,11 +21,11 @@ use std::string::FromUtf8Error;
pub type CharGrid = ValueGrid<char>;
impl CharGrid {
/// Loads a [CharGrid] with the specified width from the provided text, wrapping to as many rows as needed.
/// Loads a [`CharGrid`] with the specified width from the provided text, wrapping to as many rows as needed.
///
/// The passed rows are extended with '\0' if needed.
///
/// returns: [CharGrid] that contains a copy of the provided data.
/// returns: [`CharGrid`] that contains a copy of the provided data.
///
/// # Examples
///
@ -54,7 +54,7 @@ impl CharGrid {
for (row, text_line) in lines.iter().enumerate() {
#[allow(clippy::unwrap_used)]
// we calculated the width before setting
result.set_row_str(row, text_line).unwrap()
result.set_row_str(row, text_line).unwrap();
}
result
}
@ -70,6 +70,7 @@ impl CharGrid {
/// let grid = CharGrid::from("ab\ncd");
/// let col = grid.get_col_str(0).unwrap(); // "ac"
/// ```
#[must_use]
pub fn get_col_str(&self, x: usize) -> Option<String> {
Some(String::from_iter(self.get_col(x)?))
}
@ -85,13 +86,14 @@ impl CharGrid {
/// let grid = CharGrid::from("ab\ncd");
/// let row = grid.get_row_str(0).unwrap(); // "ab"
/// ```
#[must_use]
pub fn get_row_str(&self, y: usize) -> Option<String> {
Some(String::from_iter(self.get_row(y)?))
}
/// Overwrites a row in the grid with a str.
///
/// Returns [SetValueSeriesError] if y is out of bounds or `row` is not of the correct size.
/// Returns [`SetValueSeriesError`] if y is out of bounds or `row` is not of the correct size.
///
/// # Examples
///
@ -110,7 +112,7 @@ impl CharGrid {
/// Overwrites a column in the grid with a str.
///
/// Returns [SetValueSeriesError] if y is out of bounds or `row` is not of the correct size.
/// Returns [`SetValueSeriesError`] if y is out of bounds or `row` is not of the correct size.
///
/// # Examples
///
@ -127,9 +129,9 @@ impl CharGrid {
self.set_col(x, value.chars().collect::<Vec<_>>().as_ref())
}
/// Loads a [CharGrid] with the specified dimensions from the provided UTF-8 bytes.
/// Loads a [`CharGrid`] with the specified dimensions from the provided UTF-8 bytes.
///
/// returns: [CharGrid] that contains the provided data, or [FromUtf8Error] if the data is invalid.
/// returns: [`CharGrid`] that contains the provided data, or [`FromUtf8Error`] if the data is invalid.
///
/// # Examples
///
@ -193,7 +195,7 @@ impl From<CharGrid> for String {
}
impl From<&CharGrid> for String {
/// Converts a [CharGrid] into a [String].
/// Converts a [`CharGrid`] into a [String].
///
/// Rows are separated by '\n'.
///
@ -215,7 +217,7 @@ impl From<&CharGrid> for String {
}
impl From<&CharGrid> for Vec<u8> {
/// Converts a [CharGrid] into a [`Vec<u8>`].
/// Converts a [`CharGrid`] into a [`Vec<u8>`].
///
/// Rows are not separated.
///

View file

@ -18,7 +18,7 @@ pub struct InvalidCharError {
}
impl Cp437Grid {
/// Load an ASCII-only [&str] into a [Cp437Grid] of specified width.
/// Load an ASCII-only [&str] into a [`Cp437Grid`] of specified width.
///
/// # Panics
///

View file

@ -1,9 +1,9 @@
use std::fmt::Debug;
use std::slice::{Iter, IterMut};
use crate::*;
use crate::{DataRef, Grid};
/// A type that can be stored in a [ValueGrid], e.g. [char], [u8].
/// A type that can be stored in a [`ValueGrid`], e.g. [char], [u8].
pub trait Value: Sized + Default + Copy + Clone + Debug {}
impl<T: Sized + Default + Copy + Clone + Debug> Value for T {}
@ -12,7 +12,7 @@ impl<T: Sized + Default + Copy + Clone + Debug> Value for T {}
/// The memory layout is the one the display expects in [Command]s.
///
/// This structure can be used with any type that implements the [Value] trait.
/// You can also use the concrete type aliases provided in this crate, e.g. [CharGrid] and [ByteGrid].
/// You can also use the concrete type aliases provided in this crate, e.g. [`CharGrid`] and [`ByteGrid`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValueGrid<T: Value> {
width: usize,
@ -42,14 +42,14 @@ pub enum SetValueSeriesError {
}
impl<T: Value> ValueGrid<T> {
/// Creates a new [ValueGrid] with the specified dimensions.
/// Creates a new [`ValueGrid`] with the specified dimensions.
///
/// # Arguments
///
/// - width: size in x-direction
/// - height: size in y-direction
///
/// returns: [ValueGrid] initialized to default value.
/// returns: [`ValueGrid`] initialized to default value.
#[must_use]
pub fn new(width: usize, height: usize) -> Self {
Self {
@ -59,9 +59,9 @@ impl<T: Value> ValueGrid<T> {
}
}
/// Loads a [ValueGrid] with the specified dimensions from the provided data.
/// Loads a [`ValueGrid`] with the specified dimensions from the provided data.
///
/// returns: [ValueGrid] that contains a copy of the provided data,
/// returns: [`ValueGrid`] that contains a copy of the provided data,
/// or None if the dimensions do not match the data size.
#[must_use]
pub fn load(width: usize, height: usize, data: &[T]) -> Option<Self> {
@ -75,11 +75,11 @@ impl<T: Value> ValueGrid<T> {
})
}
/// Creates a [ValueGrid] with the specified width from the provided data,
/// Creates a [`ValueGrid`] with the specified width from the provided data,
/// wrapping to as many rows as needed,
/// without copying the vec.
///
/// returns: [ValueGrid] that contains the provided data,
/// returns: [`ValueGrid`] that contains the provided data,
/// or None if the data size is not divisible by the width.
///
/// # Examples
@ -96,9 +96,9 @@ impl<T: Value> ValueGrid<T> {
return None;
}
Some(Self {
data,
width,
height,
data,
})
}
@ -110,13 +110,13 @@ impl<T: Value> ValueGrid<T> {
) -> Self {
debug_assert_eq!(data.len(), width * height);
Self {
data,
width,
height,
data,
}
}
/// Iterate over all cells in [ValueGrid].
/// Iterate over all cells in [`ValueGrid`].
///
/// Order is equivalent to the following loop:
/// ```
@ -132,7 +132,7 @@ impl<T: Value> ValueGrid<T> {
self.data.iter()
}
/// Iterate over all rows in [ValueGrid] top to bottom.
/// Iterate over all rows in [`ValueGrid`] top to bottom.
pub fn iter_rows(&self) -> IterGridRows<T> {
IterGridRows { grid: self, row: 0 }
}
@ -177,9 +177,9 @@ impl<T: Value> ValueGrid<T> {
}
}
/// Convert between ValueGrid types.
/// Convert between `ValueGrid` types.
///
/// See also [Iterator::map].
/// See also [`Iterator::map`].
///
/// # Examples
///
@ -215,16 +215,18 @@ impl<T: Value> ValueGrid<T> {
/// Copies a row from the grid.
///
/// Returns [None] if y is out of bounds.
#[must_use]
pub fn get_row(&self, y: usize) -> Option<Vec<T>> {
self.data
.chunks_exact(self.width())
.nth(y)
.map(|row| row.to_vec())
.map(<[T]>::to_vec)
}
/// Copies a column from the grid.
///
/// Returns [None] if x is out of bounds.
#[must_use]
pub fn get_col(&self, x: usize) -> Option<Vec<T>> {
self.data
.chunks_exact(self.width())
@ -480,7 +482,7 @@ mod tests {
data_ref.copy_from_slice(&[1, 2, 3, 4]);
assert_eq!(vec.data, [1, 2, 3, 4]);
assert_eq!(vec.get(1, 0), 2)
assert_eq!(vec.get(1, 0), 2);
}
#[test]

View file

@ -126,7 +126,7 @@ mod tests_feature_cp437 {
let cp437 = str_to_cp437(utf8);
let actual = cp437_to_str(&cp437);
assert_eq!(utf8, actual)
assert_eq!(utf8, actual);
}
#[test]

View file

@ -203,6 +203,6 @@ mod tests {
#[test]
fn too_small() {
let data = vec![0u8; 4];
assert_eq!(Packet::try_from(data.as_slice()), Err(()))
assert_eq!(Packet::try_from(data.as_slice()), Err(()));
}
}