Restore manganis optimizations (#3195)

* Create a crate for constant serialization of config structs for manganis
* use SerializeConst for the image asset builder
* switch to a declarative macro for assets
* add serializable options for each asset type
* implement the new manganis macro
* optimize assets in the CLI
* Fix assets with dioxus formattable
* reduce fuzzing test limits
* use the new syntax in examples
* fix avif support
* Fix manganis doc tests
* cache asset optimization
* Split out asset and bundled asset
* make hash pointer width independent
* Fix manganis macro docs
* add constvec::is_empty method to fix clippy lint
* remove nasm feature
* simplify test_rsplit_once test so typos passes
* revert example change from clippy fix
* always accept unix paths
* fix windows path seperator
* fix folder asset hash
* Optimize assets in a blocking task
* Fix asset options docs
* Document Asset and BundledAsset
* simplify the linker macro a bit
* add more docs to AssetParser expansion
* add image format helpers
* Split by  both unix and windows path separators and take the smallest one
This commit is contained in:
Evan Almloff authored and GitHub committed 2024-11-26 18:12:24 +01:00
1 parent 31d72db82d
commit f9617c8d33
64 files changed
+6233 -704

No files matched your search

+70
View File
@@ -88,6 +88,76 @@ prettyplease = { workspace = true }
brotli = "6.0.0"
ignore = "0.4.22"
env_logger = { workspace = true }
const-serialize = { workspace = true, features = ["serde"] }
# Image compression/conversion
# - JPEG
mozjpeg = { version = "0.10.7", default-features = false, features = [
"parallel",
] }
# - PNG
imagequant = "4.2.0"
png = "0.17.9"
# Image format/conversion
image = { version = "0.25", features = ["avif"] }
# CSS Minification
lightningcss = "1.0.0-alpha.60"
# Js minification - swc has introduces minor versions with breaking changes in the past so we pin all of their crates
swc = "=0.283.0"
swc_allocator = { version = "=0.1.8", default-features = false }
swc_atoms = { version = "=0.6.7", default-features = false }
swc_cached = { version = "=0.3.20", default-features = false }
swc_common = { version = "=0.37.5", default-features = false }
swc_compiler_base = { version = "=0.19.0", default-features = false }
swc_config = { version = "=0.1.15", default-features = false }
swc_config_macro = { version = "=0.1.4", default-features = false }
swc_ecma_ast = { version = "=0.118.2", default-features = false }
swc_ecma_codegen = { version = "=0.155.1", default-features = false }
swc_ecma_codegen_macros = { version = "=0.7.7", default-features = false }
swc_ecma_compat_bugfixes = { version = "=0.12.0", default-features = false }
swc_ecma_compat_common = { version = "=0.11.0", default-features = false }
swc_ecma_compat_es2015 = { version = "=0.12.0", default-features = false }
swc_ecma_compat_es2016 = { version = "=0.12.0", default-features = false }
swc_ecma_compat_es2017 = { version = "=0.12.0", default-features = false }
swc_ecma_compat_es2018 = { version = "=0.12.0", default-features = false }
swc_ecma_compat_es2019 = { version = "=0.12.0", default-features = false }
swc_ecma_compat_es2020 = { version = "=0.12.0", default-features = false }
swc_ecma_compat_es2021 = { version = "=0.12.0", default-features = false }
swc_ecma_compat_es2022 = { version = "=0.12.0", default-features = false }
swc_ecma_compat_es3 = { version = "=0.12.0", default-features = false }
swc_ecma_ext_transforms = { version = "=0.120.0", default-features = false }
swc_ecma_lints = { version = "=0.100.0", default-features = false }
swc_ecma_loader = { version = "=0.49.1", default-features = false }
swc_ecma_minifier = { version = "=0.204.0", default-features = false }
swc_ecma_parser = { version = "=0.149.1", default-features = false }
swc_ecma_preset_env = { version = "=0.217.0", default-features = false, features = [
"serde",
] }
swc_ecma_transforms = { version = "=0.239.0", default-features = false }
swc_ecma_transforms_base = { version = "=0.145.0", default-features = false }
swc_ecma_transforms_classes = { version = "=0.134.0", default-features = false }
swc_ecma_transforms_compat = { version = "=0.171.0", default-features = false }
swc_ecma_transforms_macros = { version = "=0.5.5", default-features = false }
swc_ecma_transforms_module = { version = "=0.190.0", default-features = false }
swc_ecma_transforms_optimization = { version = "=0.208.0", default-features = false }
swc_ecma_transforms_proposal = { version = "=0.178.0", default-features = false }
swc_ecma_transforms_react = { version = "=0.191.0", default-features = false }
swc_ecma_transforms_typescript = { version = "=0.198.1", default-features = false }
swc_ecma_usage_analyzer = { version = "=0.30.3", default-features = false }
swc_ecma_utils = { version = "=0.134.2", default-features = false }
swc_ecma_visit = { version = "=0.104.8", default-features = false }
swc_eq_ignore_macros = { version = "=0.1.4", default-features = false }
swc_error_reporters = { version = "=0.21.0", default-features = false }
swc_fast_graph = { version = "=0.25.0", default-features = false }
swc_macros_common = { version = "=0.3.13", default-features = false }
swc_node_comments = { version = "=0.24.0", default-features = false }
swc_timer = { version = "=0.25.0", default-features = false }
swc_trace_macro = { version = "=0.1.3", default-features = false }
swc_transform_common = { version = "=0.1.1", default-features = false }
swc_typescript = { version = "=0.5.0", default-features = false }
swc_visit = { version = "=0.6.2", default-features = false }
tracing-subscriber = { version = "0.3.18", features = ["std", "env-filter", "json"] }
console-subscriber = { version = "0.3.0", optional = true }
+42
View File
@@ -0,0 +1,42 @@
use std::path::Path;
use anyhow::Context;
use lightningcss::{
printer::PrinterOptions,
stylesheet::{MinifyOptions, ParserOptions, StyleSheet},
};
use manganis_core::CssAssetOptions;
pub(crate) fn process_css(
css_options: &CssAssetOptions,
source: &Path,
output_path: &Path,
) -> anyhow::Result<()> {
let css = std::fs::read_to_string(source)?;
let css = if css_options.minified() {
minify_css(&css)
} else {
css
};
std::fs::write(output_path, css).with_context(|| {
format!(
"Failed to write css to output location: {}",
output_path.display()
)
})?;
Ok(())
}
pub(crate) fn minify_css(css: &str) -> String {
let mut stylesheet = StyleSheet::parse(css, ParserOptions::default()).unwrap();
stylesheet.minify(MinifyOptions::default()).unwrap();
let printer = PrinterOptions {
minify: true,
..Default::default()
};
let res = stylesheet.to_css(printer).unwrap();
res.code
}
+75
View File
@@ -0,0 +1,75 @@
use anyhow::Context;
use manganis_core::{AssetOptions, CssAssetOptions, ImageAssetOptions, JsAssetOptions};
use std::path::Path;
use super::{
css::process_css, folder::process_folder, image::process_image, js::process_js,
json::process_json,
};
/// Process a specific file asset with the given options reading from the source and writing to the output path
pub(crate) fn process_file_to(
options: &AssetOptions,
source: &Path,
output_path: &Path,
) -> anyhow::Result<()> {
// If the file already exists, then we must have a file with the same hash
// already. The hash has the file contents and options, so if we find a file
// with the same hash, we probably already created this file in the past
if output_path.exists() {
return Ok(());
}
if let Some(parent) = output_path.parent() {
if !parent.exists() {
std::fs::create_dir_all(parent)?;
}
}
match options {
AssetOptions::Unknown => match source.extension().map(|e| e.to_string_lossy()).as_deref() {
Some("css") => {
process_css(&CssAssetOptions::new(), source, output_path)?;
}
Some("js") => {
process_js(&JsAssetOptions::new(), source, output_path)?;
}
Some("json") => {
process_json(source, output_path)?;
}
Some("jpg" | "jpeg" | "png" | "webp" | "avif") => {
process_image(&ImageAssetOptions::new(), source, output_path)?;
}
None if source.is_dir() => {
process_folder(source, output_path)?;
}
Some(_) | None => {
let source_file = std::fs::File::open(source)?;
let mut reader = std::io::BufReader::new(source_file);
let output_file = std::fs::File::create(output_path)?;
let mut writer = std::io::BufWriter::new(output_file);
std::io::copy(&mut reader, &mut writer).with_context(|| {
format!(
"Failed to write file to output location: {}",
output_path.display()
)
})?;
}
},
AssetOptions::Css(options) => {
process_css(options, source, output_path)?;
}
AssetOptions::Js(options) => {
process_js(options, source, output_path)?;
}
AssetOptions::Image(options) => {
process_image(options, source, output_path)?;
}
AssetOptions::Folder(_) => {
process_folder(source, output_path)?;
}
_ => {
tracing::warn!("Unknown asset options: {:?}", options);
}
}
Ok(())
}
+41
View File
@@ -0,0 +1,41 @@
use std::path::Path;
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use super::file::process_file_to;
/// Process a folder, optimizing and copying all assets into the output folder
pub fn process_folder(source: &Path, output_folder: &Path) -> anyhow::Result<()> {
// Create the folder
std::fs::create_dir_all(output_folder)?;
// Then optimize children
let files: Vec<_> = std::fs::read_dir(source)
.into_iter()
.flatten()
.flatten()
.collect();
files.par_iter().try_for_each(|file| {
let file = file.path();
let metadata = file.metadata()?;
let output_path = output_folder.join(file.strip_prefix(source)?);
if metadata.is_dir() {
process_folder(&file, &output_path)
} else {
process_file_minimal(&file, &output_path)
}
})?;
Ok(())
}
/// Optimize a file without changing any of its contents significantly (e.g. by changing the extension)
fn process_file_minimal(input_path: &Path, output_path: &Path) -> anyhow::Result<()> {
process_file_to(
&manganis_core::AssetOptions::Unknown,
input_path,
output_path,
)?;
Ok(())
}
+23
View File
@@ -0,0 +1,23 @@
use image::{DynamicImage, EncodableLayout};
use std::{
io::{BufWriter, Write},
path::Path,
};
pub(crate) fn compress_jpg(image: DynamicImage, output_location: &Path) -> anyhow::Result<()> {
let mut comp = mozjpeg::Compress::new(mozjpeg::ColorSpace::JCS_EXT_RGBX);
let width = image.width() as usize;
let height = image.height() as usize;
comp.set_size(width, height);
let mut comp = comp.start_compress(Vec::new())?; // any io::Write will work
comp.write_scanlines(image.to_rgba8().as_bytes())?;
let jpeg_bytes = comp.finish()?;
let file = std::fs::File::create(output_location)?;
let w = &mut BufWriter::new(file);
w.write_all(&jpeg_bytes)?;
Ok(())
}
+62
View File
@@ -0,0 +1,62 @@
use std::path::Path;
use anyhow::Context;
use jpg::compress_jpg;
use manganis_core::{ImageAssetOptions, ImageFormat, ImageSize};
use png::compress_png;
mod jpg;
mod png;
pub(crate) fn process_image(
image_options: &ImageAssetOptions,
source: &Path,
output_path: &Path,
) -> anyhow::Result<()> {
let mut image = image::ImageReader::new(std::io::Cursor::new(&*std::fs::read(source)?))
.with_guessed_format()?
.decode();
if let Ok(image) = &mut image {
if let ImageSize::Manual { width, height } = image_options.size() {
*image = image.resize_exact(width, height, image::imageops::FilterType::Lanczos3);
}
}
match (image, image_options.format()) {
(image, ImageFormat::Png) => {
compress_png(image?, output_path);
}
(image, ImageFormat::Jpg) => {
compress_jpg(image?, output_path)?;
}
(Ok(image), ImageFormat::Avif) => {
if let Err(error) = image.save(output_path) {
tracing::error!("Failed to save avif image: {} with path {}. You must have the avif feature enabled to use avif assets", error, output_path.display());
}
}
(Ok(image), ImageFormat::Webp) => {
if let Err(err) = image.save(output_path) {
tracing::error!("Failed to save webp image: {}. You must have the avif feature enabled to use webp assets", err);
}
}
(Ok(image), _) => {
image.save(output_path)?;
}
// If we can't decode the image or it is of an unknown type, we just copy the file
_ => {
let source_file = std::fs::File::open(source)?;
let mut reader = std::io::BufReader::new(source_file);
let output_file = std::fs::File::create(output_path)?;
let mut writer = std::io::BufWriter::new(output_file);
std::io::copy(&mut reader, &mut writer).with_context(|| {
format!(
"Failed to write image to output location: {}",
output_path.display()
)
})?;
}
}
Ok(())
}
+52
View File
@@ -0,0 +1,52 @@
use std::{io::BufWriter, path::Path};
use image::DynamicImage;
pub(crate) fn compress_png(image: DynamicImage, output_location: &Path) {
// Image loading/saving is outside scope of this library
let width = image.width() as usize;
let height = image.height() as usize;
let bitmap: Vec<_> = image
.into_rgba8()
.pixels()
.map(|px| imagequant::RGBA::new(px[0], px[1], px[2], px[3]))
.collect();
// Configure the library
let mut liq = imagequant::new();
liq.set_speed(5).unwrap();
liq.set_quality(0, 99).unwrap();
// Describe the bitmap
let mut img = liq.new_image(&bitmap[..], width, height, 0.0).unwrap();
// The magic happens in quantize()
let mut res = match liq.quantize(&mut img) {
Ok(res) => res,
Err(err) => panic!("Quantization failed, because: {err:?}"),
};
let (palette, pixels) = res.remapped(&mut img).unwrap();
let file = std::fs::File::create(output_location).unwrap();
let w = &mut BufWriter::new(file);
let mut encoder = png::Encoder::new(w, width as u32, height as u32);
encoder.set_color(png::ColorType::Rgba);
let mut flattened_palette = Vec::new();
let mut alpha_palette = Vec::new();
for px in palette {
flattened_palette.push(px.r);
flattened_palette.push(px.g);
flattened_palette.push(px.b);
alpha_palette.push(px.a);
}
encoder.set_palette(flattened_palette);
encoder.set_trns(alpha_palette);
encoder.set_depth(png::BitDepth::Eight);
encoder.set_color(png::ColorType::Indexed);
encoder.set_compression(png::Compression::Best);
let mut writer = encoder.write_header().unwrap();
writer.write_image_data(&pixels).unwrap();
writer.finish().unwrap();
}
+69
View File
@@ -0,0 +1,69 @@
use std::io::Read;
use std::path::Path;
use std::sync::Arc;
use anyhow::Context;
use manganis_core::JsAssetOptions;
use swc::{config::JsMinifyOptions, try_with_handler, BoolOrDataConfig};
use swc_common::{sync::Lrc, FileName};
use swc_common::{SourceMap, GLOBALS};
pub(crate) fn minify_js(source: &Path) -> anyhow::Result<String> {
let mut source_file = std::fs::File::open(source)?;
let cm = Arc::new(SourceMap::default());
let mut js = String::new();
source_file.read_to_string(&mut js)?;
let c = swc::Compiler::new(cm.clone());
let output = GLOBALS
.set(&Default::default(), || {
try_with_handler(cm.clone(), Default::default(), |handler| {
let filename = Lrc::new(FileName::Real(source.to_path_buf()));
let fm = cm.new_source_file(filename, js.to_string());
c.minify(
fm,
handler,
&JsMinifyOptions {
compress: BoolOrDataConfig::from_bool(true),
mangle: BoolOrDataConfig::from_bool(true),
..Default::default()
},
)
.context("failed to minify javascript")
})
})
.map(|output| output.code);
match output {
Ok(output) => Ok(output),
Err(err) => {
tracing::error!("Failed to minify javascript: {}", err);
Ok(js)
}
}
}
pub(crate) fn process_js(
js_options: &JsAssetOptions,
source: &Path,
output_path: &Path,
) -> anyhow::Result<()> {
let js = if js_options.minified() {
minify_js(source)?
} else {
let mut source_file = std::fs::File::open(source)?;
let mut source = String::new();
source_file.read_to_string(&mut source)?;
source
};
std::fs::write(output_path, js).with_context(|| {
format!(
"Failed to write js to output location: {}",
output_path.display()
)
})?;
Ok(())
}
+33
View File
@@ -0,0 +1,33 @@
use std::{io::Read, path::Path};
use anyhow::Context;
pub(crate) fn minify_json(source: &str) -> anyhow::Result<String> {
// First try to parse the json
let json: serde_json::Value = serde_json::from_str(source)?;
// Then print it in a minified format
let json = serde_json::to_string(&json)?;
Ok(json)
}
pub(crate) fn process_json(source: &Path, output_path: &Path) -> anyhow::Result<()> {
let mut source_file = std::fs::File::open(source)?;
let mut source = String::new();
source_file.read_to_string(&mut source)?;
let json = match minify_json(&source) {
Ok(json) => json,
Err(err) => {
tracing::error!("Failed to minify json: {}", err);
source
}
};
std::fs::write(output_path, json).with_context(|| {
format!(
"Failed to write json to output location: {}",
output_path.display()
)
})?;
Ok(())
}
@@ -1,24 +1,34 @@
use anyhow::Context;
use manganis_core::{LinkSection, ResourceAsset};
use manganis_core::linker::LinkSection;
use manganis_core::BundledAsset;
use object::{read::archive::ArchiveFile, File as ObjectFile, Object, ObjectSection};
use serde::{Deserialize, Serialize};
use std::path::Path;
use std::{collections::HashMap, path::PathBuf};
mod css;
mod file;
mod folder;
mod image;
mod js;
mod json;
pub(crate) use file::process_file_to;
/// A manifest of all assets collected from dependencies
///
/// This will be filled in primarily by incremental compilation artifacts.
#[derive(Debug, PartialEq, Default, Clone, Serialize, Deserialize)]
pub(crate) struct AssetManifest {
/// Map of bundled asset name to the asset itself
pub(crate) assets: HashMap<PathBuf, ResourceAsset>,
pub(crate) assets: HashMap<PathBuf, BundledAsset>,
}
impl AssetManifest {
#[allow(dead_code)]
pub(crate) fn load_from_file(path: &Path) -> anyhow::Result<Self> {
let src = std::fs::read_to_string(path)
.context("Failed to read asset manifest from filesystem")?;
let src = std::fs::read_to_string(path)?;
serde_json::from_str(&src)
.with_context(|| format!("Failed to parse asset manifest from {path:?}\n{src}"))
}
@@ -44,7 +54,7 @@ impl AssetManifest {
Ok(())
}
/// Fill this manifest from an rlib / ar file that contains many object files and their entryies
/// Fill this manifest from an rlib / ar file that contains many object files and their entries
fn add_from_archive_file(&mut self, archive: &ArchiveFile, data: &[u8]) -> object::Result<()> {
// Look through each archive member for object files.
// Read the archive member's binary data (we know it's an object file)
@@ -84,17 +94,13 @@ impl AssetManifest {
.uncompressed_data()
.context("Could not read uncompressed data from object file")?;
let as_str = std::str::from_utf8(&bytes)
.context("object file contained non utf8 encoding")?
.chars()
.filter(|c| !c.is_control())
.collect::<String>();
let assets = serde_json::Deserializer::from_str(&as_str).into_iter::<ResourceAsset>();
for as_resource in assets.flatten() {
// Some platforms (e.g. macOS) start the manganis section with a null byte, we need to filter that out before we deserialize the JSON
let mut buffer = const_serialize::ConstReadBuffer::new(&bytes);
while let Some((remaining_buffer, asset)) =
const_serialize::deserialize_const!(BundledAsset, buffer)
{
self.assets
.insert(as_resource.absolute.clone(), as_resource);
.insert(asset.absolute_source_path().into(), asset);
buffer = remaining_buffer;
}
}
+74 -32
View File
@@ -1,12 +1,14 @@
use crate::assets::process_file_to;
use crate::Result;
use crate::{assets::AssetManifest, TraceSrc};
use crate::{BuildRequest, Platform};
use anyhow::Context;
use manganis_core::AssetOptions;
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
use std::{
fs::create_dir_all,
path::{Path, PathBuf},
};
use std::collections::HashSet;
use std::future::Future;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::{sync::atomic::AtomicUsize, time::Duration};
use tokio::process::Command;
use wasm_bindgen_cli_support::Bindgen;
@@ -377,21 +379,54 @@ impl AppBundle {
let asset_dir = self.build.asset_dir();
// First, clear the asset dir
// todo(jon): cache the asset dir, removing old files and only copying new ones that changed since the last build
_ = std::fs::remove_dir_all(&asset_dir);
_ = create_dir_all(&asset_dir);
// First, clear the asset dir of any files that don't exist in the new manifest
_ = tokio::fs::create_dir_all(&asset_dir).await;
// Create a set of all the paths that new files will be bundled to
let bundled_output_paths: HashSet<_> = self
.app
.assets
.assets
.values()
.map(|a| asset_dir.join(a.bundled_path()))
.collect();
// one possible implementation of walking a directory only visiting files
fn remove_old_assets<'a>(
path: &'a Path,
bundled_output_paths: &'a HashSet<PathBuf>,
) -> Pin<Box<dyn Future<Output = std::io::Result<()>> + Send + 'a>> {
Box::pin(async move {
// If this asset is in the manifest, we don't need to remove it
if bundled_output_paths.contains(path.canonicalize()?.as_path()) {
return Ok(());
}
// Otherwise, if it is a directory, we need to walk it and remove child files
if path.is_dir() {
for entry in std::fs::read_dir(path)?.flatten() {
let path = entry.path();
remove_old_assets(&path, bundled_output_paths).await?;
}
if path.read_dir()?.next().is_none() {
// If the directory is empty, remove it
tokio::fs::remove_dir(path).await?;
}
} else {
// If it is a file, remove it
tokio::fs::remove_file(path).await?;
}
Ok(())
})
}
remove_old_assets(&asset_dir, &bundled_output_paths).await?;
// todo(jon): we also want to eventually include options for each asset's optimization and compression, which we currently aren't
let mut assets_to_transfer = vec![];
// Queue the bundled assets
for asset in self.app.assets.assets.keys() {
let bundled = self.app.assets.assets.get(asset).unwrap();
let from = bundled.absolute.clone();
let to = asset_dir.join(&bundled.bundled);
for (asset, bundled) in &self.app.assets.assets {
let from = asset.clone();
let to = asset_dir.join(bundled.bundled_path());
tracing::debug!("Copying asset {from:?} to {to:?}");
assets_to_transfer.push((from, to));
assets_to_transfer.push((from, to, *bundled.options()));
}
// And then queue the legacy assets
@@ -399,35 +434,42 @@ impl AppBundle {
for from in self.build.krate.legacy_asset_dir_files() {
let to = asset_dir.join(from.file_name().unwrap());
tracing::debug!("Copying legacy asset {from:?} to {to:?}");
assets_to_transfer.push((from, to));
assets_to_transfer.push((from, to, AssetOptions::Unknown));
}
let asset_count = assets_to_transfer.len();
let assets_finished = AtomicUsize::new(0);
// Parallel Copy over the assets and keep track of progress with an atomic counter
// todo: we want to use the fastfs variant that knows how to parallelize folders, too
assets_to_transfer.par_iter().try_for_each(|(from, to)| {
tracing::trace!(
"Starting asset copy {current}/{asset_count} from {from:?}",
current = assets_finished.fetch_add(0, std::sync::atomic::Ordering::SeqCst),
);
let progress = self.build.progress.clone();
// Optimizing assets is expensive and blocking, so we do it in a tokio spawn blocking task
tokio::task::spawn_blocking(move || {
assets_to_transfer
.par_iter()
.try_for_each(|(from, to, options)| {
tracing::trace!(
"Starting asset copy {current}/{asset_count} from {from:?}",
current = assets_finished.fetch_add(0, std::sync::atomic::Ordering::SeqCst),
);
// todo(jon): implement optimize + pre_compress on the asset type
let res = crate::fastfs::copy_asset(from, to);
let res = process_file_to(options, from, to);
if let Err(err) = res.as_ref() {
tracing::error!("Failed to copy asset {from:?}: {err}");
}
if let Err(err) = res.as_ref() {
tracing::error!("Failed to copy asset {from:?}: {err}");
}
self.build.status_copied_asset(
assets_finished.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1,
asset_count,
from.clone(),
);
BuildRequest::status_copied_asset(
&progress,
assets_finished.fetch_add(1, std::sync::atomic::Ordering::SeqCst) + 1,
asset_count,
from.to_path_buf(),
);
res.map(|_| ())
})?;
res.map(|_| ())
})
})
.await
.map_err(|e| anyhow::anyhow!("A task failed while trying to copy assets: {e}"))??;
Ok(())
}
+7 -2
View File
@@ -59,8 +59,13 @@ impl BuildRequest {
});
}
pub(crate) fn status_copied_asset(&self, current: usize, total: usize, path: PathBuf) {
_ = self.progress.unbounded_send(BuildUpdate::Progress {
pub(crate) fn status_copied_asset(
progress: &UnboundedSender<BuildUpdate>,
current: usize,
total: usize,
path: PathBuf,
) {
_ = progress.unbounded_send(BuildUpdate::Progress {
stage: BuildStage::CopyingAssets {
current,
total,
+1 -1
View File
@@ -558,7 +558,7 @@ impl BuildRequest {
static INITIALIZED: OnceCell<Result<()>> = OnceCell::new();
let success = INITIALIZED.get_or_init(|| {
_ = remove_dir_all(self.root_dir());
_ = remove_dir_all(self.exe_dir());
create_dir_all(self.root_dir())?;
create_dir_all(self.exe_dir())?;
+2 -2
View File
@@ -181,8 +181,8 @@ impl AppHandle {
// The asset might've been renamed thanks to the manifest, let's attempt to reload that too
if let Some(resource) = self.app.app.assets.assets.get(changed_file).as_ref() {
let res = std::fs::copy(changed_file, asset_dir.join(&resource.bundled));
bundled_name = Some(PathBuf::from(&resource.bundled));
let res = std::fs::copy(changed_file, asset_dir.join(resource.bundled_path()));
bundled_name = Some(PathBuf::from(resource.bundled_path()));
if let Err(e) = res {
tracing::debug!("Failed to hotreload asset {e}");
}
+18
View File
@@ -0,0 +1,18 @@
[package]
name = "const-serialize-macro"
version = { workspace = true }
authors = ["Evan Almloff"]
edition = "2021"
description = "A macro to derive const serialize"
license = "MIT OR Apache-2.0"
repository = "https://github.com/dioxuslabs/dioxus"
homepage = "https://dioxuslabs.com/learn/0.5/getting_started"
keywords = ["const", "serialize"]
[dependencies]
syn = "2.0"
quote = "1.0"
proc-macro2 = "1.0.86"
[lib]
proc-macro = true
+207
View File
@@ -0,0 +1,207 @@
use proc_macro::TokenStream;
use quote::{quote, ToTokens};
use syn::{parse_macro_input, DeriveInput, LitInt};
use syn::{parse_quote, Generics, WhereClause, WherePredicate};
fn add_bounds(where_clause: &mut Option<WhereClause>, generics: &Generics) {
let bounds = generics.params.iter().filter_map(|param| match param {
syn::GenericParam::Type(ty) => {
Some::<WherePredicate>(parse_quote! { #ty: const_serialize::SerializeConst, })
}
syn::GenericParam::Lifetime(_) => None,
syn::GenericParam::Const(_) => None,
});
if let Some(clause) = where_clause {
clause.predicates.extend(bounds);
} else {
*where_clause = Some(parse_quote! { where #(#bounds)* });
}
}
/// Derive the const serialize trait for a struct
#[proc_macro_derive(SerializeConst)]
pub fn derive_parse(input: TokenStream) -> TokenStream {
// Parse the input tokens into a syntax tree
let input = parse_macro_input!(input as DeriveInput);
match input.data {
syn::Data::Struct(data) => match data.fields {
syn::Fields::Unnamed(_) | syn::Fields::Named(_) => {
let ty = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut where_clause = where_clause.cloned();
add_bounds(&mut where_clause, &input.generics);
let field_names = data.fields.iter().enumerate().map(|(i, field)| {
field
.ident
.as_ref()
.map(|ident| ident.to_token_stream())
.unwrap_or_else(|| {
LitInt::new(&i.to_string(), proc_macro2::Span::call_site())
.into_token_stream()
})
});
let field_types = data.fields.iter().map(|field| &field.ty);
quote! {
unsafe impl #impl_generics const_serialize::SerializeConst for #ty #ty_generics #where_clause {
const MEMORY_LAYOUT: const_serialize::Layout = const_serialize::Layout::Struct(const_serialize::StructLayout::new(
std::mem::size_of::<Self>(),
&[#(
const_serialize::StructFieldLayout::new(
std::mem::offset_of!(#ty, #field_names),
<#field_types as const_serialize::SerializeConst>::MEMORY_LAYOUT,
),
)*],
));
}
}.into()
}
syn::Fields::Unit => {
let ty = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut where_clause = where_clause.cloned();
add_bounds(&mut where_clause, &input.generics);
quote! {
unsafe impl #impl_generics const_serialize::SerializeConst for #ty #ty_generics #where_clause {
const MEMORY_LAYOUT: const_serialize::Layout = const_serialize::Layout::Struct(const_serialize::StructLayout::new(
std::mem::size_of::<Self>(),
&[],
));
}
}.into()
}
},
syn::Data::Enum(data) => match data.variants.len() {
0 => syn::Error::new(input.ident.span(), "Enums must have at least one variant")
.to_compile_error()
.into(),
1.. => {
let mut repr_c = false;
let mut discriminant_size = None;
for attr in &input.attrs {
if attr.path().is_ident("repr") {
if let Err(err) = attr.parse_nested_meta(|meta| {
// #[repr(C)]
if meta.path.is_ident("C") {
repr_c = true;
return Ok(());
}
// #[repr(u8)]
if meta.path.is_ident("u8") {
discriminant_size = Some(1);
return Ok(());
}
// #[repr(u16)]
if meta.path.is_ident("u16") {
discriminant_size = Some(2);
return Ok(());
}
// #[repr(u32)]
if meta.path.is_ident("u32") {
discriminant_size = Some(3);
return Ok(());
}
// #[repr(u64)]
if meta.path.is_ident("u64") {
discriminant_size = Some(4);
return Ok(());
}
Err(meta.error("unrecognized repr"))
}) {
return err.to_compile_error().into();
}
}
}
let variants_have_fields = data
.variants
.iter()
.any(|variant| !variant.fields.is_empty());
if !repr_c && variants_have_fields {
return syn::Error::new(input.ident.span(), "Enums must be repr(C, u*)")
.to_compile_error()
.into();
}
if discriminant_size.is_none() {
return syn::Error::new(input.ident.span(), "Enums must be repr(u*)")
.to_compile_error()
.into();
}
let ty = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let mut where_clause = where_clause.cloned();
add_bounds(&mut where_clause, &input.generics);
let mut last_discriminant = None;
let variants = data.variants.iter().map(|variant| {
let discriminant = variant
.discriminant
.as_ref()
.map(|(_, discriminant)| discriminant.to_token_stream())
.unwrap_or_else(|| match &last_discriminant {
Some(discriminant) => quote! { #discriminant + 1 },
None => {
quote! { 0 }
}
});
last_discriminant = Some(discriminant.clone());
let field_names = variant.fields.iter().enumerate().map(|(i, field)| {
field
.ident
.clone()
.unwrap_or_else(|| quote::format_ident!("__field_{}", i))
});
let field_types = variant.fields.iter().map(|field| &field.ty);
let generics = &input.generics;
quote! {
{
#[allow(unused)]
#[derive(const_serialize::SerializeConst)]
#[repr(C)]
struct VariantStruct #generics {
#(
#field_names: #field_types,
)*
}
const_serialize::EnumVariant::new(
#discriminant as u32,
match VariantStruct::MEMORY_LAYOUT {
const_serialize::Layout::Struct(layout) => layout,
_ => panic!("VariantStruct::MEMORY_LAYOUT must be a struct"),
},
std::mem::align_of::<VariantStruct>(),
)
}
}
});
quote! {
unsafe impl #impl_generics const_serialize::SerializeConst for #ty #ty_generics #where_clause {
const MEMORY_LAYOUT: const_serialize::Layout = const_serialize::Layout::Enum(const_serialize::EnumLayout::new(
std::mem::size_of::<Self>(),
const_serialize::PrimitiveLayout::new(
#discriminant_size as usize,
),
{
const DATA: &'static [const_serialize::EnumVariant] = &[
#(
#variants,
)*
];
DATA
},
));
}
}.into()
}
},
_ => syn::Error::new(input.ident.span(), "Only structs and enums are supported")
.to_compile_error()
.into(),
}
}
+1
View File
@@ -0,0 +1 @@
target
+22
View File
@@ -0,0 +1,22 @@
[package]
name = "const-serialize"
version = { workspace = true }
authors = ["Evan Almloff"]
edition = "2021"
description = "A serialization framework that works in const contexts"
license = "MIT OR Apache-2.0"
repository = "https://github.com/dioxuslabs/dioxus"
homepage = "https://dioxuslabs.com/learn/0.5/getting_started"
keywords = ["const", "serialize"]
[dependencies]
const-serialize-macro = { workspace = true }
serde = { workspace = true, features = ["derive"], optional = true }
[dev-dependencies]
const-serialize = { path = "." }
rand.workspace = true
[features]
test-big-endian = []
serde = ["dep:serde"]
+57
View File
@@ -0,0 +1,57 @@
A rust serialization library that works in const with complex(ish) types like enums, nested structs and arrays. Const rust does not have an allocator, so this library cannot work in a cross architecture environment with Vecs, slices or strings.
```rust
use const_serialize::{deserialize_const, serialize_const, serialize_eq, ConstVec, SerializeConst};
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
struct Struct {
a: u32,
b: u8,
c: u32,
d: Enum,
}
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
#[repr(C, u8)]
enum Enum {
A { one: u32, two: u16 },
B { one: u8, two: u16 } = 15,
}
const {
let data = [Struct {
a: 0x11111111,
b: 0x22,
c: 0x33333333,
d: Enum::A {
one: 0x44444444,
two: 0x5555,
},
}; 3];
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
let buf = buf.read();
let (buf, deserialized) = match deserialize_const!([Struct; 3], buf) {
Some(data) => data,
None => panic!("data mismatch"),
};
if !serialize_eq(&data, &deserialized) {
panic!("data mismatch");
}
}
```
## How it works
`const-serialize` relies heavily on well defined layouts for the types you want to serialize. The serialization format is the linear sequence of unaligned bytes stored in the order of the fields, items or variants of the type. Numbers are stored in little endian order.
In order to support complex nested types, serialization is done using a trait. Since functions in traits cannot be const, `const-serialize` uses a macro to generate constant associated items that describe the memory layout of the type. That layout is then used to read all of the bytes in the type into the serialized buffer.
The deserialization is done in a similar way, but the layout is used to write the bytes from the serialized buffer into the type.
The rust [nomicon](https://doc.rust-lang.org/nomicon/data.html) defines the memory layout of different types. It is used as a reference for the layout of the types implemented in `const-serialize`.
## Limitations
- Only constant sized types are supported. This means that you can't serialize a type like `Vec<T>`. These types are difficult to create in const contexts in general
- Only types with a well defined memory layout are supported (see https://github.com/rust-lang/rfcs/pull/3727 and https://onevariable.com/blog/pods-from-scratch). `repr(Rust)` enums don't have a well defined layout, so they are not supported. `repr(C, u8)` enums can be used instead
- Const rust does not support mutable references or points, so this crate leans heavily on function data structures for data processing.
@@ -0,0 +1,33 @@
/// A buffer that can be read from at compile time. This is very similar to [Cursor](std::io::Cursor) but is
/// designed to be used in const contexts.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ConstReadBuffer<'a> {
location: usize,
memory: &'a [u8],
}
impl<'a> ConstReadBuffer<'a> {
/// Create a new buffer from a byte slice
pub const fn new(memory: &'a [u8]) -> Self {
Self {
location: 0,
memory,
}
}
/// Get the next byte from the buffer. Returns `None` if the buffer is empty.
/// This will return the new version of the buffer with the first byte removed.
pub const fn get(mut self) -> Option<(Self, u8)> {
if self.location >= self.memory.len() {
return None;
}
let value = self.memory[self.location];
self.location += 1;
Some((self, value))
}
/// Get a reference to the underlying byte slice
pub const fn as_ref(&self) -> &[u8] {
self.memory
}
}
+422
View File
@@ -0,0 +1,422 @@
#![allow(dead_code)]
use std::{fmt::Debug, hash::Hash, mem::MaybeUninit};
use crate::ConstReadBuffer;
const DEFAULT_MAX_SIZE: usize = 2usize.pow(10);
/// [`ConstVec`] is a version of [`Vec`] that is usable in const contexts. It has
/// a fixed maximum size, but it can can grow and shrink within that size limit
/// as needed.
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// // Methods that mutate the vector will return a new vector
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// const TWO: ConstVec<u8> = ONE.push(2);
/// const THREE: ConstVec<u8> = TWO.push(3);
/// const FOUR: ConstVec<u8> = THREE.push(4);
/// // If a value is also returned, that will be placed in a tuple in the return value
/// // along with the new vector
/// const POPPED: (ConstVec<u8>, Option<u8>) = FOUR.pop();
/// assert_eq!(POPPED.0, THREE);
/// assert_eq!(POPPED.1.unwrap(), 4);
/// ```
pub struct ConstVec<T, const MAX_SIZE: usize = DEFAULT_MAX_SIZE> {
memory: [MaybeUninit<T>; MAX_SIZE],
len: u32,
}
impl<T: Clone, const MAX_SIZE: usize> Clone for ConstVec<T, MAX_SIZE> {
fn clone(&self) -> Self {
let mut cloned = Self::new_with_max_size();
for i in 0..self.len as usize {
cloned = cloned.push(self.get(i).unwrap().clone());
}
cloned
}
}
impl<T: Copy, const MAX_SIZE: usize> Copy for ConstVec<T, MAX_SIZE> {}
impl<T: PartialEq, const MAX_SIZE: usize> PartialEq for ConstVec<T, MAX_SIZE> {
fn eq(&self, other: &Self) -> bool {
self.as_ref() == other.as_ref()
}
}
impl<T: Hash, const MAX_SIZE: usize> Hash for ConstVec<T, MAX_SIZE> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_ref().hash(state)
}
}
impl<T, const MAX_SIZE: usize> Default for ConstVec<T, MAX_SIZE> {
fn default() -> Self {
Self::new_with_max_size()
}
}
impl<T: Debug, const MAX_SIZE: usize> Debug for ConstVec<T, MAX_SIZE> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ConstVec")
.field("len", &self.len)
.field("memory", &self.as_ref())
.finish()
}
}
impl<T> ConstVec<T> {
/// Create a new empty [`ConstVec`]
pub const fn new() -> Self {
Self::new_with_max_size()
}
}
impl<T, const MAX_SIZE: usize> ConstVec<T, MAX_SIZE> {
/// Create a new empty [`ConstVec`] with a custom maximum size
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8, 10> = ConstVec::new_with_max_size();
/// ```
pub const fn new_with_max_size() -> Self {
Self {
memory: [const { MaybeUninit::uninit() }; MAX_SIZE],
len: 0,
}
}
/// Push a value onto the end of the [`ConstVec`]
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// assert_eq!(ONE.as_ref(), &[1]);
/// ```
pub const fn push(mut self, value: T) -> Self {
self.memory[self.len as usize] = MaybeUninit::new(value);
self.len += 1;
self
}
/// Extend the [`ConstVec`] with the contents of a slice
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.extend(&[1, 2, 3]);
/// assert_eq!(ONE.as_ref(), &[1, 2, 3]);
/// ```
pub const fn extend(mut self, other: &[T]) -> Self
where
T: Copy,
{
let mut i = 0;
while i < other.len() {
self = self.push(other[i]);
i += 1;
}
self
}
/// Get a reference to the value at the given index
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// assert_eq!(ONE.get(0), Some(&1));
/// ```
pub const fn get(&self, index: usize) -> Option<&T> {
if index < self.len as usize {
Some(unsafe { &*self.memory[index].as_ptr() })
} else {
None
}
}
/// Get the length of the [`ConstVec`]
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// assert_eq!(ONE.len(), 1);
/// ```
pub const fn len(&self) -> usize {
self.len as usize
}
/// Check if the [`ConstVec`] is empty
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// assert!(EMPTY.is_empty());
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// assert!(!ONE.is_empty());
/// ```
pub const fn is_empty(&self) -> bool {
self.len == 0
}
/// Get a reference to the underlying slice
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// assert_eq!(ONE.as_ref(), &[1]);
/// ```
pub const fn as_ref(&self) -> &[T] {
unsafe {
&*(self.memory.split_at(self.len as usize).0 as *const [MaybeUninit<T>] as *const [T])
}
}
/// Swap the values at the given indices
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// const TWO: ConstVec<u8> = ONE.push(2);
/// const THREE: ConstVec<u8> = TWO.swap(0, 1);
/// assert_eq!(THREE.as_ref(), &[2, 1]);
/// ```
pub const fn swap(mut self, first: usize, second: usize) -> Self
where
T: Copy,
{
assert!(first < self.len as usize);
assert!(second < self.len as usize);
let temp = self.memory[first];
self.memory[first] = self.memory[second];
self.memory[second] = temp;
self
}
/// Pop a value off the end of the [`ConstVec`]
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// const TWO: ConstVec<u8> = ONE.push(2);
/// const THREE: ConstVec<u8> = TWO.push(3);
/// const POPPED: (ConstVec<u8>, Option<u8>) = THREE.pop();
/// assert_eq!(POPPED.0, TWO);
/// assert_eq!(POPPED.1.unwrap(), 3);
/// ```
pub const fn pop(mut self) -> (Self, Option<T>)
where
T: Copy,
{
let value = if self.len > 0 {
self.len -= 1;
let last = self.len as usize;
let last_value = unsafe { self.memory[last].assume_init() };
Some(last_value)
} else {
None
};
(self, value)
}
/// Remove the value at the given index
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// const TWO: ConstVec<u8> = ONE.push(2);
/// const THREE: ConstVec<u8> = TWO.push(3);
/// const REMOVED: (ConstVec<u8>, Option<u8>) = THREE.remove(1);
/// assert_eq!(REMOVED.0.as_ref(), &[1, 3]);
/// assert_eq!(REMOVED.1.unwrap(), 2);
/// ```
pub const fn remove(mut self, index: usize) -> (Self, Option<T>)
where
T: Copy,
{
let value = if index < self.len as usize {
let value = unsafe { self.memory[index].assume_init() };
let mut swap_index = index;
while swap_index + 1 < self.len as usize {
self.memory[swap_index] = self.memory[swap_index + 1];
swap_index += 1;
}
self.len -= 1;
Some(value)
} else {
None
};
(self, value)
}
/// Set the value at the given index
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// const TWO: ConstVec<u8> = ONE.set(0, 2);
/// assert_eq!(TWO.as_ref(), &[2]);
/// ```
pub const fn set(mut self, index: usize, value: T) -> Self {
if index >= self.len as usize {
panic!("Out of bounds")
}
self.memory[index] = MaybeUninit::new(value);
self
}
pub(crate) const fn into_parts(self) -> ([MaybeUninit<T>; MAX_SIZE], usize) {
(self.memory, self.len as usize)
}
/// Split the [`ConstVec`] into two at the given index
///
/// # Example
/// ```rust
/// # use const_serialize::ConstVec;
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// const TWO: ConstVec<u8> = ONE.push(2);
/// const THREE: ConstVec<u8> = TWO.push(3);
/// const SPLIT: (ConstVec<u8>, ConstVec<u8>) = THREE.split_at(1);
/// assert_eq!(SPLIT.0.as_ref(), &[1]);
/// assert_eq!(SPLIT.1.as_ref(), &[2, 3]);
/// ```
pub const fn split_at(&self, index: usize) -> (Self, Self)
where
T: Copy,
{
assert!(index <= self.len as usize);
let slice = self.as_ref();
let (left, right) = slice.split_at(index);
let mut left_vec = Self::new_with_max_size();
let mut i = 0;
while i < left.len() {
left_vec = left_vec.push(left[i]);
i += 1;
}
let mut right_vec = Self::new_with_max_size();
i = 0;
while i < right.len() {
right_vec = right_vec.push(right[i]);
i += 1;
}
(left_vec, right_vec)
}
}
impl<const MAX_SIZE: usize> ConstVec<u8, MAX_SIZE> {
/// Convert the [`ConstVec`] into a [`ConstReadBuffer`](crate::ConstReadBuffer)
///
/// # Example
/// ```rust
/// # use const_serialize::{ConstVec, ConstReadBuffer};
/// const EMPTY: ConstVec<u8> = ConstVec::new();
/// const ONE: ConstVec<u8> = EMPTY.push(1);
/// const TWO: ConstVec<u8> = ONE.push(2);
/// const READ: ConstReadBuffer = TWO.read();
/// ```
pub const fn read(&self) -> ConstReadBuffer<'_> {
ConstReadBuffer::new(self.as_ref())
}
}
#[test]
fn test_const_vec() {
const VEC: ConstVec<u32> = {
let mut vec = ConstVec::new();
vec = vec.push(1234);
vec = vec.push(5678);
vec
};
assert_eq!(VEC.as_ref(), &[1234, 5678]);
let vec = VEC;
let (vec, value) = vec.pop();
assert_eq!(value, Some(5678));
let (vec, value) = vec.pop();
assert_eq!(value, Some(1234));
let (vec, value) = vec.pop();
assert_eq!(value, None);
assert_eq!(vec.as_ref(), &[]);
}
#[test]
fn test_const_vec_len() {
const VEC: ConstVec<u32> = {
let mut vec = ConstVec::new();
vec = vec.push(1234);
vec = vec.push(5678);
vec
};
assert_eq!(VEC.len(), 2);
}
#[test]
fn test_const_vec_get() {
const VEC: ConstVec<u32> = {
let mut vec = ConstVec::new();
vec = vec.push(1234);
vec = vec.push(5678);
vec
};
assert_eq!(VEC.get(0), Some(&1234));
assert_eq!(VEC.get(1), Some(&5678));
assert_eq!(VEC.get(2), None);
}
#[test]
fn test_const_vec_swap() {
const VEC: ConstVec<u32> = {
let mut vec = ConstVec::new();
vec = vec.push(1234);
vec = vec.push(5678);
vec
};
let mut vec = VEC;
assert_eq!(vec.as_ref(), &[1234, 5678]);
vec = vec.swap(0, 1);
assert_eq!(vec.as_ref(), &[5678, 1234]);
vec = vec.swap(0, 1);
assert_eq!(vec.as_ref(), &[1234, 5678]);
}
#[test]
fn test_const_vec_remove() {
const VEC: ConstVec<u32> = {
let mut vec = ConstVec::new();
vec = vec.push(1234);
vec = vec.push(5678);
vec
};
let vec = VEC;
println!("{:?}", vec);
assert_eq!(vec.as_ref(), &[1234, 5678]);
let (vec, value) = vec.remove(0);
assert_eq!(value, Some(1234));
assert_eq!(vec.as_ref(), &[5678]);
let (vec, value) = vec.remove(0);
assert_eq!(value, Some(5678));
assert_eq!(vec.as_ref(), &[]);
}
+924
View File
@@ -0,0 +1,924 @@
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
use std::{char, mem::MaybeUninit};
mod const_buffers;
mod const_vec;
pub use const_buffers::ConstReadBuffer;
pub use const_serialize_macro::SerializeConst;
pub use const_vec::ConstVec;
/// Plain old data for a field. Stores the offset of the field in the struct and the layout of the field.
#[derive(Debug, Copy, Clone)]
pub struct StructFieldLayout {
offset: usize,
layout: Layout,
}
impl StructFieldLayout {
/// Create a new struct field layout
pub const fn new(offset: usize, layout: Layout) -> Self {
Self { offset, layout }
}
}
/// Layout for a struct. The struct layout is just a list of fields with offsets
#[derive(Debug, Copy, Clone)]
pub struct StructLayout {
size: usize,
data: &'static [StructFieldLayout],
}
impl StructLayout {
/// Create a new struct layout
pub const fn new(size: usize, data: &'static [StructFieldLayout]) -> Self {
Self { size, data }
}
}
/// The layout for an enum. The enum layout is just a discriminate size and a tag layout.
#[derive(Debug, Copy, Clone)]
pub struct EnumLayout {
size: usize,
discriminant: PrimitiveLayout,
variants_offset: usize,
variants: &'static [EnumVariant],
}
impl EnumLayout {
/// Create a new enum layout
pub const fn new(
size: usize,
discriminant: PrimitiveLayout,
variants: &'static [EnumVariant],
) -> Self {
let mut max_align = 1;
let mut i = 0;
while i < variants.len() {
let EnumVariant { align, .. } = &variants[i];
if *align > max_align {
max_align = *align;
}
i += 1;
}
let variants_offset = (discriminant.size / max_align) + max_align;
Self {
size,
discriminant,
variants_offset,
variants,
}
}
}
/// The layout for an enum variant. The enum variant layout is just a struct layout with a tag and alignment.
#[derive(Debug, Copy, Clone)]
pub struct EnumVariant {
// Note: tags may not be sequential
tag: u32,
data: StructLayout,
align: usize,
}
impl EnumVariant {
/// Create a new enum variant layout
pub const fn new(tag: u32, data: StructLayout, align: usize) -> Self {
Self { tag, data, align }
}
}
/// The layout for a constant sized array. The array layout is just a length and an item layout.
#[derive(Debug, Copy, Clone)]
pub struct ListLayout {
len: usize,
item_layout: &'static Layout,
}
impl ListLayout {
/// Create a new list layout
pub const fn new(len: usize, item_layout: &'static Layout) -> Self {
Self { len, item_layout }
}
}
/// The layout for a primitive type. The bytes will be reversed if the target is big endian.
#[derive(Debug, Copy, Clone)]
pub struct PrimitiveLayout {
size: usize,
}
impl PrimitiveLayout {
/// Create a new primitive layout
pub const fn new(size: usize) -> Self {
Self { size }
}
}
/// The layout for a type. This layout defines a sequence of locations and reversed or not bytes. These bytes will be copied from during serialization and copied into during deserialization.
#[derive(Debug, Copy, Clone)]
pub enum Layout {
/// An enum layout
Enum(EnumLayout),
/// A struct layout
Struct(StructLayout),
/// A list layout
List(ListLayout),
/// A primitive layout
Primitive(PrimitiveLayout),
}
impl Layout {
/// The size of the type in bytes.
const fn size(&self) -> usize {
match self {
Layout::Enum(layout) => layout.size,
Layout::Struct(layout) => layout.size,
Layout::List(layout) => layout.len * layout.item_layout.size(),
Layout::Primitive(layout) => layout.size,
}
}
}
/// A trait for types that can be serialized and deserialized in const.
///
/// # Safety
/// The layout must accurately describe the memory layout of the type
pub unsafe trait SerializeConst: Sized {
/// The memory layout of the type. This type must have plain old data; no pointers or references.
const MEMORY_LAYOUT: Layout;
/// Assert that the memory layout of the type is the same as the size of the type
const _ASSERT: () = assert!(Self::MEMORY_LAYOUT.size() == std::mem::size_of::<Self>());
}
macro_rules! impl_serialize_const {
($type:ty) => {
unsafe impl SerializeConst for $type {
const MEMORY_LAYOUT: Layout = Layout::Primitive(PrimitiveLayout {
size: std::mem::size_of::<$type>(),
});
}
};
}
impl_serialize_const!(u8);
impl_serialize_const!(u16);
impl_serialize_const!(u32);
impl_serialize_const!(u64);
impl_serialize_const!(i8);
impl_serialize_const!(i16);
impl_serialize_const!(i32);
impl_serialize_const!(i64);
impl_serialize_const!(bool);
impl_serialize_const!(f32);
impl_serialize_const!(f64);
unsafe impl<const N: usize, T: SerializeConst> SerializeConst for [T; N] {
const MEMORY_LAYOUT: Layout = Layout::List(ListLayout {
len: N,
item_layout: &T::MEMORY_LAYOUT,
});
}
macro_rules! impl_serialize_const_tuple {
($($generic:ident: $generic_number:expr),*) => {
impl_serialize_const_tuple!(@impl ($($generic,)*) = $($generic: $generic_number),*);
};
(@impl $inner:ty = $($generic:ident: $generic_number:expr),*) => {
unsafe impl<$($generic: SerializeConst),*> SerializeConst for ($($generic,)*) {
const MEMORY_LAYOUT: Layout = {
Layout::Struct(StructLayout {
size: std::mem::size_of::<($($generic,)*)>(),
data: &[
$(
StructFieldLayout::new(std::mem::offset_of!($inner, $generic_number), $generic::MEMORY_LAYOUT),
)*
],
})
};
}
};
}
impl_serialize_const_tuple!(T1: 0);
impl_serialize_const_tuple!(T1: 0, T2: 1);
impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2);
impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3);
impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4);
impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5);
impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6);
impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7);
impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7, T9: 8);
impl_serialize_const_tuple!(T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7, T9: 8, T10: 9);
const MAX_STR_SIZE: usize = 256;
/// A string that is stored in a constant sized buffer that can be serialized and deserialized at compile time
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ConstStr {
#[cfg_attr(feature = "serde", serde(with = "serde_bytes"))]
bytes: [u8; MAX_STR_SIZE],
len: u32,
}
#[cfg(feature = "serde")]
mod serde_bytes {
use serde::{Deserialize, Serializer};
pub fn serialize<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(bytes)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; super::MAX_STR_SIZE], D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes = Vec::<u8>::deserialize(deserializer)?;
bytes
.try_into()
.map_err(|_| serde::de::Error::custom("Failed to convert bytes to a fixed size array"))
}
}
unsafe impl SerializeConst for ConstStr {
const MEMORY_LAYOUT: Layout = Layout::Struct(StructLayout {
size: std::mem::size_of::<Self>(),
data: &[
StructFieldLayout::new(
std::mem::offset_of!(Self, bytes),
Layout::List(ListLayout {
len: MAX_STR_SIZE,
item_layout: &Layout::Primitive(PrimitiveLayout {
size: std::mem::size_of::<u8>(),
}),
}),
),
StructFieldLayout::new(
std::mem::offset_of!(Self, len),
Layout::Primitive(PrimitiveLayout {
size: std::mem::size_of::<u32>(),
}),
),
],
});
}
impl ConstStr {
/// Create a new constant string
pub const fn new(s: &str) -> Self {
let str_bytes = s.as_bytes();
let mut bytes = [0; MAX_STR_SIZE];
let mut i = 0;
while i < str_bytes.len() {
bytes[i] = str_bytes[i];
i += 1;
}
Self {
bytes,
len: str_bytes.len() as u32,
}
}
/// Get a reference to the string
pub const fn as_str(&self) -> &str {
let str_bytes = self.bytes.split_at(self.len as usize).0;
match std::str::from_utf8(str_bytes) {
Ok(s) => s,
Err(_) => panic!(
"Invalid utf8; ConstStr should only ever be constructed from valid utf8 strings"
),
}
}
/// Get the length of the string
pub const fn len(&self) -> usize {
self.len as usize
}
/// Check if the string is empty
pub const fn is_empty(&self) -> bool {
self.len == 0
}
/// Push a character onto the string
pub const fn push(self, byte: char) -> Self {
assert!(byte.is_ascii(), "Only ASCII bytes are supported");
let (bytes, len) = char_to_bytes(byte);
let (str, _) = bytes.split_at(len);
let Ok(str) = std::str::from_utf8(str) else {
panic!("Invalid utf8; char_to_bytes should always return valid utf8 bytes")
};
self.push_str(str)
}
/// Push a str onto the string
pub const fn push_str(self, str: &str) -> Self {
let Self { mut bytes, len } = self;
assert!(
str.len() + len as usize <= MAX_STR_SIZE,
"String is too long"
);
let str_bytes = str.as_bytes();
let new_len = len as usize + str_bytes.len();
let mut i = 0;
while i < str_bytes.len() {
bytes[len as usize + i] = str_bytes[i];
i += 1;
}
Self {
bytes,
len: new_len as u32,
}
}
/// Split the string at a byte index. The byte index must be a char boundary
pub const fn split_at(self, index: usize) -> (Self, Self) {
let (left, right) = self.bytes.split_at(index);
let left = match std::str::from_utf8(left) {
Ok(s) => s,
Err(_) => {
panic!("Invalid utf8; you cannot split at a byte that is not a char boundary")
}
};
let right = match std::str::from_utf8(right) {
Ok(s) => s,
Err(_) => {
panic!("Invalid utf8; you cannot split at a byte that is not a char boundary")
}
};
(Self::new(left), Self::new(right))
}
/// Split the string at the last occurrence of a character
pub const fn rsplit_once(&self, char: char) -> Option<(Self, Self)> {
let str = self.as_str();
let mut index = str.len() - 1;
// First find the bytes we are searching for
let (char_bytes, len) = char_to_bytes(char);
let (char_bytes, _) = char_bytes.split_at(len);
let bytes = str.as_bytes();
// Then walk backwards from the end of the string
loop {
let byte = bytes[index];
// Look for char boundaries in the string and check if the bytes match
if let Some(char_boundary_len) = utf8_char_boundary_to_char_len(byte) {
// Split up the string into three sections: [before_char, in_char, after_char]
let (before_char, after_index) = bytes.split_at(index);
let (in_char, after_char) = after_index.split_at(char_boundary_len as usize);
if in_char.len() != char_boundary_len as usize {
panic!("in_char.len() should always be equal to char_boundary_len as usize")
}
// Check if the bytes for the current char and the target char match
let mut in_char_eq = true;
let mut i = 0;
let min_len = if in_char.len() < char_bytes.len() {
in_char.len()
} else {
char_bytes.len()
};
while i < min_len {
in_char_eq &= in_char[i] == char_bytes[i];
i += 1;
}
// If they do, convert the bytes to strings and return the split strings
if in_char_eq {
let Ok(before_char_str) = std::str::from_utf8(before_char) else {
panic!("Invalid utf8; utf8_char_boundary_to_char_len should only return Some when the byte is a character boundary")
};
let Ok(after_char_str) = std::str::from_utf8(after_char) else {
panic!("Invalid utf8; utf8_char_boundary_to_char_len should only return Some when the byte is a character boundary")
};
return Some((Self::new(before_char_str), Self::new(after_char_str)));
}
}
match index.checked_sub(1) {
Some(new_index) => index = new_index,
None => return None,
}
}
}
/// Split the string at the first occurrence of a character
pub const fn split_once(&self, char: char) -> Option<(Self, Self)> {
let str = self.as_str();
let mut index = 0;
// First find the bytes we are searching for
let (char_bytes, len) = char_to_bytes(char);
let (char_bytes, _) = char_bytes.split_at(len);
let bytes = str.as_bytes();
// Then walk forwards from the start of the string
while index < bytes.len() {
let byte = bytes[index];
// Look for char boundaries in the string and check if the bytes match
if let Some(char_boundary_len) = utf8_char_boundary_to_char_len(byte) {
// Split up the string into three sections: [before_char, in_char, after_char]
let (before_char, after_index) = bytes.split_at(index);
let (in_char, after_char) = after_index.split_at(char_boundary_len as usize);
if in_char.len() != char_boundary_len as usize {
panic!("in_char.len() should always be equal to char_boundary_len as usize")
}
// Check if the bytes for the current char and the target char match
let mut in_char_eq = true;
let mut i = 0;
let min_len = if in_char.len() < char_bytes.len() {
in_char.len()
} else {
char_bytes.len()
};
while i < min_len {
in_char_eq &= in_char[i] == char_bytes[i];
i += 1;
}
// If they do, convert the bytes to strings and return the split strings
if in_char_eq {
let Ok(before_char_str) = std::str::from_utf8(before_char) else {
panic!("Invalid utf8; utf8_char_boundary_to_char_len should only return Some when the byte is a character boundary")
};
let Ok(after_char_str) = std::str::from_utf8(after_char) else {
panic!("Invalid utf8; utf8_char_boundary_to_char_len should only return Some when the byte is a character boundary")
};
return Some((Self::new(before_char_str), Self::new(after_char_str)));
}
}
index += 1
}
None
}
}
#[test]
fn test_rsplit_once() {
let str = ConstStr::new("hello world");
assert_eq!(
str.rsplit_once(' '),
Some((ConstStr::new("hello"), ConstStr::new("world")))
);
let unicode_str = ConstStr::new("hi😀hello😀world😀world");
assert_eq!(
unicode_str.rsplit_once('😀'),
Some((ConstStr::new("hi😀hello😀world"), ConstStr::new("world")))
);
assert_eq!(unicode_str.rsplit_once('❌'), None);
for _ in 0..100 {
let random_str: String = (0..rand::random::<u8>() % 50)
.map(|_| rand::random::<char>())
.collect();
let konst = ConstStr::new(&random_str);
let mut seen_chars = std::collections::HashSet::new();
for char in random_str.chars().rev() {
let (char_bytes, len) = char_to_bytes(char);
let char_bytes = &char_bytes[..len];
assert_eq!(char_bytes, char.to_string().as_bytes());
if seen_chars.contains(&char) {
continue;
}
seen_chars.insert(char);
let (correct_left, correct_right) = random_str.rsplit_once(char).unwrap();
let (left, right) = konst.rsplit_once(char).unwrap();
println!("splitting {random_str:?} at {char:?}");
assert_eq!(left.as_str(), correct_left);
assert_eq!(right.as_str(), correct_right);
}
}
}
const CONTINUED_CHAR_MASK: u8 = 0b10000000;
const BYTE_CHAR_BOUNDARIES: [u8; 4] = [0b00000000, 0b11000000, 0b11100000, 0b11110000];
// Const version of https://doc.rust-lang.org/src/core/char/methods.rs.html#1765-1797
const fn char_to_bytes(char: char) -> ([u8; 4], usize) {
let code = char as u32;
let len = char.len_utf8();
let mut bytes = [0; 4];
match len {
1 => {
bytes[0] = code as u8;
}
2 => {
bytes[0] = (code >> 6 & 0x1F) as u8 | BYTE_CHAR_BOUNDARIES[1];
bytes[1] = (code & 0x3F) as u8 | CONTINUED_CHAR_MASK;
}
3 => {
bytes[0] = (code >> 12 & 0x0F) as u8 | BYTE_CHAR_BOUNDARIES[2];
bytes[1] = (code >> 6 & 0x3F) as u8 | CONTINUED_CHAR_MASK;
bytes[2] = (code & 0x3F) as u8 | CONTINUED_CHAR_MASK;
}
4 => {
bytes[0] = (code >> 18 & 0x07) as u8 | BYTE_CHAR_BOUNDARIES[3];
bytes[1] = (code >> 12 & 0x3F) as u8 | CONTINUED_CHAR_MASK;
bytes[2] = (code >> 6 & 0x3F) as u8 | CONTINUED_CHAR_MASK;
bytes[3] = (code & 0x3F) as u8 | CONTINUED_CHAR_MASK;
}
_ => panic!(
"encode_utf8: need more than 4 bytes to encode the unicode character, but the buffer has 4 bytes"
),
};
(bytes, len)
}
#[test]
fn fuzz_char_to_bytes() {
use std::char;
for _ in 0..100 {
let char = rand::random::<char>();
let (bytes, len) = char_to_bytes(char);
let str = std::str::from_utf8(&bytes[..len]).unwrap();
assert_eq!(char.to_string(), str);
}
}
const fn utf8_char_boundary_to_char_len(byte: u8) -> Option<u8> {
match byte {
0b00000000..=0b01111111 => Some(1),
0b11000000..=0b11011111 => Some(2),
0b11100000..=0b11101111 => Some(3),
0b11110000..=0b11111111 => Some(4),
_ => None,
}
}
#[test]
fn fuzz_utf8_byte_to_char_len() {
for _ in 0..100 {
let random_string: String = (0..rand::random::<u8>())
.map(|_| rand::random::<char>())
.collect();
let bytes = random_string.as_bytes();
let chars: std::collections::HashMap<_, _> = random_string.char_indices().collect();
for (i, byte) in bytes.iter().enumerate() {
match utf8_char_boundary_to_char_len(*byte) {
Some(char_len) => {
let char = chars
.get(&i)
.unwrap_or_else(|| panic!("{byte:b} is not a character boundary"));
assert_eq!(char.len_utf8(), char_len as usize);
}
None => {
assert!(!chars.contains_key(&i), "{byte:b} is a character boundary");
}
}
}
}
}
/// Serialize a struct that is stored at the pointer passed in
const fn serialize_const_struct(
ptr: *const (),
mut to: ConstVec<u8>,
layout: &StructLayout,
) -> ConstVec<u8> {
let mut i = 0;
while i < layout.data.len() {
// Serialize the field at the offset pointer in the struct
let StructFieldLayout { offset, layout } = &layout.data[i];
let field = unsafe { ptr.byte_add(*offset) };
to = serialize_const_ptr(field, to, layout);
i += 1;
}
to
}
/// Serialize an enum that is stored at the pointer passed in
const fn serialize_const_enum(
ptr: *const (),
mut to: ConstVec<u8>,
layout: &EnumLayout,
) -> ConstVec<u8> {
let mut discriminant = 0;
let byte_ptr = ptr as *const u8;
let mut offset = 0;
while offset < layout.discriminant.size {
// If the bytes are reversed, walk backwards from the end of the number when pushing bytes
let byte = if cfg!(target_endian = "big") {
unsafe {
byte_ptr
.byte_add(layout.discriminant.size - offset - 1)
.read()
}
} else {
unsafe { byte_ptr.byte_add(offset).read() }
};
to = to.push(byte);
discriminant |= (byte as u32) << (offset * 8);
offset += 1;
}
let mut i = 0;
while i < layout.variants.len() {
// If the variant is the discriminated one, serialize it
let EnumVariant { tag, data, .. } = &layout.variants[i];
if discriminant == *tag {
let data_ptr = unsafe { ptr.byte_add(layout.variants_offset) };
to = serialize_const_struct(data_ptr, to, data);
break;
}
i += 1;
}
to
}
/// Serialize a primitive type that is stored at the pointer passed in
const fn serialize_const_primitive(
ptr: *const (),
mut to: ConstVec<u8>,
layout: &PrimitiveLayout,
) -> ConstVec<u8> {
let ptr = ptr as *const u8;
let mut offset = 0;
while offset < layout.size {
// If the bytes are reversed, walk backwards from the end of the number when pushing bytes
if cfg!(any(target_endian = "big", feature = "test-big-endian")) {
to = to.push(unsafe { ptr.byte_add(layout.size - offset - 1).read() });
} else {
to = to.push(unsafe { ptr.byte_add(offset).read() });
}
offset += 1;
}
to
}
/// Serialize a constant sized array that is stored at the pointer passed in
const fn serialize_const_list(
ptr: *const (),
mut to: ConstVec<u8>,
layout: &ListLayout,
) -> ConstVec<u8> {
let len = layout.len;
let mut i = 0;
while i < len {
let field = unsafe { ptr.byte_add(i * layout.item_layout.size()) };
to = serialize_const_ptr(field, to, layout.item_layout);
i += 1;
}
to
}
/// Serialize a pointer to a type that is stored at the pointer passed in
const fn serialize_const_ptr(ptr: *const (), to: ConstVec<u8>, layout: &Layout) -> ConstVec<u8> {
match layout {
Layout::Enum(layout) => serialize_const_enum(ptr, to, layout),
Layout::Struct(layout) => serialize_const_struct(ptr, to, layout),
Layout::List(layout) => serialize_const_list(ptr, to, layout),
Layout::Primitive(layout) => serialize_const_primitive(ptr, to, layout),
}
}
/// Serialize a type into a buffer
///
/// # Example
///
/// ```rust
/// use const_serialize::{ConstVec, SerializeConst, serialize_const};
///
/// #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
/// struct Struct {
/// a: u32,
/// b: u8,
/// c: u32,
/// }
///
/// let mut buffer = ConstVec::new();
/// buffer = serialize_const(&Struct {
/// a: 0x11111111,
/// b: 0x22,
/// c: 0x33333333,
/// }, buffer);
/// let buf = buffer.read();
/// assert_eq!(buf.as_ref(), &[0x11, 0x11, 0x11, 0x11, 0x22, 0x33, 0x33, 0x33, 0x33]);
/// ```
#[must_use = "The data is serialized into the returned buffer"]
pub const fn serialize_const<T: SerializeConst>(data: &T, to: ConstVec<u8>) -> ConstVec<u8> {
let ptr = data as *const T as *const ();
serialize_const_ptr(ptr, to, &T::MEMORY_LAYOUT)
}
/// Deserialize a primitive type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added.
const fn deserialize_const_primitive<'a, const N: usize>(
mut from: ConstReadBuffer<'a>,
layout: &PrimitiveLayout,
out: (usize, [MaybeUninit<u8>; N]),
) -> Option<(ConstReadBuffer<'a>, [MaybeUninit<u8>; N])> {
let (start, mut out) = out;
let mut offset = 0;
while offset < layout.size {
// If the bytes are reversed, walk backwards from the end of the number when filling in bytes
let (from_new, value) = match from.get() {
Some(data) => data,
None => return None,
};
from = from_new;
if cfg!(any(target_endian = "big", feature = "test-big-endian")) {
out[start + layout.size - offset - 1] = MaybeUninit::new(value);
} else {
out[start + offset] = MaybeUninit::new(value);
}
offset += 1;
}
Some((from, out))
}
/// Deserialize a struct type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added.
const fn deserialize_const_struct<'a, const N: usize>(
mut from: ConstReadBuffer<'a>,
layout: &StructLayout,
out: (usize, [MaybeUninit<u8>; N]),
) -> Option<(ConstReadBuffer<'a>, [MaybeUninit<u8>; N])> {
let (start, mut out) = out;
let mut i = 0;
while i < layout.data.len() {
// Deserialize the field at the offset pointer in the struct
let StructFieldLayout { offset, layout } = &layout.data[i];
let (new_from, new_out) = match deserialize_const_ptr(from, layout, (start + *offset, out))
{
Some(data) => data,
None => return None,
};
from = new_from;
out = new_out;
i += 1;
}
Some((from, out))
}
/// Deserialize an enum type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added.
const fn deserialize_const_enum<'a, const N: usize>(
mut from: ConstReadBuffer<'a>,
layout: &EnumLayout,
out: (usize, [MaybeUninit<u8>; N]),
) -> Option<(ConstReadBuffer<'a>, [MaybeUninit<u8>; N])> {
let (start, mut out) = out;
let mut discriminant = 0;
// First, deserialize the discriminant
let mut offset = 0;
while offset < layout.discriminant.size {
// If the bytes are reversed, walk backwards from the end of the number when filling in bytes
let (from_new, value) = match from.get() {
Some(data) => data,
None => return None,
};
from = from_new;
if cfg!(target_endian = "big") {
out[start + layout.size - offset - 1] = MaybeUninit::new(value);
discriminant |= (value as u32) << ((layout.discriminant.size - offset - 1) * 8);
} else {
out[start + offset] = MaybeUninit::new(value);
discriminant |= (value as u32) << (offset * 8);
}
offset += 1;
}
// Then, deserialize the variant
let mut i = 0;
let mut matched_variant = false;
while i < layout.variants.len() {
// If the variant is the discriminated one, deserialize it
let EnumVariant { tag, data, .. } = &layout.variants[i];
if discriminant == *tag {
let offset = layout.variants_offset;
let (new_from, new_out) =
match deserialize_const_struct(from, data, (start + offset, out)) {
Some(data) => data,
None => return None,
};
from = new_from;
out = new_out;
matched_variant = true;
break;
}
i += 1;
}
if !matched_variant {
return None;
}
Some((from, out))
}
/// Deserialize a list type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added.
const fn deserialize_const_list<'a, const N: usize>(
mut from: ConstReadBuffer<'a>,
layout: &ListLayout,
out: (usize, [MaybeUninit<u8>; N]),
) -> Option<(ConstReadBuffer<'a>, [MaybeUninit<u8>; N])> {
let (start, mut out) = out;
let len = layout.len;
let item_layout = layout.item_layout;
let mut i = 0;
while i < len {
let (new_from, new_out) =
match deserialize_const_ptr(from, item_layout, (start + i * item_layout.size(), out)) {
Some(data) => data,
None => return None,
};
from = new_from;
out = new_out;
i += 1;
}
Some((from, out))
}
/// Deserialize a type into the out buffer at the offset passed in. Returns a new version of the buffer with the data added.
const fn deserialize_const_ptr<'a, const N: usize>(
from: ConstReadBuffer<'a>,
layout: &Layout,
out: (usize, [MaybeUninit<u8>; N]),
) -> Option<(ConstReadBuffer<'a>, [MaybeUninit<u8>; N])> {
match layout {
Layout::Enum(layout) => deserialize_const_enum(from, layout, out),
Layout::Struct(layout) => deserialize_const_struct(from, layout, out),
Layout::List(layout) => deserialize_const_list(from, layout, out),
Layout::Primitive(layout) => deserialize_const_primitive(from, layout, out),
}
}
/// Deserialize a type into the output buffer. Accepts (Type, ConstVec<u8>) as input and returns Option<(ConstReadBuffer, Instance of type)>
///
/// # Example
/// ```rust
/// # use const_serialize::{deserialize_const, serialize_const, ConstVec, SerializeConst};
/// #[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
/// struct Struct {
/// a: u32,
/// b: u8,
/// c: u32,
/// d: u32,
/// }
///
/// let mut buffer = ConstVec::new();
/// buffer = serialize_const(&Struct {
/// a: 0x11111111,
/// b: 0x22,
/// c: 0x33333333,
/// d: 0x44444444,
/// }, buffer);
/// let buf = buffer.read();
/// assert_eq!(deserialize_const!(Struct, buf).unwrap().1, Struct {
/// a: 0x11111111,
/// b: 0x22,
/// c: 0x33333333,
/// d: 0x44444444,
/// });
/// ```
#[macro_export]
macro_rules! deserialize_const {
($type:ty, $buffer:expr) => {
unsafe {
const __SIZE: usize = std::mem::size_of::<$type>();
$crate::deserialize_const_raw::<__SIZE, $type>($buffer)
}
};
}
/// Deserialize a buffer into a type. This will return None if the buffer doesn't have enough data to fill the type.
/// # Safety
/// N must be `std::mem::size_of::<T>()`
#[must_use = "The data is deserialized from the input buffer"]
pub const unsafe fn deserialize_const_raw<const N: usize, T: SerializeConst>(
from: ConstReadBuffer,
) -> Option<(ConstReadBuffer, T)> {
// Create uninitized memory with the size of the type
let out = [MaybeUninit::uninit(); N];
// Fill in the bytes into the buffer for the type
let (from, out) = match deserialize_const_ptr(from, &T::MEMORY_LAYOUT, (0, out)) {
Some(data) => data,
None => return None,
};
// Now that the memory is filled in, transmute it into the type
Some((from, unsafe {
std::mem::transmute_copy::<[MaybeUninit<u8>; N], T>(&out)
}))
}
/// Check if the serialized representation of two items are the same
pub const fn serialize_eq<T: SerializeConst>(first: &T, second: &T) -> bool {
let first_serialized = ConstVec::<u8>::new();
let first_serialized = serialize_const(first, first_serialized);
let second_serialized = ConstVec::<u8>::new();
let second_serialized = serialize_const(second, second_serialized);
let first_buf = first_serialized.as_ref();
let second_buf = second_serialized.as_ref();
if first_buf.len() != second_buf.len() {
return false;
}
let mut i = 0;
while i < first_buf.len() {
if first_buf[i] != second_buf[i] {
return false;
}
i += 1;
}
true
}
+206
View File
@@ -0,0 +1,206 @@
use const_serialize::{deserialize_const, serialize_const, ConstVec, SerializeConst};
use std::mem::MaybeUninit;
#[test]
fn test_transmute_bytes_to_enum() {
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C, u8)]
enum Enum<T> {
A { one: u32, two: u16 },
B { one: u8, two: T } = 15,
}
#[repr(C)]
#[derive(Debug, PartialEq)]
struct A {
one: u32,
two: u16,
}
#[repr(C)]
#[derive(Debug, PartialEq)]
struct B<T> {
one: u8,
two: T,
}
const SIZE: usize = std::mem::size_of::<Enum<u16>>();
let mut out = [MaybeUninit::uninit(); SIZE];
let discriminate_size = std::mem::size_of::<u8>();
let tag_align = 0;
let union_alignment = std::mem::align_of::<A>().max(std::mem::align_of::<B<u16>>());
let data_align = (discriminate_size / union_alignment) + union_alignment;
let a_one_align = std::mem::offset_of!(A, one);
let a_two_align = std::mem::offset_of!(A, two);
let b_one_align = std::mem::offset_of!(B<u16>, one);
let b_two_align = std::mem::offset_of!(B<u16>, two);
let one = 1234u32;
let two = 5678u16;
let first = Enum::A { one, two };
for (i, byte) in one.to_le_bytes().iter().enumerate() {
out[data_align + i + a_one_align] = MaybeUninit::new(*byte);
}
for (i, byte) in two.to_le_bytes().iter().enumerate() {
out[data_align + i + a_two_align] = MaybeUninit::new(*byte);
}
out[tag_align] = MaybeUninit::new(0);
let out = unsafe { std::mem::transmute_copy::<[MaybeUninit<u8>; SIZE], Enum<u16>>(&out) };
assert_eq!(out, first);
let mut out = [MaybeUninit::uninit(); SIZE];
let one = 123u8;
let two = 58u16;
let second = Enum::B { one, two };
for (i, byte) in one.to_le_bytes().iter().enumerate() {
out[data_align + i + b_one_align] = MaybeUninit::new(*byte);
}
for (i, byte) in two.to_le_bytes().iter().enumerate() {
out[data_align + i + b_two_align] = MaybeUninit::new(*byte);
}
out[tag_align] = MaybeUninit::new(15);
let out = unsafe { std::mem::transmute_copy::<[MaybeUninit<u8>; SIZE], Enum<u16>>(&out) };
assert_eq!(out, second);
}
#[test]
fn test_serialize_enum() {
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
#[repr(C, u8)]
enum Enum {
A { one: u32, two: u16 },
B { one: u8, two: u16 } = 15,
}
println!("{:#?}", Enum::MEMORY_LAYOUT);
let data = Enum::A {
one: 0x11111111,
two: 0x22,
};
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data);
let data = Enum::B {
one: 0x11,
two: 0x2233,
};
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data);
}
#[test]
fn test_serialize_u8_enum() {
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
#[repr(u8)]
enum Enum {
A,
B,
}
println!("{:#?}", Enum::MEMORY_LAYOUT);
let data = Enum::A;
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data);
let data = Enum::B;
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data);
}
#[test]
fn test_serialize_corrupted_enum() {
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
#[repr(C, u8)]
enum Enum {
A { one: u32, two: u16 },
}
let data = Enum::A {
one: 0x11111111,
two: 0x22,
};
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
buf = buf.set(0, 2);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf), None);
}
#[test]
fn test_serialize_nested_enum() {
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
#[repr(C, u8)]
enum Enum {
A { one: u32, two: u16 },
B { one: u8, two: InnerEnum } = 15,
}
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
#[repr(C, u16)]
enum InnerEnum {
A(u8),
B { one: u64, two: f64 } = 1000,
C { one: u32, two: u16 },
}
let data = Enum::A {
one: 0x11111111,
two: 0x22,
};
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data);
let data = Enum::B {
one: 0x11,
two: InnerEnum::A(0x22),
};
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data);
let data = Enum::B {
one: 0x11,
two: InnerEnum::B {
one: 0x2233,
two: 0.123456789,
},
};
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data);
let data = Enum::B {
one: 0x11,
two: InnerEnum::C {
one: 0x2233,
two: 56789,
},
};
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!(Enum, buf).unwrap().1, data);
}
+59
View File
@@ -0,0 +1,59 @@
use const_serialize::{serialize_eq, SerializeConst};
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
struct Struct {
a: u32,
b: u8,
c: u32,
d: Enum,
}
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
#[repr(C, u8)]
enum Enum {
A { one: u32, two: u16 },
B { one: u8, two: u16 } = 15,
}
#[test]
fn const_eq() {
const {
let data = [
Struct {
a: 0x11111111,
b: 0x22,
c: 0x33333333,
d: Enum::A {
one: 0x44444444,
two: 0x5555,
},
},
Struct {
a: 123,
b: 9,
c: 38,
d: Enum::B {
one: 0x44,
two: 0x555,
},
},
Struct {
a: 9,
b: 123,
c: 39,
d: Enum::B {
one: 0x46,
two: 0x555,
},
},
];
let mut other = data;
other[2].a += 1;
if serialize_eq(&data, &other) {
panic!("data should be different");
}
if !serialize_eq(&data, &data) {
panic!("data should be the same");
}
}
}
+34
View File
@@ -0,0 +1,34 @@
use const_serialize::{deserialize_const, serialize_const, ConstVec};
#[test]
fn test_serialize_const_layout_list() {
let mut buf = ConstVec::new();
buf = serialize_const(&[1u8, 2, 3] as &[u8; 3], buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(deserialize_const!([u8; 3], buf).unwrap().1, [1, 2, 3])
}
#[test]
fn test_serialize_const_layout_nested_lists() {
let mut buf = ConstVec::new();
buf = serialize_const(
&[[1u8, 2, 3], [4u8, 5, 6], [7u8, 8, 9]] as &[[u8; 3]; 3],
buf,
);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(
deserialize_const!([[u8; 3]; 3], buf).unwrap().1,
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
);
}
#[test]
fn test_serialize_list_too_little_data() {
let mut buf = ConstVec::new();
buf = buf.push(1);
let buf = buf.read();
assert_eq!(deserialize_const!([u64; 10], buf), None);
}
@@ -0,0 +1,71 @@
use const_serialize::{deserialize_const, serialize_const, ConstVec};
#[test]
fn test_serialize_const_layout_primitive() {
let mut buf = ConstVec::new();
buf = serialize_const(&1234u32, buf);
if cfg!(feature = "test-big-endian") {
assert_eq!(buf.as_ref(), 1234u32.to_be_bytes());
} else {
assert_eq!(buf.as_ref(), 1234u32.to_le_bytes());
}
let buf = buf.read();
assert_eq!(deserialize_const!(u32, buf).unwrap().1, 1234u32);
let mut buf = ConstVec::new();
buf = serialize_const(&1234u64, buf);
if cfg!(feature = "test-big-endian") {
assert_eq!(buf.as_ref(), 1234u64.to_be_bytes());
} else {
assert_eq!(buf.as_ref(), 1234u64.to_le_bytes());
}
let buf = buf.read();
assert_eq!(deserialize_const!(u64, buf).unwrap().1, 1234u64);
let mut buf = ConstVec::new();
buf = serialize_const(&1234i32, buf);
if cfg!(feature = "test-big-endian") {
assert_eq!(buf.as_ref(), 1234i32.to_be_bytes());
} else {
assert_eq!(buf.as_ref(), 1234i32.to_le_bytes());
}
let buf = buf.read();
assert_eq!(deserialize_const!(i32, buf).unwrap().1, 1234i32);
let mut buf = ConstVec::new();
buf = serialize_const(&1234i64, buf);
if cfg!(feature = "test-big-endian") {
assert_eq!(buf.as_ref(), 1234i64.to_be_bytes());
} else {
assert_eq!(buf.as_ref(), 1234i64.to_le_bytes());
}
let buf = buf.read();
assert_eq!(deserialize_const!(i64, buf).unwrap().1, 1234i64);
let mut buf = ConstVec::new();
buf = serialize_const(&true, buf);
assert_eq!(buf.as_ref(), [1u8]);
let buf = buf.read();
assert!(deserialize_const!(bool, buf).unwrap().1);
let mut buf = ConstVec::new();
buf = serialize_const(&0.631f32, buf);
if cfg!(feature = "test-big-endian") {
assert_eq!(buf.as_ref(), 0.631f32.to_be_bytes());
} else {
assert_eq!(buf.as_ref(), 0.631f32.to_le_bytes());
}
let buf = buf.read();
assert_eq!(deserialize_const!(f32, buf).unwrap().1, 0.631);
}
#[test]
fn test_serialize_primitive_too_little_data() {
let mut buf = ConstVec::new();
buf = buf.push(1);
buf = buf.push(1);
buf = buf.push(1);
let buf = buf.read();
assert_eq!(deserialize_const!(u64, buf), None);
}
+40
View File
@@ -0,0 +1,40 @@
use const_serialize::{deserialize_const, serialize_const, ConstStr, ConstVec};
#[test]
fn test_serialize_const_layout_str() {
let mut buf = ConstVec::new();
let str = ConstStr::new("hello");
buf = serialize_const(&str, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(
deserialize_const!(ConstStr, buf).unwrap().1.as_str(),
"hello"
);
}
#[test]
fn test_serialize_const_layout_nested_str() {
let mut buf = ConstVec::new();
let str = ConstStr::new("hello");
buf = serialize_const(&[str, str, str] as &[ConstStr; 3], buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
assert_eq!(
deserialize_const!([ConstStr; 3], buf).unwrap().1,
[
ConstStr::new("hello"),
ConstStr::new("hello"),
ConstStr::new("hello")
]
);
}
#[test]
fn test_serialize_str_too_little_data() {
let mut buf = ConstVec::new();
buf = buf.push(1);
let buf = buf.read();
assert_eq!(deserialize_const!(ConstStr, buf), None);
}
+164
View File
@@ -0,0 +1,164 @@
use const_serialize::{deserialize_const, serialize_const, ConstVec, SerializeConst};
use std::mem::MaybeUninit;
#[test]
fn test_transmute_bytes_to_struct() {
struct MyStruct {
a: u32,
b: u8,
c: u32,
d: u32,
}
const SIZE: usize = std::mem::size_of::<MyStruct>();
let mut out = [MaybeUninit::uninit(); SIZE];
let first_align = std::mem::offset_of!(MyStruct, a);
let second_align = std::mem::offset_of!(MyStruct, b);
let third_align = std::mem::offset_of!(MyStruct, c);
let fourth_align = std::mem::offset_of!(MyStruct, d);
for (i, byte) in 1234u32.to_le_bytes().iter().enumerate() {
out[i + first_align] = MaybeUninit::new(*byte);
}
for (i, byte) in 12u8.to_le_bytes().iter().enumerate() {
out[i + second_align] = MaybeUninit::new(*byte);
}
for (i, byte) in 13u32.to_le_bytes().iter().enumerate() {
out[i + third_align] = MaybeUninit::new(*byte);
}
for (i, byte) in 14u32.to_le_bytes().iter().enumerate() {
out[i + fourth_align] = MaybeUninit::new(*byte);
}
let out = unsafe { std::mem::transmute_copy::<[MaybeUninit<u8>; SIZE], MyStruct>(&out) };
assert_eq!(out.a, 1234);
assert_eq!(out.b, 12);
assert_eq!(out.c, 13);
assert_eq!(out.d, 14);
}
#[test]
fn test_serialize_const_layout_struct_list() {
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
struct Struct {
a: u32,
b: u8,
c: u32,
d: u32,
}
impl Struct {
#[allow(dead_code)]
const fn equal(&self, other: &Struct) -> bool {
self.a == other.a && self.b == other.b && self.c == other.c && self.d == other.d
}
}
#[derive(Clone, Copy, Debug, PartialEq, SerializeConst)]
struct OtherStruct {
a: u32,
b: u8,
c: Struct,
d: u32,
}
impl OtherStruct {
#[allow(dead_code)]
const fn equal(&self, other: &OtherStruct) -> bool {
self.a == other.a && self.b == other.b && self.c.equal(&other.c) && self.d == other.d
}
}
const INNER_DATA: Struct = Struct {
a: 0x11111111,
b: 0x22,
c: 0x33333333,
d: 0x44444444,
};
const DATA: [OtherStruct; 3] = [
OtherStruct {
a: 0x11111111,
b: 0x22,
c: INNER_DATA,
d: 0x44444444,
},
OtherStruct {
a: 0x111111,
b: 0x23,
c: INNER_DATA,
d: 0x44444444,
},
OtherStruct {
a: 0x11111111,
b: 0x11,
c: INNER_DATA,
d: 0x44441144,
},
];
const _ASSERT: () = {
let mut buf = ConstVec::new();
buf = serialize_const(&DATA, buf);
let buf = buf.read();
let [first, second, third] = match deserialize_const!([OtherStruct; 3], buf) {
Some((_, data)) => data,
None => panic!("data mismatch"),
};
if !(first.equal(&DATA[0]) && second.equal(&DATA[1]) && third.equal(&DATA[2])) {
panic!("data mismatch");
}
};
const _ASSERT_2: () = {
let mut buf = ConstVec::new();
const DATA_AGAIN: [[OtherStruct; 3]; 3] = [DATA, DATA, DATA];
buf = serialize_const(&DATA_AGAIN, buf);
let buf = buf.read();
let [first, second, third] = match deserialize_const!([[OtherStruct; 3]; 3], buf) {
Some((_, data)) => data,
None => panic!("data mismatch"),
};
if !(first[0].equal(&DATA[0]) && first[1].equal(&DATA[1]) && first[2].equal(&DATA[2])) {
panic!("data mismatch");
}
if !(second[0].equal(&DATA[0]) && second[1].equal(&DATA[1]) && second[2].equal(&DATA[2])) {
panic!("data mismatch");
}
if !(third[0].equal(&DATA[0]) && third[1].equal(&DATA[1]) && third[2].equal(&DATA[2])) {
panic!("data mismatch");
}
};
let mut buf = ConstVec::new();
buf = serialize_const(&DATA, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
let (_, data2) = deserialize_const!([OtherStruct; 3], buf).unwrap();
assert_eq!(DATA, data2);
}
#[test]
fn test_serialize_const_layout_struct() {
#[derive(Debug, PartialEq, SerializeConst)]
struct Struct {
a: u32,
b: u8,
c: u32,
d: u32,
}
#[derive(Debug, PartialEq, SerializeConst)]
struct OtherStruct(u32, u8, Struct, u32);
println!("{:?}", OtherStruct::MEMORY_LAYOUT);
let data = Struct {
a: 0x11111111,
b: 0x22,
c: 0x33333333,
d: 0x44444444,
};
let data = OtherStruct(0x11111111, 0x22, data, 0x44444444);
let mut buf = ConstVec::new();
buf = serialize_const(&data, buf);
println!("{:?}", buf.as_ref());
let buf = buf.read();
let (_, data2) = deserialize_const!(OtherStruct, buf).unwrap();
assert_eq!(data, data2);
}
+28
View File
@@ -0,0 +1,28 @@
use const_serialize::{deserialize_const, serialize_const, ConstVec};
#[test]
fn test_serialize_const_layout_tuple() {
let mut buf = ConstVec::new();
buf = serialize_const(&(1234u32, 5678u16), buf);
let buf = buf.read();
assert_eq!(
deserialize_const!((u32, u16), buf).unwrap().1,
(1234u32, 5678u16)
);
let mut buf = ConstVec::new();
buf = serialize_const(&(1234f64, 5678u16, 90u8), buf);
let buf = buf.read();
assert_eq!(
deserialize_const!((f64, u16, u8), buf).unwrap().1,
(1234f64, 5678u16, 90u8)
);
let mut buf = ConstVec::new();
buf = serialize_const(&(1234u32, 5678u16, 90u8, 1000000f64), buf);
let buf = buf.read();
assert_eq!(
deserialize_const!((u32, u16, u8, f64), buf).unwrap().1,
(1234u32, 5678u16, 90u8, 1000000f64)
);
}
+1 -1
View File
@@ -26,7 +26,7 @@ dioxus-desktop = { workspace = true, default-features = true, optional = true }
dioxus-fullstack = { workspace = true, default-features = true, optional = true }
dioxus-liveview = { workspace = true, optional = true }
dioxus-ssr = { workspace = true, optional = true }
manganis = { workspace = true, optional = true }
manganis = { workspace = true, features = ["dioxus"], optional = true }
serde = { version = "1.0.136", optional = true }
+1 -1
View File
@@ -128,7 +128,7 @@ pub mod prelude {
#[cfg(feature = "asset")]
#[cfg_attr(docsrs, doc(cfg(feature = "asset")))]
pub use manganis::{self, asset, Asset, ImageAsset, ImageType};
pub use manganis::{self, *};
}
#[cfg(feature = "web")]
+9 -1
View File
@@ -13,5 +13,13 @@ keywords = ["dom", "ui", "gui", "assets"]
# be careful with dependencies you add here - these need to get compiled for the proc macro and therefore
# prevent the main code from compiling!
[dependencies]
serde_json = "1.0"
serde = { workspace = true, features = ["derive"] }
const-serialize = { workspace = true, features = ["serde"] }
dioxus-core-types = { workspace = true, optional = true }
[dev-dependencies]
manganis = { workspace = true }
dioxus = { workspace = true }
[features]
dioxus = ["dep:dioxus-core-types"]
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Whitespace-only changes.
Whitespace-only changes.
+150 -112
View File
@@ -1,118 +1,156 @@
use serde::{Deserialize, Serialize};
use std::{
hash::{Hash, Hasher},
path::PathBuf,
time::SystemTime,
};
use crate::AssetOptions;
use const_serialize::{ConstStr, SerializeConst};
use std::path::PathBuf;
/// The location we'll write to the link section - needs to be serializable
///
/// This basically is 1:1 with `manganis/Asset` but with more metadata to be useful to the macro and cli
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct ResourceAsset {
/// The input path `/assets/blah.css`
pub input: PathBuf,
/// The canonicalized asset
///
/// `Users/dioxus/dev/app/assets/blah.css`
pub absolute: PathBuf,
/// The post-bundle name of the asset - do we include the `assets` name?
///
/// `blahcss123.css`
pub bundled: String,
/// An asset that should be copied by the bundler with some options. This type will be
/// serialized into the binary and added to the link section [`LinkSection::CURRENT`](crate::linker::LinkSection::CURRENT).
/// CLIs that support manganis, should pull out the assets from the link section, optimize,
/// and write them to the filesystem at [`BundledAsset::bundled_path`] for the application
/// to use.
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct BundledAsset {
/// The absolute path of the asset
absolute_source_path: ConstStr,
/// The bundled path of the asset
bundled_path: ConstStr,
/// The options for the asset
options: AssetOptions,
}
impl ResourceAsset {
pub fn parse_any(raw: &str) -> Result<Self, AssetParseError> {
// get the location where the asset is absolute, relative to
//
// IE
// /users/dioxus/dev/app/
// is the root of
// /users/dioxus/dev/app/assets/blah.css
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap();
// 1. the input file should be a pathbuf
let input = PathBuf::from(raw);
// 2. absolute path to the asset
let absolute = manifest_dir.join(raw.trim_start_matches('/'));
let absolute =
absolute
.canonicalize()
.map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: absolute,
})?;
// 3. the bundled path is the unique name of the asset
let bundled = Self::make_unique_name(absolute.clone())?;
Ok(Self {
input,
absolute,
bundled,
})
}
fn make_unique_name(file_path: PathBuf) -> Result<String, AssetParseError> {
// Create a hasher
let mut hash = std::collections::hash_map::DefaultHasher::new();
// Open the file to get its options
let file = std::fs::File::open(&file_path).map_err(AssetParseError::FailedToReadAsset)?;
let modified = file
.metadata()
.and_then(|metadata| metadata.modified())
.unwrap_or(SystemTime::UNIX_EPOCH);
// Hash a bunch of metadata
// name, options, modified time, and maybe the version of our crate
// Hash the last time the file was updated and the file source. If either of these change, we need to regenerate the unique name
modified.hash(&mut hash);
file_path.hash(&mut hash);
let uuid = hash.finish();
let file_name = file_path
.file_stem()
.expect("file_path should have a file_stem")
.to_string_lossy();
let extension = file_path
.extension()
.map(|f| f.to_string_lossy())
.unwrap_or_default();
Ok(format!("{file_name}-{uuid:x}.{extension}"))
}
}
#[derive(Debug)]
pub enum AssetParseError {
ParseError(String),
AssetDoesntExist {
err: std::io::Error,
path: std::path::PathBuf,
},
FailedToReadAsset(std::io::Error),
FailedToReadMetadata(std::io::Error),
}
impl std::fmt::Display for AssetParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AssetParseError::ParseError(err) => write!(f, "Failed to parse asset: {}", err),
AssetParseError::AssetDoesntExist { err, path } => {
write!(f, "Asset at {} doesn't exist: {}", path.display(), err)
}
AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err),
AssetParseError::FailedToReadMetadata(err) => {
write!(f, "Failed to read asset metadata: {}", err)
}
impl BundledAsset {
#[doc(hidden)]
/// This should only be called from the macro
/// Create a new asset
pub const fn new(
absolute_source_path: &'static str,
bundled_path: &'static str,
options: AssetOptions,
) -> Self {
Self {
absolute_source_path: ConstStr::new(absolute_source_path),
bundled_path: ConstStr::new(bundled_path),
options,
}
}
#[doc(hidden)]
/// This should only be called from the macro
/// Create a new asset but with a relative path
///
/// This method is deprecated and will be removed in a future release.
#[deprecated(
note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`"
)]
pub const fn new_relative(
absolute_source_path: &'static str,
bundled_path: &'static str,
options: AssetOptions,
) -> Self {
Self::new(absolute_source_path, bundled_path, options)
}
/// Get the bundled name of the asset. This identifier cannot be used to read the asset directly
pub fn bundled_path(&self) -> &str {
self.bundled_path.as_str()
}
/// Get the absolute path of the asset source. This path will not be available when the asset is bundled
pub fn absolute_source_path(&self) -> &str {
self.absolute_source_path.as_str()
}
/// Get the options for the asset
pub const fn options(&self) -> &AssetOptions {
&self.options
}
}
/// A bundled asset with some options. The asset can be used in rsx! to reference the asset.
/// It should not be read directly with [`std::fs::read`] because the path needs to be resolved
/// relative to the bundle
///
/// ```rust
/// # use manganis::{asset, Asset};
/// # use dioxus::prelude::*;
/// const ASSET: Asset = asset!("/assets/image.png");
/// rsx! {
/// img { src: ASSET }
/// };
/// ```
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct Asset {
/// The bundled asset
bundled: BundledAsset,
/// The link section for the asset
keep_link_section: fn() -> u8,
}
impl Asset {
#[doc(hidden)]
/// This should only be called from the macro
/// Create a new asset from the bundled form of the asset and the link section
pub const fn new(bundled: BundledAsset, keep_link_section: fn() -> u8) -> Self {
Self {
bundled,
keep_link_section,
}
}
/// Get the bundled asset
pub const fn bundled(&self) -> &BundledAsset {
&self.bundled
}
/// Return a canonicalized path to the asset
///
/// Attempts to resolve it against an `assets` folder in the current directory.
/// If that doesn't exist, it will resolve against the cargo manifest dir
pub fn resolve(&self) -> PathBuf {
// Force a volatile read of the asset link section to ensure the symbol makes it into the binary
(self.keep_link_section)();
#[cfg(feature = "dioxus")]
// If the asset is relative, we resolve the asset at the current directory
if !dioxus_core_types::is_bundled_app() {
return PathBuf::from(self.bundled.absolute_source_path.as_str());
}
// Otherwise presumably we're bundled and we can use the bundled path
PathBuf::from("/assets/").join(PathBuf::from(
self.bundled.bundled_path.as_str().trim_start_matches('/'),
))
}
}
impl From<Asset> for String {
fn from(value: Asset) -> Self {
value.to_string()
}
}
impl From<Asset> for Option<String> {
fn from(value: Asset) -> Self {
Some(value.to_string())
}
}
impl std::fmt::Display for Asset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.resolve().display())
}
}
#[cfg(feature = "dioxus")]
impl dioxus_core_types::DioxusFormattable for Asset {
fn format(&self) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Owned(self.to_string())
}
}
@@ -0,0 +1,77 @@
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// Options for a css asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct CssAssetOptions {
minify: bool,
preload: bool,
}
impl Default for CssAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl CssAssetOptions {
/// Create a new css asset using the builder
pub const fn new() -> Self {
Self {
preload: false,
minify: true,
}
}
/// Sets whether the css should be minified (default: true)
///
/// Minifying the css can make your site load faster by loading less data
///
/// ```rust
/// # use manganis::{asset, Asset, CssAssetOptions};
/// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_minify(false));
/// ```
#[allow(unused)]
pub const fn with_minify(self, minify: bool) -> Self {
Self { minify, ..self }
}
/// Check if the asset is minified
pub const fn minified(&self) -> bool {
self.minify
}
/// Make the asset preloaded
///
/// Preloading css will make the image start to load as soon as possible. This is useful for css that is used soon after the page loads or css that may not be used immediately, but should start loading sooner
///
/// ```rust
/// # use manganis::{asset, Asset, CssAssetOptions};
/// const _: Asset = asset!("/assets/style.css", CssAssetOptions::new().with_preload(true));
/// ```
#[allow(unused)]
pub const fn with_preload(self, preload: bool) -> Self {
Self { preload, ..self }
}
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool {
self.preload
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Css(self)
}
}
@@ -0,0 +1,35 @@
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// The builder for [`FolderAsset`]
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct FolderAssetOptions {}
impl Default for FolderAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl FolderAssetOptions {
/// Create a new folder asset using the builder
pub const fn new() -> Self {
Self {}
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Folder(self)
}
}
@@ -0,0 +1,225 @@
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// The type of an image. You can read more about the tradeoffs between image formats [here](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types)
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
#[repr(u8)]
pub enum ImageFormat {
/// A png image. Png images cannot contain transparency and tend to compress worse than other formats
Png,
/// A jpg image. Jpg images can contain transparency and tend to compress better than png images
Jpg,
/// A webp image. Webp images can contain transparency and tend to compress better than jpg images
Webp,
/// An avif image. Avif images can compress slightly better than webp images but are not supported by all browsers
Avif,
/// An unknown image type
Unknown,
}
/// The size of an image asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
#[repr(C, u8)]
pub enum ImageSize {
/// A manual size in pixels
Manual {
/// The width of the image in pixels
width: u32,
/// The height of the image in pixels
height: u32,
},
/// The size will be automatically determined from the image source
Automatic,
}
/// Options for an image asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct ImageAssetOptions {
ty: ImageFormat,
low_quality_preview: bool,
size: ImageSize,
preload: bool,
}
impl Default for ImageAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl ImageAssetOptions {
/// Create a new image asset options
pub const fn new() -> Self {
Self {
ty: ImageFormat::Unknown,
low_quality_preview: false,
size: ImageSize::Automatic,
preload: false,
}
}
/// Make the asset preloaded
///
/// Preloading an image will make the image start to load as soon as possible. This is useful for images that will be displayed soon after the page loads or images that may not be visible immediately, but should start loading sooner
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true));
/// ```
pub const fn with_preload(self, preload: bool) -> Self {
Self { preload, ..self }
}
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool {
self.preload
}
/// Sets the format of the image
///
/// Choosing the right format can make your site load much faster. Webp and avif images tend to be a good default for most images
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Webp));
/// ```
pub const fn with_format(self, format: ImageFormat) -> Self {
Self { ty: format, ..self }
}
/// Sets the format of the image to [`ImageFormat::Avif`]
///
/// Avif images tend to be a good default for most images rendered in browser because
/// they compress images well
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_avif());
/// ```
pub const fn with_avif(self) -> Self {
self.with_format(ImageFormat::Avif)
}
/// Sets the format of the image to [`ImageFormat::Webp`]
///
/// Webp images tend to be a good default for most images rendered in browser because
/// they compress images well
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_webp());
/// ```
pub const fn with_webp(self) -> Self {
self.with_format(ImageFormat::Webp)
}
/// Sets the format of the image to [`ImageFormat::Jpg`]
///
/// Jpeg images compress much better than [`ImageFormat::Png`], but worse than [`ImageFormat::Webp`] or [`ImageFormat::Avif`]
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_jpg());
/// ```
pub const fn with_jpg(self) -> Self {
self.with_format(ImageFormat::Jpg)
}
/// Sets the format of the image to [`ImageFormat::Png`]
///
/// Png images don't compress very well, so they are not recommended for large images
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_png());
/// ```
pub const fn with_png(self) -> Self {
self.with_format(ImageFormat::Png)
}
/// Get the format of the image
pub const fn format(&self) -> ImageFormat {
self.ty
}
/// Sets the size of the image
///
/// If you only use the image in one place, you can set the size of the image to the size it will be displayed at. This will make the image load faster
///
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 512, height: 512 }));
/// ```
pub const fn with_size(self, size: ImageSize) -> Self {
Self { size, ..self }
}
/// Get the size of the image
pub const fn size(&self) -> ImageSize {
self.size
}
// LQIP is currently disabled until we have the CLI set up to inject the low quality image preview after the crate is built through the linker
// /// Make the image use a low quality preview
// ///
// /// A low quality preview is a small version of the image that will load faster. This is useful for large images on mobile devices that may take longer to load
// ///
// /// ```rust
// /// # use manganis::{asset, Asset, ImageAssetOptions};
// /// const _: Asset = manganis::asset!("/assets/image.png", ImageAssetOptions::new().with_low_quality_image_preview());
// /// ```
//
// pub const fn with_low_quality_image_preview(self, low_quality_preview: bool) -> Self {
// Self {
// low_quality_preview,
// ..self
// }
// }
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Image(self)
}
pub(crate) const fn extension(&self) -> Option<&'static str> {
match self.ty {
ImageFormat::Png => Some("png"),
ImageFormat::Jpg => Some("jpg"),
ImageFormat::Webp => Some("webp"),
ImageFormat::Avif => Some("avif"),
ImageFormat::Unknown => None,
}
}
}
+77
View File
@@ -0,0 +1,77 @@
use const_serialize::SerializeConst;
use crate::AssetOptions;
/// Options for a javascript asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
pub struct JsAssetOptions {
minify: bool,
preload: bool,
}
impl Default for JsAssetOptions {
fn default() -> Self {
Self::new()
}
}
impl JsAssetOptions {
/// Create a new js asset builder
pub const fn new() -> Self {
Self {
minify: true,
preload: false,
}
}
/// Sets whether the js should be minified (default: true)
///
/// Minifying the js can make your site load faster by loading less data
///
/// ```rust
/// # use manganis::{asset, Asset, JsAssetOptions};
/// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_minify(false));
/// ```
#[allow(unused)]
pub const fn with_minify(self, minify: bool) -> Self {
Self { minify, ..self }
}
/// Check if the asset is minified
pub const fn minified(&self) -> bool {
self.minify
}
/// Make the asset preloaded
///
/// Preloading the javascript will make the javascript start to load as soon as possible. This is useful for javascript that will be used soon after the page loads or javascript that may not be used immediately, but should start loading sooner
///
/// ```rust
/// # use manganis::{asset, Asset, JsAssetOptions};
/// const _: Asset = asset!("/assets/script.js", JsAssetOptions::new().with_preload(true));
/// ```
#[allow(unused)]
pub const fn with_preload(self, preload: bool) -> Self {
Self { preload, ..self }
}
/// Check if the asset is preloaded
pub const fn preloaded(&self) -> bool {
self.preload
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> AssetOptions {
AssetOptions::Js(self)
}
}
+17 -3
View File
@@ -1,5 +1,19 @@
mod asset;
mod linker;
mod folder;
pub use folder::*;
mod images;
pub use images::*;
mod options;
pub use options::*;
mod css;
pub use css::*;
mod js;
pub use js::*;
mod asset;
pub use asset::*;
pub use linker::*;
pub mod linker;
@@ -1,3 +1,5 @@
//! Utilities for working with Manganis assets in the linker. This module defines [`LinkSection`] which has information about what section manganis assets are stored in on each platform.
/// Information about the manganis link section for a given platform
#[derive(Debug, Clone, Copy)]
pub struct LinkSection {
@@ -0,0 +1,48 @@
use const_serialize::SerializeConst;
use crate::{CssAssetOptions, FolderAssetOptions, ImageAssetOptions, JsAssetOptions};
/// Settings for a generic asset
#[derive(
Debug,
PartialEq,
PartialOrd,
Clone,
Copy,
Hash,
SerializeConst,
serde::Serialize,
serde::Deserialize,
)]
#[repr(C, u8)]
#[non_exhaustive]
pub enum AssetOptions {
/// An image asset
Image(ImageAssetOptions),
/// A folder asset
Folder(FolderAssetOptions),
/// A css asset
Css(CssAssetOptions),
/// A javascript asset
Js(JsAssetOptions),
/// An unknown asset
Unknown,
}
impl AssetOptions {
/// Try to get the extension for the asset. If the asset options don't define an extension, this will return None
pub const fn extension(&self) -> Option<&'static str> {
match self {
AssetOptions::Image(image) => image.extension(),
AssetOptions::Css(_) => Some("css"),
AssetOptions::Js(_) => Some("js"),
AssetOptions::Folder(_) => None,
AssetOptions::Unknown => None,
}
}
/// Convert the options into options for a generic asset
pub const fn into_asset_options(self) -> Self {
self
}
}
+3 -2
View File
@@ -18,9 +18,10 @@ proc-macro = true
proc-macro2 = { version = "1.0" }
quote = "1.0"
syn = { version = "2.0", features = ["full", "extra-traits"] }
serde_json = "1.0"
serde = { workspace = true, features = ["derive"] }
manganis-core = { workspace = true }
[features]
default = []
[dev-dependencies]
manganis.workspace = true
Whitespace-only changes.
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Whitespace-only changes.
Whitespace-only changes.
+155 -51
View File
@@ -1,14 +1,107 @@
use manganis_core::{AssetParseError, ResourceAsset};
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens, TokenStreamExt};
use std::{
hash::Hasher,
io::Read,
path::{Path, PathBuf},
};
use syn::{
parse::{Parse, ParseStream},
LitStr,
LitStr, Token,
};
fn resolve_path(raw: &str) -> Result<PathBuf, AssetParseError> {
// Get the location of the root of the crate which is where all assets are relative to
//
// IE
// /users/dioxus/dev/app/
// is the root of
// /users/dioxus/dev/app/assets/blah.css
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap();
// 1. the input file should be a pathbuf
let input = PathBuf::from(raw);
// 2. absolute path to the asset
manifest_dir
.join(raw.trim_start_matches('/'))
.canonicalize()
.map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: input.clone(),
})
}
fn hash_file_contents(file_path: &Path) -> Result<u64, AssetParseError> {
// Create a hasher
let mut hash = std::collections::hash_map::DefaultHasher::new();
// If this is a folder, hash the folder contents
if file_path.is_dir() {
let files =
std::fs::read_dir(file_path).map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: file_path.to_path_buf(),
})?;
for file in files.flatten() {
let path = file.path();
hash_file_contents(&path)?;
}
return Ok(hash.finish());
}
// Otherwise, open the file to get its contents
let mut file =
std::fs::File::open(file_path).map_err(|err| AssetParseError::AssetDoesntExist {
err,
path: file_path.to_path_buf(),
})?;
// We add a hash to the end of the file so it is invalidated when the bundled version of the file changes
// The hash includes the file contents, the options, and the version of manganis. From the macro, we just
// know the file contents, so we only include that hash
let mut buffer = [0; 8192];
loop {
let read = file
.read(&mut buffer)
.map_err(AssetParseError::FailedToReadAsset)?;
if read == 0 {
break;
}
hash.write(&buffer[..read]);
}
Ok(hash.finish())
}
#[derive(Debug)]
pub(crate) enum AssetParseError {
AssetDoesntExist {
err: std::io::Error,
path: std::path::PathBuf,
},
FailedToReadAsset(std::io::Error),
}
impl std::fmt::Display for AssetParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AssetParseError::AssetDoesntExist { err, path } => {
write!(f, "Asset at {} doesn't exist: {}", path.display(), err)
}
AssetParseError::FailedToReadAsset(err) => write!(f, "Failed to read asset: {}", err),
}
}
}
pub struct AssetParser {
/// The span of the source string
path_span: proc_macro2::Span,
/// The asset itself
asset: Result<ResourceAsset, AssetParseError>,
asset: Result<PathBuf, AssetParseError>,
/// The source of the trailing options
options: TokenStream2,
@@ -26,8 +119,8 @@ impl Parse for AssetParser {
// ```
// asset!(
// "/assets/myfile.png",
// asset::image()
// .format(ImageType::Jpg)
// ImageAssetOptions::new()
// .format(ImageFormat::Jpg)
// .size(512, 512)
// )
// ```
@@ -36,29 +129,27 @@ impl Parse for AssetParser {
fn parse(input: ParseStream) -> syn::Result<Self> {
// And then parse the options
let src = input.parse::<LitStr>()?;
let asset = ResourceAsset::parse_any(&src.value());
let path_span = src.span();
let asset = resolve_path(&src.value());
let _comma = input.parse::<Token![,]>();
let options = input.parse()?;
Ok(Self { asset, options })
Ok(Self {
path_span,
asset,
options,
})
}
}
impl ToTokens for AssetParser {
// Need to generate:
//
// - 1. absolute file path on the user's system: `/users/dioxus/dev/project/assets/blah.css`
// - 2. original input in case that's useful: `../blah.css`
// - 3. path relative to the CARGO_MANIFEST_DIR - and then we'll add a `/`: `/assets/blah.css
// - 4. file from which this macro was called: `/users/dioxus/dev/project/src/lib.rs`
// - 5: The link section containing all this data
// - 6: the input tokens such that the builder gets validated by the const code
// - 7: the bundled name `/blahcss123.css`
//
// Not that we'll use everything, but at least we have this metadata for more post-processing.
//
// For now, `2` and `3` will be the same since we don't support relative paths... a bit of
// a limitation from rust itself. We technically could support them but not without some hoops
// to jump through
// The manganis macro outputs info to two different places:
// 1) The crate the macro was invoked in
// - It needs the hashed contents of the file, the file path, and the file options
// - Most of this is just forwarding the input, the only thing that the macro needs to do is hash the file contents
// 2) A bundler that supports manganis (currently just dioxus-cli)
// - The macro needs to output the absolute path to the asset for the bundler to find later
// - It also needs to serialize the bundled asset along with the asset options for the bundler to use later
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let asset = match self.asset.as_ref() {
Ok(asset) => asset,
@@ -68,47 +159,60 @@ impl ToTokens for AssetParser {
return;
}
};
let asset_str = asset.display().to_string();
let mut asset_str = proc_macro2::Literal::string(&asset_str);
asset_str.set_span(self.path_span);
// 1. the link section itself
let link_section = crate::generate_link_section(&asset);
let hash = match hash_file_contents(asset) {
Ok(hash) => hash,
Err(err) => {
let err = err.to_string();
tokens.append_all(quote! { compile_error!(#err) });
return;
}
};
// 2. original
let input = asset.input.display().to_string();
// 3. resolved on the user's system
let local = asset.absolute.display().to_string();
// 4. bundled
let bundled = asset.bundled.to_string();
// 5. source tokens
let option_source = &self.options;
// Generate the link section for the asset
// The link section includes the source path and the output path of the asset
let link_section = crate::generate_link_section(quote!(__ASSET));
// generate the asset::new method to deprecate the `./assets/blah.css` syntax
let method = if !asset.input.starts_with("/") {
let constructor = if asset.is_relative() {
quote::quote! { new_relative }
} else {
quote::quote! { new }
};
let options = if self.options.is_empty() {
quote! { manganis::AssetOptions::Unknown }
} else {
self.options.clone()
};
tokens.extend(quote! {
Asset::#method(
{
#link_section
manganis::Asset {
// "/assets/blah.css"
input: #input,
{
// We keep a hash of the contents of the asset for cache busting
const __ASSET_HASH: u64 = #hash;
// The source is used by the CLI to copy the asset
const __ASSET_SOURCE_PATH: &'static str = #asset_str;
// The options give the CLI info about how to process the asset
// Note: into_asset_options is not a trait, so we cannot accept the options directly
// in the constructor. Stable rust doesn't have support for constant functions in traits
const __ASSET_OPTIONS: manganis::AssetOptions = #options.into_asset_options();
// We calculate the bundled path from the hash and any transformations done by the options
// This is the final path that the asset will be written to
const __ASSET_BUNDLED_PATH: manganis::macro_helpers::const_serialize::ConstStr = manganis::macro_helpers::generate_unique_path(__ASSET_SOURCE_PATH, __ASSET_HASH, &__ASSET_OPTIONS);
// Get the reference to the string that was generated. We cannot return &'static str from
// generate_unique_path because it would return a reference to data generated in the function
const __ASSET_BUNDLED_PATH_STR: &'static str = __ASSET_BUNDLED_PATH.as_str();
// Create the asset that the crate will use. This is used both in the return value and
// added to the linker for the bundler to copy later
const __ASSET: manganis::BundledAsset = manganis::BundledAsset::#constructor(__ASSET_SOURCE_PATH, __ASSET_BUNDLED_PATH_STR, __ASSET_OPTIONS);
// "/users/dioxus/dev/app/assets/blah.css"
local: #local,
#link_section
// "/blahcss123.css"
bundled: #bundled,
metadata: __volatile_reader as fn() -> u8,
}
}
) #option_source
manganis::Asset::new(__ASSET, __keep_link_section)
}
})
}
}
@@ -11,6 +11,42 @@ pub(crate) mod linker;
use linker::generate_link_section;
/// The asset macro collects assets that will be included in the final binary
///
/// # Files
///
/// The file builder collects an arbitrary file. Relative paths are resolved relative to the package root
/// ```rust
/// # use manganis::{asset, Asset};
/// const _: Asset = asset!("/assets/asset.txt");
/// ```
/// Or you can use URLs to read the asset at build time from a remote location
/// ```rust
/// # use manganis::{asset, Asset};
/// const _: Asset = asset!("/assets/image.png");
/// ```
///
/// # Images
///
/// You can collect images which will be automatically optimized with the image builder:
/// ```rust
/// # use manganis::{asset, Asset};
/// const _: Asset = asset!("/assets/image.png");
/// ```
/// Resize the image at compile time to make the assets file size smaller:
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 52, height: 52 }));
/// ```
/// Or convert the image at compile time to a web friendly format:
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions, ImageSize, ImageFormat};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Avif));
/// ```
/// You can mark images as preloaded to make them load faster in your app
/// ```rust
/// # use manganis::{asset, Asset, ImageAssetOptions};
/// const _: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_preload(true));
/// ```
#[proc_macro]
pub fn asset(input: TokenStream) -> TokenStream {
let asset = parse_macro_input!(input as asset::AssetParser);
+21 -13
View File
@@ -1,25 +1,33 @@
use proc_macro2::TokenStream as TokenStream2;
use serde::Serialize;
use quote::ToTokens;
/// this new approach will store the assets descriptions *inside the executable*.
/// The trick is to use the `link_section` attribute.
/// We force rust to store a json representation of the asset description
/// We store description of the assets an application uses in the executable.
/// We use the `link_section` attribute embed an extra section in the executable.
/// We force rust to store a serialized representation of the asset description
/// inside a particular region of the binary, with the label "manganis".
/// After linking, the "manganis" sections of the different executables will be merged.
pub fn generate_link_section(asset: &impl Serialize) -> TokenStream2 {
/// After linking, the "manganis" sections of the different object files will be merged.
pub fn generate_link_section(asset: impl ToTokens) -> TokenStream2 {
let position = proc_macro2::Span::call_site();
let asset_description = serde_json::to_string(asset).unwrap();
let len = asset_description.as_bytes().len();
let asset_bytes = syn::LitByteStr::new(asset_description.as_bytes(), position);
let section_name = syn::LitStr::new(manganis_core::LinkSection::CURRENT.link_section, position);
let section_name = syn::LitStr::new(
manganis_core::linker::LinkSection::CURRENT.link_section,
position,
);
quote::quote! {
// First serialize the asset into a constant sized buffer
const __BUFFER: manganis::macro_helpers::const_serialize::ConstVec<u8> = manganis::macro_helpers::serialize_asset(&#asset);
// Then pull out the byte slice
const __BYTES: &[u8] = __BUFFER.as_ref();
// And the length of the byte slice
const __LEN: usize = __BYTES.len();
// Now that we have the size of the asset, copy the bytes into a static array
#[link_section = #section_name]
#[used]
static ASSET: [u8; #len] = * #asset_bytes;
static __LINK_SECTION: [u8; __LEN] = manganis::macro_helpers::copy_bytes(__BYTES);
fn __volatile_reader() -> u8 {
unsafe { std::ptr::read_volatile(ASSET.as_ptr()) }
fn __keep_link_section() -> u8 {
unsafe { std::ptr::read_volatile(__LINK_SECTION.as_ptr()) }
}
}
}
+5 -24
View File
@@ -13,29 +13,10 @@ keywords = ["assets"]
[lib]
[dependencies]
manganis-macro = { workspace = true, optional = true }
dioxus-core-types = { workspace = true }
# dunce = "1.0.2"
# [target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
# core-foundation = "0.10.0"
# [target.'cfg(target_os = "macos")'.dependencies]
# core-foundation = "0.9.3"
# [target.'cfg(target_os ="macos")'.dependencies]
# infer = { workspace = true }
# dirs = "5.0.1"
# infer = { workspace = true }
# manganis-common = { workspace = true }
once_cell = "1.19.0"
dunce = "1.0.2"
serde = { version = "1.0.183", features = ["derive"] }
anyhow = "1"
base64 = { workspace = true }
const-serialize = { workspace = true }
manganis-core = { workspace = true }
manganis-macro = { workspace = true }
[features]
default = ["macro"]
html = []
# url-encoding = ["manganis-macro/url-encoding"]
macro = ["dep:manganis-macro"]
default = []
dioxus = ["manganis-core/dioxus"]
+31
View File
@@ -0,0 +1,31 @@
# Manganis
The Manganis allows you to submit assets to any build tool that supports collecting assets. It makes it easy to self-host assets that are distributed throughout your libraries. Manganis also handles optimizing, converting, and fetching assets.
If you defined this in a component library:
```rust
use manganis::{Asset, asset};
const AVIF_ASSET: Asset = manganis::asset!("/assets/image.png");
```
AVIF_ASSET will be set to a new file name that will be served by some CLI. That file can be collected by any package that depends on the component library.
```rust
use manganis::{ImageFormat, ImageAssetOptions, Asset, asset, ImageSize};
// You can collect arbitrary files. Absolute paths are resolved relative to the package root
const _: Asset = asset!("/assets/script.js");
// You can collect images which will be automatically optimized
pub const PNG_ASSET: Asset =
asset!("/assets/image.png");
// Resize the image at compile time to make the assets smaller
pub const RESIZED_PNG_ASSET: Asset =
asset!("/assets/image.png", ImageAssetOptions::new().with_size(ImageSize::Manual { width: 52, height: 52 }));
// Or convert the image at compile time to a web friendly format
pub const AVIF_ASSET: Asset = asset!("/assets/image.png", ImageAssetOptions::new().with_format(ImageFormat::Avif));
```
## Adding Support to Your CLI
To add support for your CLI, you need to integrate with the [manganis_cli_support](https://github.com/DioxusLabs/manganis/tree/main/cli-support) crate. This crate provides utilities to collect assets that integrate with the Manganis macro. It makes it easy to integrate an asset collection and optimization system into a build tool.
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Whitespace-only changes.
Whitespace-only changes.
-87
View File
@@ -1,87 +0,0 @@
use dioxus_core_types::DioxusFormattable;
use std::path::PathBuf;
/// Asset
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)]
pub struct Asset {
/// The input URI given to the macro
pub input: &'static str,
/// The absolute path to the asset on the filesystem
pub local: &'static str,
/// The asset location after its been bundled
///
/// `blah-123.css``
pub bundled: &'static str,
/// The metadata for this asset plumbed by the compiler
pub metadata: fn() -> u8,
}
impl Asset {
/// Create a new asset
pub const fn new(self) -> Self {
self
}
/// Create a new asset but with a relative path
///
/// This method is deprecated and will be removed in a future release.
#[deprecated(
note = "Relative asset!() paths are not supported. Use a path like `/assets/myfile.png` instead of `./assets/myfile.png`"
)]
pub const fn new_relative(self) -> Self {
self
}
/// Get the path to the asset
pub fn path(&self) -> PathBuf {
PathBuf::from(self.input.to_string())
}
/// Get the path to the asset
pub fn relative_path(&self) -> PathBuf {
PathBuf::from(self.input.trim_start_matches('/').to_string())
}
/// Return a canonicalized path to the asset
///
/// Attempts to resolve it against an `assets` folder in the current directory.
/// If that doesn't exist, it will resolve against the cargo manifest dir
pub fn resolve(&self) -> PathBuf {
// Force a volatile read of the metadata to ensure the symbol makes it into the binary
(self.metadata)();
// If the asset is relative, we resolve the asset at the current directory
if !dioxus_core_types::is_bundled_app() {
return PathBuf::from(self.local);
}
// Otherwise presumably we're bundled and we can use the bundled path
PathBuf::from("/assets/").join(PathBuf::from(self.bundled.trim_start_matches('/')))
}
}
impl From<Asset> for String {
fn from(value: Asset) -> Self {
value.to_string()
}
}
impl From<Asset> for Option<String> {
fn from(value: Asset) -> Self {
Some(value.to_string())
}
}
impl std::fmt::Display for Asset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.resolve().display())
}
}
impl DioxusFormattable for Asset {
fn format(&self) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Owned(self.to_string())
}
}
-31
View File
@@ -1,31 +0,0 @@
use crate::Asset;
/// This is basically a compile-time version of ResourceAsset
/// A struct that contains the relative and absolute paths of an asset
#[derive(Debug, PartialEq, PartialOrd, Clone, Hash)]
pub struct FolderAsset {
src: Asset,
}
impl Asset {
/// Create a new folder using `Asset` as the base type
pub const fn folder(self) -> FolderAsset {
FolderAsset::new(self)
}
}
impl FolderAsset {
/// Create a new folder asset from an `Asset`
pub const fn new(src: Asset) -> Self {
Self { src }
}
}
/// The builder for [`FolderAsset`]
pub struct FolderAssetBuilder;
/// Create an folder asset from the local path
#[allow(unused)]
pub const fn folder(src: &'static str) -> FolderAssetBuilder {
FolderAssetBuilder {}
}
+39
View File
@@ -0,0 +1,39 @@
use const_serialize::{serialize_const, ConstVec, SerializeConst};
// From rustchash - https://github.com/rust-lang/rustc-hash/blob/6745258da00b7251bed4a8461871522d0231a9c7/src/lib.rs#L98
const K: u64 = 0xf1357aea2e62a9c5;
pub(crate) struct ConstHasher {
hash: u64,
}
impl ConstHasher {
pub const fn new() -> Self {
Self { hash: 0 }
}
pub const fn finish(&self) -> u64 {
self.hash
}
pub const fn write(mut self, bytes: &[u8]) -> Self {
let mut i = 0;
while i < bytes.len() {
self = self.write_byte(bytes[i]);
i += 1;
}
self
}
pub const fn write_byte(mut self, byte: u8) -> Self {
self.hash = self.hash.wrapping_add(byte as u64).wrapping_mul(K);
self
}
pub const fn hash_by_bytes<T: SerializeConst>(self, item: &T) -> Self {
let mut bytes = ConstVec::new();
bytes = serialize_const(item, bytes);
let bytes = bytes.as_ref();
self.write(bytes)
}
}
-141
View File
@@ -1,141 +0,0 @@
use dioxus_core_types::DioxusFormattable;
use crate::Asset;
/// An image asset that is built by the [`asset!`] macro
#[derive(Debug, PartialEq, PartialOrd, Clone, Hash, Copy)]
pub struct ImageAsset {
/// The path to the image
asset: Asset,
/// A low quality preview of the image that is URL encoded
preview: Option<&'static str>,
/// A caption for the image
caption: Option<&'static str>,
/// The format of the image
format: Option<ImageType>,
}
impl Asset {
/// Convert this asset into an image asset
pub const fn image(self) -> ImageAsset {
ImageAsset::new(self)
}
}
impl ImageAsset {
/// Creates a new image asset
pub const fn new(path: Asset) -> Self {
Self {
asset: path,
preview: None,
caption: None,
format: None,
}
}
/// Returns the path to the image
pub const fn path(&self) -> &'static str {
self.asset.bundled
}
/// Returns the preview of the image
pub const fn preview(&self) -> Option<&'static str> {
self.preview
}
/// Sets the preview of the image
pub const fn with_preview(self, preview: Option<&'static str>) -> Self {
Self { preview, ..self }
}
/// Returns the caption of the image
pub const fn caption(&self) -> Option<&'static str> {
self.caption
}
/// Sets the caption of the image
pub const fn with_caption(self, caption: Option<&'static str>) -> Self {
Self { caption, ..self }
}
/// Sets the format of the image
pub const fn format(self, format: Option<ImageType>) -> Self {
Self { format, ..self }
}
}
impl std::ops::Deref for ImageAsset {
type Target = Asset;
fn deref(&self) -> &Self::Target {
&self.asset
}
}
impl std::fmt::Display for ImageAsset {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.asset.fmt(f)
}
}
impl DioxusFormattable for ImageAsset {
fn format(&self) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Owned(self.to_string())
}
}
/// The type of an image. You can read more about the tradeoffs between image formats [here](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Image_types)
#[derive(Debug, PartialEq, PartialOrd, Clone, Copy, Hash)]
pub enum ImageType {
/// A png image. Png images cannot contain transparency and tend to compress worse than other formats
Png,
/// A jpg image. Jpg images can contain transparency and tend to compress better than png images
Jpg,
/// A webp image. Webp images can contain transparency and tend to compress better than jpg images
Webp,
/// An avif image. Avif images can compress slightly better than webp images but are not supported by all browsers
Avif,
}
/// A builder for an image asset. This must be used in the [`asset!`] macro.
///
/// > **Note**: This will do nothing outside of the `asset!` macro
pub struct ImageAssetBuilder;
impl ImageAssetBuilder {
/// Sets the format of the image
#[allow(unused)]
pub const fn format(self, format: ImageType) -> Self {
Self
}
/// Sets the size of the image
#[allow(unused)]
pub const fn size(self, x: u32, y: u32) -> Self {
Self
}
/// Make the image use a low quality preview
#[allow(unused)]
pub const fn low_quality_preview(self) -> Self {
Self
}
/// Make the image preloaded
#[allow(unused)]
pub const fn preload(self) -> Self {
Self
}
/// Make the image URL encoded
#[allow(unused)]
pub const fn url_encoded(self) -> Self {
Self
}
}
/// Create an image asset from the local path or url to the image
#[allow(unused)]
pub const fn image(path: &'static str) -> ImageAssetBuilder {
ImageAssetBuilder
}
+9 -12
View File
@@ -1,15 +1,12 @@
//! Manganis is a Rust library for bundling assets into a final binary.
#![doc = include_str!("../README.md")]
#![deny(missing_docs)]
#[cfg(feature = "macro")]
pub use manganis_macro::*;
mod hash;
#[doc(hidden)]
pub mod macro_helpers;
pub use manganis_macro::asset;
mod folder;
pub use folder::*;
mod images;
pub use images::*;
mod builder;
pub use builder::*;
pub use manganis_core::{
Asset, AssetOptions, BundledAsset, CssAssetOptions, FolderAssetOptions, ImageAssetOptions,
ImageFormat, ImageSize, JsAssetOptions,
};
@@ -0,0 +1,144 @@
pub use const_serialize;
use const_serialize::{serialize_const, ConstStr, ConstVec};
use manganis_core::{AssetOptions, BundledAsset};
use crate::hash::ConstHasher;
/// Format the input path with a hash to create an unique output path for the macro in the form `{input_path}-{hash}.{extension}`
pub const fn generate_unique_path(
input_path: &str,
content_hash: u64,
asset_config: &AssetOptions,
) -> ConstStr {
// Format the unique path with the format `{input_path}-{hash}.{extension}`
// Start with the input path
let mut input_path = ConstStr::new(input_path);
// Then strip the prefix from the input path. The path comes from the build platform, but
// in wasm, we don't know what the path separator is from the build platform. We need to
// split by both unix and windows paths and take the smallest one
let mut extension = None;
match (input_path.rsplit_once('/'), input_path.rsplit_once('\\')) {
(Some((_, unix_new_input_path)), Some((_, windows_new_input_path))) => {
input_path = if unix_new_input_path.len() < windows_new_input_path.len() {
unix_new_input_path
} else {
windows_new_input_path
};
}
(Some((_, unix_new_input_path)), _) => {
input_path = unix_new_input_path;
}
(_, Some((_, windows_new_input_path))) => {
input_path = windows_new_input_path;
}
_ => {}
}
if let Some((new_input_path, new_extension)) = input_path.rsplit_once('.') {
extension = Some(new_extension);
input_path = new_input_path;
}
// Then add a dash
let mut macro_output_path = input_path.push_str("-");
// Hash the contents along with the asset config to create a unique hash for the asset
// When this hash changes, the client needs to re-fetch the asset
let mut hasher = ConstHasher::new();
hasher = hasher.write(&content_hash.to_le_bytes());
hasher = hasher.hash_by_bytes(asset_config);
let hash = hasher.finish();
// Then add the hash in hex form
let hash_bytes = hash.to_le_bytes();
let mut i = 0;
while i < hash_bytes.len() {
let byte = hash_bytes[i];
let first = byte >> 4;
let second = byte & 0x0f;
const fn byte_to_char(byte: u8) -> char {
match char::from_digit(byte as u32, 16) {
Some(c) => c,
None => panic!("byte must be a valid digit"),
}
}
macro_output_path = macro_output_path.push(byte_to_char(first));
macro_output_path = macro_output_path.push(byte_to_char(second));
i += 1;
}
// Finally add the extension
match asset_config.extension() {
Some(extension) => {
macro_output_path = macro_output_path.push('.');
macro_output_path = macro_output_path.push_str(extension)
}
None => {
if let Some(extension) = extension {
macro_output_path = macro_output_path.push('.');
macro_output_path = macro_output_path.push_str(extension.as_str())
}
}
}
macro_output_path
}
#[test]
fn test_unique_path() {
use manganis_core::{ImageAssetOptions, ImageFormat};
use std::path::PathBuf;
let mut input_path = PathBuf::from("some");
input_path.push("prefix");
input_path.push("test.png");
let content_hash = 123456789;
let asset_config = AssetOptions::Image(ImageAssetOptions::new().with_format(ImageFormat::Avif));
let output_path =
generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config);
assert_eq!(output_path.as_str(), "test-603a88fe296462a3.avif");
// Changing the path without changing the contents shouldn't change the hash
let mut input_path = PathBuf::from("some");
input_path.push("prefix");
input_path.push("prefix");
input_path.push("test.png");
let content_hash = 123456789;
let asset_config = AssetOptions::Image(ImageAssetOptions::new().with_format(ImageFormat::Avif));
let output_path =
generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config);
assert_eq!(output_path.as_str(), "test-603a88fe296462a3.avif");
let mut input_path = PathBuf::from("test");
input_path.push("ing");
input_path.push("test");
let content_hash = 123456789;
let asset_config = AssetOptions::Unknown;
let output_path =
generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config);
assert_eq!(output_path.as_str(), "test-c8c4cfad21cac262");
// Just changing the content hash should change the total hash
let mut input_path = PathBuf::from("test");
input_path.push("ing");
input_path.push("test");
let content_hash = 123456780;
let asset_config = AssetOptions::Unknown;
let output_path =
generate_unique_path(&input_path.to_string_lossy(), content_hash, &asset_config);
assert_eq!(output_path.as_str(), "test-7bced03789ff865c");
}
/// Serialize an asset to a const buffer
pub const fn serialize_asset(asset: &BundledAsset) -> ConstVec<u8> {
let write = ConstVec::new();
serialize_const(asset, write)
}
/// Copy a slice into a constant sized buffer at compile time
pub const fn copy_bytes<const N: usize>(bytes: &[u8]) -> [u8; N] {
let mut out = [0; N];
let mut i = 0;
while i < N {
out[i] = bytes[i];
i += 1;
}
out
}