Skip to content

Commit

Permalink
Revert adding generic sizes for Stamps
Browse files Browse the repository at this point in the history
  • Loading branch information
bellinitte committed Oct 26, 2022
1 parent fba8792 commit a3fe271
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 180 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Designed primarily for `#![no_std]` usage, in embedded or other program-memory-c
stockbook = "0.2.0
```
The main functionality of Stockbook is the `stamp!` macro, which lets you include data similarly to how [`include_bytes!`](https://doc.rust-lang.org/stable/core/macro.include_bytes.html) does, but from an image, specifically a 1-bit black and white image. The macro returns a `Stamp` type, which just holds a static reference to the pixel data — the size of the image is encoded statically in the type. The pixel data is represented internally as an array of bytes, in which individual bits correspond to individual pixels.
The main functionality of Stockbook is the `stamp!` macro, which lets you include data similarly to how [`include_bytes!`](https://doc.rust-lang.org/stable/core/macro.include_bytes.html) does, but from an image, specifically a 1-bit black and white image. The macro returns a `Stamp` type, which just holds the image's width, height, and a static reference to the pixel data. The pixel data is represented internally as an array of bytes, in which individual bits correspond to individual pixels.
## Example
Expand All @@ -24,9 +24,9 @@ File `assets/star.png` (scaled x8 for preview, originally 12x12 px):
File `src/lib.rs`:
```rust
use stockbook::{stamp, Color, Size, Stamp};
use stockbook::{stamp, Color, Stamp};
static STAR_SPRITE: Stamp<Size<12, 12>> = stamp!("assets/star.png");
static STAR_SPRITE: Stamp = stamp!("assets/star.png");

pub fn draw_star() {
for (x, y, color) in STAR_SPRITE.pixels() {
Expand Down
10 changes: 5 additions & 5 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ use syn::{
/// `image.png`, and a `main.rs` with the following contents:
///
/// ```rust,ignore
/// use stockbook::{stamp, Size, Stamp};
/// use stockbook::{stamp, Stamp};
///
/// static IMAGE: Stamp<Size<16, 12>> = stamp!("image.png");
/// static IMAGE: Stamp = stamp!("image.png");
/// ```
///
/// Compiling `main.rs` is going to statically embed the image in the binary.
Expand All @@ -46,11 +46,11 @@ use syn::{
/// For example, this fails to compile:
///
/// ```rust,ignore
/// use stockbook::{stamp, Size, Stamp};
/// use stockbook::{stamp, Stamp};
///
/// const IMAGE_PATH: &str = "image.png";
///
/// static IMAGE: Stamp<Size<16, 12>> = stamp!(IMAGE_PATH); // passing an identifier, not a string literal
/// static IMAGE: Stamp = stamp!(IMAGE_PATH); // passing an identifier, not a string literal
/// ```
///
/// ## Relative paths
Expand Down Expand Up @@ -194,7 +194,7 @@ impl ToTokens for Stamp {

tokens.extend(quote! {
unsafe {
::stockbook::Stamp::<::stockbook::Size<#width, #height>>::from_raw_unchecked(#array)
::stockbook::Stamp::from_raw_unchecked(#width, #height, #array)
}
});
}
Expand Down
52 changes: 25 additions & 27 deletions src/iter/pixels.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::{dynamic, traits, Color, Stamp};
use crate::{Color, Stamp};
use core::iter::FusedIterator;

/// An iterator that yields all pixels of a [`Stamp`].
///
/// This type is created by the [`pixels`](Stamp::pixels) method on [`Stamp`]. See
/// its documentation for more details.
#[derive(Debug)]
pub struct Pixels<'a, S: traits::Size = dynamic::Size> {
cursor: Cursor<'a, S>,
cursor_back: CursorBack<'a, S>,
pub struct Pixels<'a> {
cursor: Cursor<'a>,
cursor_back: CursorBack<'a>,
remaining: usize,
}

impl<'a, S: traits::Size> Pixels<'a, S> {
pub(crate) fn new(stamp: &'a Stamp<S>) -> Self {
impl<'a> Pixels<'a> {
pub(crate) fn new(stamp: &'a Stamp) -> Self {
Self {
cursor: Cursor::new(stamp),
cursor_back: CursorBack::new(stamp),
Expand All @@ -24,19 +24,19 @@ impl<'a, S: traits::Size> Pixels<'a, S> {

/// An iterator that cycles throygh all pixels of a [`Stamp`] from front to back.
#[derive(Debug)]
struct Cursor<'a, S: traits::Size> {
struct Cursor<'a> {
x: usize,
y: usize,
stamp: &'a Stamp<S>,
stamp: &'a Stamp,
}

impl<'a, S: traits::Size> Cursor<'a, S> {
fn new(stamp: &'a Stamp<S>) -> Self {
impl<'a> Cursor<'a> {
fn new(stamp: &'a Stamp) -> Self {
Self { x: 0, y: 0, stamp }
}
}

impl<S: traits::Size> Iterator for Cursor<'_, S> {
impl Iterator for Cursor<'_> {
type Item = (usize, usize, Color);

fn next(&mut self) -> Option<(usize, usize, Color)> {
Expand All @@ -58,14 +58,14 @@ impl<S: traits::Size> Iterator for Cursor<'_, S> {

/// An iterator that cycles throygh all pixels of a [`Stamp`] from back to front.
#[derive(Debug)]
struct CursorBack<'a, S: traits::Size> {
struct CursorBack<'a> {
x: usize,
y: usize,
stamp: &'a Stamp<S>,
stamp: &'a Stamp,
}

impl<'a, S: traits::Size> CursorBack<'a, S> {
fn new(stamp: &'a Stamp<S>) -> Self {
impl<'a> CursorBack<'a> {
fn new(stamp: &'a Stamp) -> Self {
Self {
x: stamp.width().saturating_sub(1),
y: stamp.height().saturating_sub(1),
Expand All @@ -74,7 +74,7 @@ impl<'a, S: traits::Size> CursorBack<'a, S> {
}
}

impl<S: traits::Size> Iterator for CursorBack<'_, S> {
impl Iterator for CursorBack<'_> {
type Item = (usize, usize, Color);

fn next(&mut self) -> Option<(usize, usize, Color)> {
Expand All @@ -96,7 +96,7 @@ impl<S: traits::Size> Iterator for CursorBack<'_, S> {
}
}

impl<S: traits::Size> Iterator for Pixels<'_, S> {
impl Iterator for Pixels<'_> {
type Item = (usize, usize, Color);

fn next(&mut self) -> Option<Self::Item> {
Expand All @@ -109,50 +109,48 @@ impl<S: traits::Size> Iterator for Pixels<'_, S> {
}
}

impl<S: traits::Size> DoubleEndedIterator for Pixels<'_, S> {
impl DoubleEndedIterator for Pixels<'_> {
fn next_back(&mut self) -> Option<Self::Item> {
self.remaining = self.remaining.checked_sub(1)?;
self.cursor_back.next()
}
}

impl<S: traits::Size> ExactSizeIterator for Pixels<'_, S> {}
impl ExactSizeIterator for Pixels<'_> {}

impl<S: traits::Size> FusedIterator for Pixels<'_, S> {}
impl FusedIterator for Pixels<'_> {}

#[cfg(test)]
mod tests {
use crate::Size;

use super::*;

#[test]
fn test_zero_size_stamp() {
let stamp = Stamp::<Size<0, 0>>::from_raw(&[]);
let stamp = Stamp::from_raw(0, 0, &[]);
let mut pixels = stamp.pixels();

assert_eq!(pixels.next(), None);
}

#[test]
fn test_zero_width_stamp() {
let stamp = Stamp::<Size<0, 3>>::from_raw(&[]);
let stamp = Stamp::from_raw(0, 3, &[]);
let mut pixels = stamp.pixels();

assert_eq!(pixels.next(), None);
}

#[test]
fn test_zero_height_stamp() {
let stamp = Stamp::<Size<3, 0>>::from_raw(&[]);
let stamp = Stamp::from_raw(3, 0, &[]);
let mut pixels = stamp.pixels();

assert_eq!(pixels.next(), None);
}

#[test]
fn test_double_ended() {
let stamp = Stamp::<Size<2, 2>>::from_raw(&[0b1010_0000]);
let stamp = Stamp::from_raw(2, 2, &[0b1010_0000]);
let mut pixels = stamp.pixels();

assert_eq!(pixels.next(), Some((0, 0, Color::White)));
Expand All @@ -165,7 +163,7 @@ mod tests {

#[test]
fn test_rev() {
let stamp = Stamp::<Size<2, 2>>::from_raw(&[0b1010_0000]);
let stamp = Stamp::from_raw(2, 2, &[0b1010_0000]);
let mut pixels = stamp.pixels().rev();

assert_eq!(pixels.next(), Some((1, 1, Color::Black)));
Expand Down

0 comments on commit a3fe271

Please sign in to comment.