Files
2025-09-26 12:35:52 +00:00

26 lines
7.3 KiB
HTML

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Convenient `Display` and other traits for binary data."><title>delog::hex - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Italic-81dc35de.woff2,FiraSans-Regular-0fe48ade.woff2,FiraSans-MediumItalic-ccf7e434.woff2,FiraSans-Medium-e1aa3f0a.woff2,SourceCodePro-Regular-8badfe75.ttf.woff2,SourceCodePro-Semibold-aa29a496.ttf.woff2".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="../../static.files/${f}">`).join(""))</script><link rel="stylesheet" href="../../static.files/normalize-9960930a.css"><link rel="stylesheet" href="../../static.files/rustdoc-84e720fa.css"><meta name="rustdoc-vars" data-root-path="../../" data-static-root-path="../../static.files/" data-current-crate="delog" data-themes="" data-resource-suffix="" data-rustdoc-version="1.89.0 (29483883e 2025-08-04)" data-channel="1.89.0" data-search-js="search-92309212.js" data-settings-js="settings-5514c975.js" ><script src="../../static.files/storage-4e99c027.js"></script><script defer src="../sidebar-items.js"></script><script defer src="../../static.files/main-fd3af306.js"></script><noscript><link rel="stylesheet" href="../../static.files/noscript-32bb7600.css"></noscript><link rel="alternate icon" type="image/png" href="../../static.files/favicon-32x32-6580c154.png"><link rel="icon" type="image/svg+xml" href="../../static.files/favicon-044be391.svg"></head><body class="rustdoc mod"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle" title="show sidebar"></button></nav><nav class="sidebar"><div class="sidebar-crate"><h2><a href="../../delog/index.html">delog</a><span class="version">0.1.8</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Module hex</a></h2><h3><a href="#structs">Module Items</a></h3><ul class="block"><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#traits" title="Traits">Traits</a></li><li><a href="#functions" title="Functions">Functions</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="../index.html">In crate delog</a></h2></div></div></nav><div class="sidebar-resizer" title="Drag to resize sidebar"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><div class="rustdoc-breadcrumbs"><a href="../index.html">delog</a></div><h1>Module <span>hex</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"><a class="src" href="../../src/delog/hex.rs.html#1-310">Source</a> </span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Convenient <code>Display</code> and other traits for binary data.</p>
<p>Standard Rust uses the <code>fmt::UpperHex</code> and <code>LowerHex</code> traits to implement hexadecimal
representations for use in format strings. For instance,
<code>format_args!("{:02X?}", &amp;[7, 0xA1, 0xFF])</code> produces the string <code>"[07, A1, FF]"</code>.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{:02X?}"</span>, [<span class="number">7u8</span>, <span class="number">0xA1</span>, <span class="number">0xFF</span>].as_ref()), <span class="string">"[07, A1, FF]"</span>);</code></pre></div>
<p>However, this only works for the <code>Debug</code> trait, not <code>Display</code>; needs extra dancing
to pad with leading zeros, and is not very compact when debugging binary data formats.</p>
<p>The idea of this module is to generate newtypes around byte arrays/slices, and implement the <code>fmt</code> traits on these.
In release builds, this is all compiled out and translates to direct instructions for the formatting machinery.</p>
<p>The ide to implement truncated outputs comes from the <code>hex_fmt</code> library, which we refer to
for additional newtypes that can be used.</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>delog::{hex_str, hexstr};
<span class="kw">let </span>data = <span class="kw-2">&amp;</span>[<span class="number">7u8</span>, <span class="number">0xA1</span>, <span class="number">255</span>, <span class="number">0xC7</span>];
<span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{}"</span>, <span class="macro">hexstr!</span>(data)), <span class="string">"07A1FFC7"</span>);
<span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{:x}"</span>, <span class="macro">hexstr!</span>(data)), <span class="string">"07a1ffc7"</span>);
<span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{:2x}"</span>, <span class="macro">hex_str!</span>(data)), <span class="string">"07..c7"</span>);
<span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{:&lt;3x}"</span>, <span class="macro">hex_str!</span>(data)), <span class="string">"07 a1 ff.."</span>);
<span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{:&gt;3x}"</span>, <span class="macro">hex_str!</span>(data)), <span class="string">"..a1 ff c7"</span>);
<span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{}"</span>, <span class="macro">hex_str!</span>(data, <span class="number">2</span>)), <span class="string">"07A1 FFC7"</span>);
<span class="macro">assert_eq!</span>(<span class="macro">format!</span>(<span class="string">"{:&lt;3}"</span>, <span class="macro">hex_str!</span>(data, sep: <span class="string">"|"</span>)), <span class="string">"07|A1|FF.."</span>);</code></pre></div>
</div></details><h2 id="structs" class="section-header">Structs<a href="#structs" class="anchor">§</a></h2><dl class="item-table"><dt><a class="struct" href="struct.HexStr.html" title="struct delog::hex::HexStr">HexStr</a></dt><dd>Zero-sized wrapper newtype, allowing grouping bytes in blocks of N hexadecimals
during formatting.</dd><dt><a class="struct" href="struct.U1.html" title="struct delog::hex::U1">U1</a></dt><dd>A type that represents the integer <code>1</code>.</dd></dl><h2 id="traits" class="section-header">Traits<a href="#traits" class="anchor">§</a></h2><dl class="item-table"><dt><a class="trait" href="trait.Separator.html" title="trait delog::hex::Separator">Separator</a></dt><dd>A type that specifies a separator str.</dd><dt><a class="trait" href="trait.Unsigned.html" title="trait delog::hex::Unsigned">Unsigned</a></dt><dd>A type that specifies an unsigned integer.</dd></dl><h2 id="functions" class="section-header">Functions<a href="#functions" class="anchor">§</a></h2><dl class="item-table"><dt><a class="fn" href="fn.HexStr.html" title="fn delog::hex::HexStr">HexStr</a></dt><dd>Explicitly construct a newtype to format with.</dd></dl></section></div></main></body></html>