From 5324928044cba800454b1861eb9999038bc28c2e Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 6 Jun 2023 16:14:42 +0200 Subject: Wrap `Screenshot::bytes` in an `Arc` and implement `AsRef<[u8]>` --- runtime/src/screenshot.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'runtime') diff --git a/runtime/src/screenshot.rs b/runtime/src/screenshot.rs index 527e400f..b88f2c20 100644 --- a/runtime/src/screenshot.rs +++ b/runtime/src/screenshot.rs @@ -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, + pub bytes: Arc>, /// The size of the [`Screenshot`]. pub size: Size, } @@ -26,7 +28,10 @@ impl Debug for Screenshot { impl Screenshot { /// Creates a new [`Screenshot`]. pub fn new(bytes: Vec, size: Size) -> 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 { -- cgit