From 59f67b79c7ff50f0bc9a27373d0fa36d1523e08a Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 19 Sep 2022 12:49:36 +0200 Subject: Remove image store --- src/loading/mod.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) (limited to 'src/loading/mod.rs') diff --git a/src/loading/mod.rs b/src/loading/mod.rs index d37dd1fc..ecc1e8d5 100644 --- a/src/loading/mod.rs +++ b/src/loading/mod.rs @@ -8,10 +8,14 @@ mod mem; pub use fs::*; pub use mem::*; +use std::fmt::{self, Debug, Formatter}; use std::io; +use std::ops::Deref; use std::path::Path; +use std::sync::Arc; use crate::font::FontInfo; +use crate::util::Prehashed; /// A hash that identifies a file. /// @@ -29,7 +33,7 @@ pub trait Loader { fn resolve(&self, path: &Path) -> io::Result; /// Load a file from a path. - fn load(&self, path: &Path) -> io::Result>; + fn load(&self, path: &Path) -> io::Result; } /// A loader which serves nothing. @@ -44,7 +48,61 @@ impl Loader for BlankLoader { Err(io::ErrorKind::NotFound.into()) } - fn load(&self, _: &Path) -> io::Result> { + fn load(&self, _: &Path) -> io::Result { Err(io::ErrorKind::NotFound.into()) } } + +/// A shared buffer that is cheap to clone. +#[derive(Clone, Hash, Eq, PartialEq)] +pub struct Buffer(Prehashed>>); + +impl Buffer { + /// Return a view into the buffer. + pub fn as_slice(&self) -> &[u8] { + self + } + + /// Return a copy of the buffer as a vector. + pub fn to_vec(&self) -> Vec { + self.0.to_vec() + } +} + +impl From<&[u8]> for Buffer { + fn from(slice: &[u8]) -> Self { + Self(Prehashed::new(Arc::new(slice.to_vec()))) + } +} + +impl From> for Buffer { + fn from(vec: Vec) -> Self { + Self(Prehashed::new(Arc::new(vec))) + } +} + +impl From>> for Buffer { + fn from(arc: Arc>) -> Self { + Self(Prehashed::new(arc)) + } +} + +impl Deref for Buffer { + type Target = [u8]; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl AsRef<[u8]> for Buffer { + fn as_ref(&self) -> &[u8] { + self + } +} + +impl Debug for Buffer { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.pad("Buffer(..)") + } +} -- cgit v1.2.3