fix warnings, minor docs changes

This commit is contained in:
Vinzenz Schroeter 2025-01-12 02:36:43 +01:00
parent 8320ee2d80
commit dea176d0d9
8 changed files with 37 additions and 19 deletions

View file

@ -1,5 +1,3 @@
//! Implementation of [Bitmap]
use crate::data_ref::DataRef; use crate::data_ref::DataRef;
use crate::BitVec; use crate::BitVec;
use crate::*; use crate::*;

View file

@ -1,3 +1,4 @@
pub use bitvec::prelude::*; pub use bitvec::prelude::*;
/// An alias for the specific type of [bitvec::prelude::BitVec] used. /// An alias for the specific type of [bitvec::prelude::BitVec] used.
pub type BitVec = bitvec::prelude::BitVec<u8, Msb0>; pub type BitVec = bitvec::prelude::BitVec<u8, Msb0>;

View file

@ -1,2 +1,2 @@
/// A simple grid of bytes - see [primitive_grid::PrimitiveGrid]. /// A 2d grid of bytes - see [crate::ValueGrid].
pub type ByteGrid = crate::value_grid::ValueGrid<u8>; pub type ByteGrid = crate::value_grid::ValueGrid<u8>;

View file

@ -1,8 +1,22 @@
use crate::value_grid::{SetValueSeriesError, TryLoadPrimitiveGridError, ValueGrid}; use crate::{Grid, SetValueSeriesError, TryLoadValueGridError, ValueGrid};
use crate::Grid;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
/// A grid containing UTF-8 characters. /// A grid containing UTF-8 characters.
///
/// To send a CharGrid to the display, use [crate::Command::Utf8Data].
///
/// Also see [crate::ValueGrid] for the non-specialized operations and examples.
///
/// # Examples
///
/// ```rust
/// # use servicepoint::{CharGrid, Command, Connection, Origin};
/// let grid = CharGrid::from("You can\nload multiline\nstrings directly");
/// assert_eq!(grid.get_row_str(1), Some("load multiline\0\0".to_string()));
///
/// # let connection = Connection::Fake;
/// let command = Command::Utf8Data(Origin::ZERO, grid);
/// ```
pub type CharGrid = ValueGrid<char>; pub type CharGrid = ValueGrid<char>;
impl CharGrid { impl CharGrid {
@ -64,7 +78,7 @@ pub enum LoadUtf8Error {
#[error(transparent)] #[error(transparent)]
FromUtf8Error(#[from] FromUtf8Error), FromUtf8Error(#[from] FromUtf8Error),
#[error(transparent)] #[error(transparent)]
TryLoadError(#[from] TryLoadPrimitiveGridError), TryLoadError(#[from] TryLoadValueGridError),
} }
impl From<&str> for CharGrid { impl From<&str> for CharGrid {

View file

@ -522,7 +522,7 @@ impl Command {
String::from_utf8(payload.clone())?.chars().collect(); String::from_utf8(payload.clone())?.chars().collect();
Ok(Command::Utf8Data( Ok(Command::Utf8Data(
Origin::new(*a as usize, *b as usize), Origin::new(*a as usize, *b as usize),
CharGrid::load(*c as usize, *d as usize, &*payload), CharGrid::load(*c as usize, *d as usize, &payload),
)) ))
} }
} }

View file

@ -51,7 +51,7 @@ pub use crate::grid::Grid;
pub use crate::origin::{Origin, Pixels, Tiles}; pub use crate::origin::{Origin, Pixels, Tiles};
pub use crate::packet::{Header, Packet, Payload}; pub use crate::packet::{Header, Packet, Payload};
pub use crate::value_grid::{ pub use crate::value_grid::{
IterGridRows, SetValueSeriesError, TryLoadPrimitiveGridError, Value, ValueGrid, IterGridRows, SetValueSeriesError, TryLoadValueGridError, Value, ValueGrid,
}; };
mod bitmap; mod bitmap;

View file

@ -1,5 +1,3 @@
//! This module contains the implementation of the [ValueGrid].
use std::fmt::Debug; use std::fmt::Debug;
use std::slice::{Iter, IterMut}; use std::slice::{Iter, IterMut};
@ -9,7 +7,12 @@ use crate::*;
pub trait Value: Sized + Default + Copy + Clone + Debug {} pub trait Value: Sized + Default + Copy + Clone + Debug {}
impl<T: Sized + Default + Copy + Clone + Debug> Value for T {} impl<T: Sized + Default + Copy + Clone + Debug> Value for T {}
/// A 2D grid of bytes /// A 2D grid of values.
///
/// 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].
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct ValueGrid<T: Value> { pub struct ValueGrid<T: Value> {
width: usize, width: usize,
@ -78,14 +81,14 @@ 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 or [TryLoadPrimitiveGridError]. /// returns: [ValueGrid] that contains a copy of the provided data or [TryLoadValueGridError].
pub fn try_load( pub fn try_load(
width: usize, width: usize,
height: usize, height: usize,
data: Vec<T>, data: Vec<T>,
) -> Result<Self, TryLoadPrimitiveGridError> { ) -> Result<Self, TryLoadValueGridError> {
if width * height != data.len() { if width * height != data.len() {
return Err(TryLoadPrimitiveGridError::InvalidDimensions); return Err(TryLoadValueGridError::InvalidDimensions);
} }
Ok(Self { Ok(Self {
@ -159,7 +162,7 @@ impl<T: Value> ValueGrid<T> {
} }
} }
/// Convert between PrimitiveGrid types. /// Convert between ValueGrid types.
/// ///
/// See also [Iterator::map]. /// See also [Iterator::map].
/// ///
@ -273,14 +276,16 @@ impl<T: Value> ValueGrid<T> {
} }
} }
/// Errors that can occur when loading a grid
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum TryLoadPrimitiveGridError { pub enum TryLoadValueGridError {
#[error("The provided dimensions do not match with the data size")] #[error("The provided dimensions do not match with the data size")]
/// The provided dimensions do not match with the data size
InvalidDimensions, InvalidDimensions,
} }
impl<T: Value> Grid<T> for ValueGrid<T> { impl<T: Value> Grid<T> for ValueGrid<T> {
/// Sets the value of the cell at the specified position in the `PrimitiveGrid. /// Sets the value of the cell at the specified position in the `ValueGrid.
/// ///
/// # Arguments /// # Arguments
/// ///

View file

@ -8,9 +8,9 @@ use std::convert::Into;
use std::intrinsics::transmute; use std::intrinsics::transmute;
use std::ptr::NonNull; use std::ptr::NonNull;
/// see [Brightness::MIN] /// see [servicepoint::Brightness::MIN]
pub const SP_BRIGHTNESS_MIN: u8 = 0; pub const SP_BRIGHTNESS_MIN: u8 = 0;
/// see [Brightness::MAX] /// see [servicepoint::Brightness::MAX]
pub const SP_BRIGHTNESS_MAX: u8 = 11; pub const SP_BRIGHTNESS_MAX: u8 = 11;
/// Count of possible brightness values /// Count of possible brightness values
pub const SP_BRIGHTNESS_LEVELS: u8 = 12; pub const SP_BRIGHTNESS_LEVELS: u8 = 12;