diff options
author | 2024-05-01 01:52:49 +0200 | |
---|---|---|
committer | 2024-05-01 01:52:49 +0200 | |
commit | 58ea914ad21ea9c5ae7b7b1c167ed084c9ddb07a (patch) | |
tree | 2e6969c78cc856759efcc24dcef114ff35095336 | |
parent | b52c7bb610f593fffc624d461dca17ac50c81626 (diff) | |
download | iced-58ea914ad21ea9c5ae7b7b1c167ed084c9ddb07a.tar.gz iced-58ea914ad21ea9c5ae7b7b1c167ed084c9ddb07a.tar.bz2 iced-58ea914ad21ea9c5ae7b7b1c167ed084c9ddb07a.zip |
Make `image::Id` actually opaque
-rw-r--r-- | core/src/image.rs | 60 | ||||
-rw-r--r-- | examples/pokedex/src/main.rs | 2 | ||||
-rw-r--r-- | examples/screenshot/src/main.rs | 2 |
3 files changed, 40 insertions, 24 deletions
diff --git a/core/src/image.rs b/core/src/image.rs index a0e40787..c44ccc30 100644 --- a/core/src/image.rs +++ b/core/src/image.rs @@ -10,13 +10,26 @@ use std::path::{Path, PathBuf}; /// A handle of some image data. #[derive(Clone, PartialEq, Eq)] pub enum Handle { - /// File data + /// A file handle. The image data will be read + /// from the file path. + /// + /// Use [`from_path`] to create this variant. + /// + /// [`from_path`]: Self::from_path Path(Id, PathBuf), - /// In-memory data + /// A handle pointing to some encoded image bytes in-memory. + /// + /// Use [`from_bytes`] to create this variant. + /// + /// [`from_bytes`]: Self::from_bytes Bytes(Id, Bytes), - /// Decoded image pixels in RGBA format. + /// A handle pointing to decoded image pixels in RGBA format. + /// + /// Use [`from_rgba`] to create this variant. + /// + /// [`from_rgba`]: Self::from_bytes Rgba { /// The id of this handle. id: Id, @@ -39,12 +52,24 @@ impl Handle { Self::Path(Id::path(&path), path) } - /// Creates an image [`Handle`] containing the image pixels directly. This - /// function expects the input data to be provided as a `Vec<u8>` of RGBA - /// pixels. + /// Creates an image [`Handle`] containing the encoded image data directly. + /// + /// Makes an educated guess about the image format by examining the given data. + /// + /// This is useful if you already have your image loaded in-memory, maybe + /// because you downloaded or generated it procedurally. + pub fn from_bytes(bytes: impl Into<Bytes>) -> Handle { + Self::Bytes(Id::unique(), bytes.into()) + } + + /// Creates an image [`Handle`] containing the decoded image pixels directly. + /// + /// This function expects the pixel data to be provided as a collection of [`Bytes`] + /// of RGBA pixels. Therefore, the length of the pixel data should always be + /// `width * height * 4`. /// /// This is useful if you have already decoded your image. - pub fn from_pixels( + pub fn from_rgba( width: u32, height: u32, pixels: impl Into<Bytes>, @@ -57,16 +82,6 @@ impl Handle { } } - /// Creates an image [`Handle`] containing the image data directly. - /// - /// Makes an educated guess about the image format by examining the given data. - /// - /// 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 Into<Bytes>) -> Handle { - Self::Bytes(Id::unique(), bytes.into()) - } - /// Returns the unique identifier of the [`Handle`]. pub fn id(&self) -> Id { match self { @@ -100,10 +115,11 @@ impl std::fmt::Debug for Handle { /// The unique identifier of some [`Handle`] data. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub enum Id { - /// A unique identifier. +pub struct Id(_Id); + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +enum _Id { Unique(u64), - /// A hash identifier. Hash(u64), } @@ -113,7 +129,7 @@ impl Id { static NEXT_ID: AtomicU64 = AtomicU64::new(0); - Self::Unique(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed)) + Self(_Id::Unique(NEXT_ID.fetch_add(1, atomic::Ordering::Relaxed))) } fn path(path: impl AsRef<Path>) -> Self { @@ -124,7 +140,7 @@ impl Id { hasher.finish() }; - Self::Hash(hash) + Self(_Id::Hash(hash)) } } diff --git a/examples/pokedex/src/main.rs b/examples/pokedex/src/main.rs index 0811c08d..35f23236 100644 --- a/examples/pokedex/src/main.rs +++ b/examples/pokedex/src/main.rs @@ -188,7 +188,7 @@ impl Pokemon { { let bytes = reqwest::get(&url).await?.bytes().await?; - Ok(image::Handle::from_memory(bytes)) + Ok(image::Handle::from_bytes(bytes)) } #[cfg(target_arch = "wasm32")] diff --git a/examples/screenshot/src/main.rs b/examples/screenshot/src/main.rs index d887c41b..5c175ccc 100644 --- a/examples/screenshot/src/main.rs +++ b/examples/screenshot/src/main.rs @@ -109,7 +109,7 @@ impl Example { fn view(&self) -> Element<'_, Message> { let image: Element<Message> = if let Some(screenshot) = &self.screenshot { - image(image::Handle::from_pixels( + image(image::Handle::from_rgba( screenshot.size.width, screenshot.size.height, screenshot.clone(), |