diff options
author | 2024-03-27 12:54:01 -0700 | |
---|---|---|
committer | 2024-05-01 00:28:55 +0200 | |
commit | 8d27af24a76d9792e22b3380e11b846fd5533805 (patch) | |
tree | b1aa5fa56a5d7050f90682e28a27f51e65dffdb9 /core | |
parent | 89892f1760d4ec67f458d67ae722c3f06d524a1b (diff) | |
download | iced-8d27af24a76d9792e22b3380e11b846fd5533805.tar.gz iced-8d27af24a76d9792e22b3380e11b846fd5533805.tar.bz2 iced-8d27af24a76d9792e22b3380e11b846fd5533805.zip |
Utilize bytes::Bytes for images
Diffstat (limited to 'core')
-rw-r--r-- | core/Cargo.toml | 1 | ||||
-rw-r--r-- | core/src/image.rs | 62 |
2 files changed, 7 insertions, 56 deletions
diff --git a/core/Cargo.toml b/core/Cargo.toml index 7bd37021..3c557bca 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -19,6 +19,7 @@ advanced = [] [dependencies] bitflags.workspace = true +bytes.workspace = true glam.workspace = true log.workspace = true num-traits.workspace = true diff --git a/core/src/image.rs b/core/src/image.rs index dc74e5c1..7316ede7 100644 --- a/core/src/image.rs +++ b/core/src/image.rs @@ -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)] @@ -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(), }) } @@ -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 { @@ -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 { |