Skip to main content

read_fonts/model/font/
blob.rs

1//! Blobs of font bytes.
2
3use alloc::{sync::Arc, vec::Vec};
4use core::ops::Deref;
5
6/// Font data as a blob of bytes.
7#[derive(Clone)]
8pub enum FontBlob {
9    /// A borrowed static reference to a slice of bytes.
10    Static(&'static [u8]),
11    /// An `Arc` containing anything that can be viewed as a contiguous slice
12    /// of bytes. Typically a `Vec` or a memory mapped buffer.
13    Shared(Arc<dyn AsRef<[u8]> + Send + Sync>),
14}
15
16impl AsRef<[u8]> for FontBlob {
17    fn as_ref(&self) -> &[u8] {
18        match self {
19            Self::Static(bytes) => bytes,
20            Self::Shared(arc) => arc.as_ref().as_ref(),
21        }
22    }
23}
24
25impl Deref for FontBlob {
26    type Target = [u8];
27
28    fn deref(&self) -> &Self::Target {
29        self.as_ref()
30    }
31}
32
33impl From<&'static [u8]> for FontBlob {
34    fn from(value: &'static [u8]) -> Self {
35        Self::Static(value)
36    }
37}
38
39impl From<Arc<dyn AsRef<[u8]> + Send + Sync>> for FontBlob {
40    fn from(value: Arc<dyn AsRef<[u8]> + Send + Sync>) -> Self {
41        Self::Shared(value)
42    }
43}
44
45impl From<Vec<u8>> for FontBlob {
46    fn from(value: Vec<u8>) -> Self {
47        Self::Shared(Arc::new(value))
48    }
49}