Skip to content

Commit

Permalink
Utilize bytes::Bytes for images
Browse files Browse the repository at this point in the history
  • Loading branch information
Bajix committed Apr 2, 2024
1 parent 31d1d5f commit fa14216
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 56 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ iced_winit = { version = "0.13.0-dev", path = "winit" }

async-std = "1.0"
bitflags = "2.0"
bytes = "1.6"
bytemuck = { version = "1.0", features = ["derive"] }
cosmic-text = "0.10"
dark-light = "1.0"
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ advanced = []

[dependencies]
bitflags.workspace = true
bytes.workspace = true
glam.workspace = true
log.workspace = true
num-traits.workspace = true
Expand Down
62 changes: 6 additions & 56 deletions core/src/image.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Load and draw raster graphics.
pub use bytes::Bytes;

use crate::{Rectangle, Size};

use rustc_hash::FxHasher;
use std::hash::{Hash, Hasher as _};
use std::path::PathBuf;
use std::sync::Arc;

/// A handle of some image data.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -29,12 +30,12 @@ impl Handle {
pub fn from_pixels(
width: u32,
height: u32,
pixels: impl AsRef<[u8]> + Send + Sync + 'static,
pixels: impl Into<Bytes>,
) -> Handle {
Self::from_data(Data::Rgba {
width,
height,
pixels: Bytes::new(pixels),
pixels: pixels.into(),
})
}

Expand All @@ -44,10 +45,8 @@ impl Handle {
///
/// This is useful if you already have your image loaded in-memory, maybe
/// because you downloaded or generated it procedurally.
pub fn from_memory(
bytes: impl AsRef<[u8]> + Send + Sync + 'static,
) -> Handle {
Self::from_data(Data::Bytes(Bytes::new(bytes)))
pub fn from_memory(bytes: impl Into<Bytes>) -> Handle {
Self::from_data(Data::Bytes(bytes.into()))
}

fn from_data(data: Data) -> Handle {
Expand Down Expand Up @@ -86,55 +85,6 @@ impl Hash for Handle {
}
}

/// A wrapper around raw image data.
///
/// It behaves like a `&[u8]`.
#[derive(Clone)]
pub struct Bytes(Arc<dyn AsRef<[u8]> + Send + Sync + 'static>);

impl Bytes {
/// Creates new [`Bytes`] around `data`.
pub fn new(data: impl AsRef<[u8]> + Send + Sync + 'static) -> Self {
Self(Arc::new(data))
}
}

impl std::fmt::Debug for Bytes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.as_ref().as_ref().fmt(f)
}
}

impl std::hash::Hash for Bytes {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.as_ref().as_ref().hash(state);
}
}

impl PartialEq for Bytes {
fn eq(&self, other: &Self) -> bool {
let a = self.as_ref();
let b = other.as_ref();
core::ptr::eq(a, b) || a == b
}
}

impl Eq for Bytes {}

impl AsRef<[u8]> for Bytes {
fn as_ref(&self) -> &[u8] {
self.0.as_ref().as_ref()
}
}

impl std::ops::Deref for Bytes {
type Target = [u8];

fn deref(&self) -> &[u8] {
self.0.as_ref().as_ref()
}
}

/// The data of a raster image.
#[derive(Clone, PartialEq, Eq, Hash)]
pub enum Data {
Expand Down

0 comments on commit fa14216

Please sign in to comment.