Skip to content

Commit 72b4226

Browse files
authored
feat: reduce Debug format size for binary buffers (#13809)
* feat: reduce Image debug output For example now: `Image { rgba: Cow::Borrowed([u8; 4096]), width: 32, height: 32 }) }` * feat: reduce EmbeddedAssets debug size For example now: ``` EmbeddedAssets { assets: { "/index.html": [u8; 1835], "/index.js": [u8; 212], }, global_hashes: [ Script( "'sha256-EOd6N98xxmK5s7VvxV7W2w7YG+dmP52MqNiZUq1NLeE='", ), Style( "'sha256-YEercZJImS+vUX2bz7vkQ0aA4rtBIPLuCEWz+yraQ/g='", ), ], html_hashes: { "/index.html": [ Script( "'sha256-3g8CfFrjFLGpwD2o+hwMt+lh/hsHbQ3XY+EPJ35fFKk='", ), Script( "'sha256-EOd6N98xxmK5s7VvxV7W2w7YG+dmP52MqNiZUq1NLeE='", ), ], }, } ``` * feat: reduce `app_icon` debug size * chore: changelog * chore: include tauri-utils in changelog * doc: comment had extra closing brackets [skip ci]
1 parent d6d941c commit 72b4226

File tree

5 files changed

+85
-6
lines changed

5 files changed

+85
-6
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri": patch:enhance
3+
"tauri-utils": patch:enhance
4+
---
5+
6+
Reduced `Debug` format size for binary buffers.

crates/tauri-utils/src/assets.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ impl CspHash<'_> {
113113
}
114114

115115
/// [`Assets`] implementation that only contains compile-time compressed and embedded assets.
116-
#[derive(Debug)]
117116
pub struct EmbeddedAssets {
118117
assets: phf::Map<&'static str, &'static [u8]>,
119118
// Hashes that must be injected to the CSP of every HTML file.
@@ -122,6 +121,36 @@ pub struct EmbeddedAssets {
122121
html_hashes: phf::Map<&'static str, &'static [CspHash<'static>]>,
123122
}
124123

124+
/// Temporary struct that overrides the Debug formatting for the `assets` field.
125+
///
126+
/// It reduces the output size compared to the default, as that would format the binary
127+
/// data as a slice of numbers like `[65, 66, 67]` for "ABC". This instead shows the length
128+
/// of the slice.
129+
///
130+
/// For example: `{"/index.html": [u8; 1835], "/index.js": [u8; 212]}`
131+
struct DebugAssetMap<'a>(&'a phf::Map<&'static str, &'static [u8]>);
132+
133+
impl std::fmt::Debug for DebugAssetMap<'_> {
134+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
135+
let mut map = f.debug_map();
136+
for (k, v) in self.0.entries() {
137+
map.key(k);
138+
map.value(&format_args!("[u8; {}]", v.len()));
139+
}
140+
map.finish()
141+
}
142+
}
143+
144+
impl std::fmt::Debug for EmbeddedAssets {
145+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
146+
f.debug_struct("EmbeddedAssets")
147+
.field("assets", &DebugAssetMap(&self.assets))
148+
.field("global_hashes", &self.global_hashes)
149+
.field("html_hashes", &self.html_hashes)
150+
.finish()
151+
}
152+
}
153+
125154
impl EmbeddedAssets {
126155
/// Creates a new instance from the given asset map and script hash list.
127156
pub const fn new(

crates/tauri/src/image/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,37 @@ use std::sync::Arc;
1212
use crate::{Resource, ResourceId, ResourceTable};
1313

1414
/// An RGBA Image in row-major order from top to bottom.
15-
#[derive(Debug, Clone)]
15+
#[derive(Clone)]
1616
pub struct Image<'a> {
1717
rgba: Cow<'a, [u8]>,
1818
width: u32,
1919
height: u32,
2020
}
2121

22+
impl std::fmt::Debug for Image<'_> {
23+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24+
f.debug_struct("Image")
25+
.field(
26+
"rgba",
27+
// Reduces the debug size compared to the derived default, as the default
28+
// would format the raw bytes as numbers `[0, 0, 0, 0]` for 1 pixel.
29+
// The custom format doesn't grow as much with larger images:
30+
// `Image { rgba: Cow::Borrowed([u8; 4096]), width: 32, height: 32 }`
31+
&format_args!(
32+
"Cow::{}([u8; {}])",
33+
match &self.rgba {
34+
Cow::Borrowed(_) => "Borrowed",
35+
Cow::Owned(_) => "Owned",
36+
},
37+
self.rgba.len()
38+
),
39+
)
40+
.field("width", &self.width)
41+
.field("height", &self.height)
42+
.finish()
43+
}
44+
}
45+
2246
impl Resource for Image<'static> {}
2347

2448
impl Image<'static> {

crates/tauri/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,32 @@ pub struct Context<R: Runtime> {
397397
pub(crate) plugin_global_api_scripts: Option<&'static [&'static str]>,
398398
}
399399

400+
/// Temporary struct that overrides the Debug formatting for the `app_icon` field.
401+
///
402+
/// It reduces the output size compared to the default, as that would format the binary
403+
/// data as a slice of numbers `[65, 66, 67]`. This instead shows the length of the Vec.
404+
///
405+
/// For example: `Some([u8; 493])`
406+
pub(crate) struct DebugAppIcon<'a>(&'a Option<Vec<u8>>);
407+
408+
impl std::fmt::Debug for DebugAppIcon<'_> {
409+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
410+
match self.0 {
411+
Option::None => f.write_str("None"),
412+
Option::Some(icon) => f
413+
.debug_tuple("Some")
414+
.field(&format_args!("[u8; {}]", icon.len()))
415+
.finish(),
416+
}
417+
}
418+
}
419+
400420
impl<R: Runtime> fmt::Debug for Context<R> {
401421
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
402422
let mut d = f.debug_struct("Context");
403423
d.field("config", &self.config)
404424
.field("default_window_icon", &self.default_window_icon)
405-
.field("app_icon", &self.app_icon)
425+
.field("app_icon", &DebugAppIcon(&self.app_icon))
406426
.field("package_info", &self.package_info)
407427
.field("pattern", &self.pattern)
408428
.field("plugin_global_api_scripts", &self.plugin_global_api_scripts);

crates/tauri/src/manager/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use tauri_utils::{
1818
config::{Csp, CspDirectiveSources},
1919
};
2020

21-
use crate::resources::ResourceTable;
2221
use crate::{
2322
app::{
2423
AppHandle, ChannelInterceptor, GlobalWebviewEventListener, GlobalWindowEventListener,
@@ -27,8 +26,9 @@ use crate::{
2726
event::{EmitArgs, Event, EventId, EventTarget, Listeners},
2827
ipc::{Invoke, InvokeHandler, RuntimeAuthority},
2928
plugin::PluginStore,
29+
resources::ResourceTable,
3030
utils::{config::Config, PackageInfo},
31-
Assets, Context, EventName, Pattern, Runtime, StateManager, Webview, Window,
31+
Assets, Context, DebugAppIcon, EventName, Pattern, Runtime, StateManager, Webview, Window,
3232
};
3333

3434
#[cfg(desktop)]
@@ -233,7 +233,7 @@ impl<R: Runtime> fmt::Debug for AppManager<R> {
233233
.field("plugins", &self.plugins)
234234
.field("state", &self.state)
235235
.field("config", &self.config)
236-
.field("app_icon", &self.app_icon)
236+
.field("app_icon", &DebugAppIcon(&self.app_icon))
237237
.field("package_info", &self.package_info)
238238
.field("pattern", &self.pattern);
239239

0 commit comments

Comments
 (0)