Wrap Screenshot::bytes in an Arc and implement AsRef<[u8]>

This commit is contained in:
Héctor Ramón Jiménez 2023-06-06 16:14:42 +02:00
parent 7adfaa88a6
commit 5324928044
No known key found for this signature in database
GPG key ID: 140CC052C94F138E
2 changed files with 16 additions and 5 deletions

View file

@ -133,7 +133,7 @@ impl Application for Example {
image(image::Handle::from_pixels(
screenshot.size.width,
screenshot.size.height,
screenshot.bytes.clone(),
screenshot.clone(),
))
.content_fit(ContentFit::ScaleDown)
.width(Length::Fill)

View file

@ -1,5 +1,7 @@
use iced_core::{Rectangle, Size};
use crate::core::{Rectangle, Size};
use std::fmt::{Debug, Formatter};
use std::sync::Arc;
/// Data of a screenshot, captured with `window::screenshot()`.
///
@ -7,7 +9,7 @@ use std::fmt::{Debug, Formatter};
#[derive(Clone)]
pub struct Screenshot {
/// The bytes of the [`Screenshot`].
pub bytes: Vec<u8>,
pub bytes: Arc<Vec<u8>>,
/// The size of the [`Screenshot`].
pub size: Size<u32>,
}
@ -26,7 +28,10 @@ impl Debug for Screenshot {
impl Screenshot {
/// Creates a new [`Screenshot`].
pub fn new(bytes: Vec<u8>, size: Size<u32>) -> Self {
Self { bytes, size }
Self {
bytes: Arc::new(bytes),
size,
}
}
/// Crops a [`Screenshot`] to the provided `region`. This will always be relative to the
@ -62,12 +67,18 @@ impl Screenshot {
);
Ok(Self {
bytes: chopped,
bytes: Arc::new(chopped),
size: Size::new(region.width, region.height),
})
}
}
impl AsRef<[u8]> for Screenshot {
fn as_ref(&self) -> &[u8] {
&self.bytes
}
}
#[derive(Debug, thiserror::Error)]
/// Errors that can occur when cropping a [`Screenshot`].
pub enum CropError {