ailloli_ui_core/ids/
svg_source.rs

1//! Borrowed and shared SVG byte sources keyed by allocation identity.
2
3use std::hash::{Hash, Hasher};
4use std::sync::Arc;
5
6/// SVG bytes for GPU rasterization.
7///
8/// Equality and hashing use **pointer identity**, not byte content (for cache keys).
9///
10/// # Examples
11///
12/// ```
13/// use std::sync::Arc;
14/// use ailloli_ui_core::SvgSource;
15/// let bytes: Arc<[u8]> = Arc::from(&b"<svg/>"[..]);
16/// assert_eq!(SvgSource::Owned(bytes.clone()), SvgSource::Owned(bytes));
17/// ```
18#[derive(Debug, Clone)]
19pub enum SvgSource {
20    /// Compile-time embedded SVG.
21    Static(&'static [u8]),
22    /// Heap-owned SVG bytes.
23    Owned(Arc<[u8]>),
24    /// SVG stored as UTF-8 string.
25    Str(Arc<str>),
26}
27
28impl SvgSource {
29    /// Returns the allocation address used for equality and cache hashing.
30    ///
31    /// Length and byte content are deliberately excluded. Clones of an owned
32    /// source share a key; separately allocated identical bytes do not.
33    fn identity_key(&self) -> usize {
34        match self {
35            SvgSource::Static(bytes) => bytes.as_ptr().addr(),
36            SvgSource::Owned(bytes) => Arc::as_ptr(bytes).addr(),
37            SvgSource::Str(s) => Arc::as_ptr(s).addr(),
38        }
39    }
40
41    /// Returns raw, unvalidated bytes for an SVG parser.
42    ///
43    /// # Examples
44    ///
45    /// ```
46    /// use ailloli_ui_core::SvgSource;
47    /// assert_eq!(SvgSource::Static(b"<svg/>").as_bytes(), b"<svg/>");
48    /// ```
49    pub fn as_bytes(&self) -> &[u8] {
50        match self {
51            SvgSource::Static(bytes) => bytes,
52            SvgSource::Owned(bytes) => bytes,
53            SvgSource::Str(s) => s.as_bytes(),
54        }
55    }
56}
57
58impl PartialEq for SvgSource {
59    fn eq(&self, other: &Self) -> bool {
60        self.identity_key() == other.identity_key()
61    }
62}
63
64impl Eq for SvgSource {}
65
66impl Hash for SvgSource {
67    fn hash<H: Hasher>(&self, state: &mut H) {
68        self.identity_key().hash(state);
69    }
70}