moosicbox_app_native_image/
lib.rs

1#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
2#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
3#![allow(clippy::multiple_crate_versions)]
4
5use std::{
6    collections::HashMap,
7    sync::{Arc, LazyLock, RwLock},
8};
9
10use bytes::Bytes;
11
12pub static BYTES: LazyLock<RwLock<HashMap<String, Arc<Bytes>>>> =
13    LazyLock::new(|| RwLock::new(HashMap::new()));
14
15#[macro_export]
16macro_rules! image {
17    ($path:expr $(,)?) => {{
18        let bytes = include_bytes!($path);
19        moosicbox_app_native_image::BYTES.write().unwrap().insert(
20            ($path).to_owned(),
21            std::sync::Arc::new(bytes.to_vec().into()),
22        );
23        $path
24    }};
25}
26
27/// # Panics
28///
29/// * If the `BYTES` `RwLock` is poisoned
30pub fn get_image(path: &str) -> Option<Arc<Bytes>> {
31    BYTES.read().unwrap().get(path).cloned()
32}