Wrap image Bytes with Arc instead of Data

This commit is contained in:
Héctor Ramón Jiménez 2023-02-17 14:37:15 +01:00
parent d2996f3ed8
commit d7c83080f2
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -9,7 +9,7 @@ use std::sync::Arc;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Handle { pub struct Handle {
id: u64, id: u64,
data: Arc<Data>, data: Data,
} }
impl Handle { impl Handle {
@ -33,7 +33,7 @@ impl Handle {
Self::from_data(Data::Rgba { Self::from_data(Data::Rgba {
width, width,
height, height,
pixels: ImageBytes::new(pixels), pixels: Bytes::new(pixels),
}) })
} }
@ -46,7 +46,7 @@ impl Handle {
pub fn from_memory( pub fn from_memory(
bytes: impl AsRef<[u8]> + Clone + Send + Sync + 'static, bytes: impl AsRef<[u8]> + Clone + Send + Sync + 'static,
) -> Handle { ) -> Handle {
Self::from_data(Data::Bytes(ImageBytes::new(bytes))) Self::from_data(Data::Bytes(Bytes::new(bytes)))
} }
fn from_data(data: Data) -> Handle { fn from_data(data: Data) -> Handle {
@ -55,7 +55,7 @@ impl Handle {
Handle { Handle {
id: hasher.finish(), id: hasher.finish(),
data: Arc::new(data), data,
} }
} }
@ -85,55 +85,38 @@ impl Hash for Handle {
} }
} }
/// Wrapper around raw image data. /// A wrapper around raw image data.
/// ///
/// Behaves like a `&[u8]`. /// It behaves like a `&[u8]`.
pub struct ImageBytes(Box<dyn ImageBytesTrait>); #[derive(Clone)]
pub struct Bytes(Arc<dyn AsRef<[u8]> + Send + Sync + 'static>);
trait ImageBytesTrait: AsRef<[u8]> + Send + Sync + 'static { impl Bytes {
fn clone_boxed(&self) -> Box<dyn ImageBytesTrait>; /// Creates new [`Bytes`] around `data`.
} pub fn new(data: impl AsRef<[u8]> + Clone + Send + Sync + 'static) -> Self {
Self(Arc::new(data))
impl<T: AsRef<[u8]> + Clone + Send + Sync + 'static> ImageBytesTrait for T {
fn clone_boxed(&self) -> Box<dyn ImageBytesTrait> {
Box::new(self.clone())
} }
} }
impl ImageBytes { impl std::fmt::Debug for Bytes {
/// Creates a new `ImageBytes` around `data`.
pub fn new(
data: impl AsRef<[u8]> + Clone + Send + Sync + 'static,
) -> ImageBytes {
Self(Box::new(data))
}
}
impl Clone for ImageBytes {
fn clone(&self) -> Self {
ImageBytes(self.0.clone_boxed())
}
}
impl std::fmt::Debug for ImageBytes {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.as_ref().as_ref().fmt(f) self.0.as_ref().as_ref().fmt(f)
} }
} }
impl std::hash::Hash for ImageBytes { impl std::hash::Hash for Bytes {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.as_ref().as_ref().hash(state); self.0.as_ref().as_ref().hash(state);
} }
} }
impl AsRef<[u8]> for ImageBytes { impl AsRef<[u8]> for Bytes {
fn as_ref(&self) -> &[u8] { fn as_ref(&self) -> &[u8] {
self.0.as_ref().as_ref() self.0.as_ref().as_ref()
} }
} }
impl std::ops::Deref for ImageBytes { impl std::ops::Deref for Bytes {
type Target = [u8]; type Target = [u8];
fn deref(&self) -> &[u8] { fn deref(&self) -> &[u8] {
@ -148,7 +131,7 @@ pub enum Data {
Path(PathBuf), Path(PathBuf),
/// In-memory data /// In-memory data
Bytes(ImageBytes), Bytes(Bytes),
/// Decoded image pixels in RGBA format. /// Decoded image pixels in RGBA format.
Rgba { Rgba {
@ -157,7 +140,7 @@ pub enum Data {
/// The height of the image. /// The height of the image.
height: u32, height: u32,
/// The pixels. /// The pixels.
pixels: ImageBytes, pixels: Bytes,
}, },
} }