]> Untitled Git - bitcoindevkit.org/blob
06bdaa6cd9724c0e1022968d9fc3ed30ebd0b89d
[bitcoindevkit.org] /
1 <!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="Creates a new newtype around a `Hash` type."><title>hash_newtype in bdk_chain::bitcoin::secp256k1::hashes - Rust</title><script>if(window.location.protocol!=="file:")document.head.insertAdjacentHTML("beforeend","SourceSerif4-Regular-6b053e98.ttf.woff2,FiraSans-Regular-0fe48ade.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-46132b98.css"><meta name="rustdoc-vars" data-root-path="../../../../" data-static-root-path="../../../../static.files/" data-current-crate="bdk_chain" data-themes="" data-resource-suffix="" data-rustdoc-version="1.85.0-nightly (6d9f6ae36 2024-12-16)" data-channel="nightly" data-search-js="search-036cda7a.js" data-settings-js="settings-0f613d39.js" ><script src="../../../../static.files/storage-59e33391.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../../../static.files/main-5f194d8c.js"></script><noscript><link rel="stylesheet" href="../../../../static.files/noscript-893ab5e7.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 macro"><!--[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><a class="logo-container" href="../../../../bdk_chain/index.html"><img src="https://github.com/bitcoindevkit/bdk/raw/master/static/bdk.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../../../../bdk_chain/index.html"><img src="https://github.com/bitcoindevkit/bdk/raw/master/static/bdk.png" alt="logo"></a><h2><a href="../../../../bdk_chain/index.html">bdk_<wbr>chain</a><span class="version">0.21.0</span></h2></div><div class="sidebar-elems"><div id="rustdoc-modnav"><h2><a href="index.html">In bdk_<wbr>chain::<wbr>bitcoin::<wbr>secp256k1::<wbr>hashes</a></h2></div></div></nav><div class="sidebar-resizer"></div><main><div class="width-limiter"><rustdoc-search></rustdoc-search><section id="main-content" class="content"><div class="main-heading"><span class="rustdoc-breadcrumbs"><a href="../../../index.html">bdk_chain</a>::<wbr><a href="../../index.html">bitcoin</a>::<wbr><a href="../index.html">secp256k1</a>::<wbr><a href="index.html">hashes</a></span><h1>Macro <span class="macro">hash_newtype</span><button id="copy-path" title="Copy item path to clipboard">Copy item path</button></h1><rustdoc-toolbar></rustdoc-toolbar><span class="sub-heading"></span></div><pre class="rust item-decl"><code>macro_rules! hash_newtype {
2 ($($(#[$($type_attrs:tt)*])* $type_vis:vis struct $newtype:ident($(#[$field_attrs:tt])* $field_vis:vis $hash:path);)+) =&gt; { ... };
3 }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Creates a new newtype around a <a href="https://doc.rust-lang.org/nightly/core/hash/macros/derive.Hash.html" title="derive core::hash::macros::Hash"><code>Hash</code></a> type.</p>
4 <p>The syntax is similar to the usual tuple struct syntax:</p>
5
6 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">hash_newtype!</span> {
7 <span class="doccomment">/// Hash of `Foo`.
8 </span><span class="kw">pub struct </span>MyNewtype(<span class="kw">pub </span>sha256::Hash);
9 }</code></pre></div>
10 <p>You can use any valid visibility specifier in place of <code>pub</code> or you can omit either or both, if
11 you want the type or its field to be private.</p>
12 <p>Whether the hash is reversed or not when displaying depends on the inner type. However you can
13 override it like this:</p>
14
15 <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="macro">hash_newtype!</span> {
16 <span class="attr">#[hash_newtype(backward)]
17 </span><span class="kw">struct </span>MyNewtype(sha256::Hash);
18 }</code></pre></div>
19 <p>This will display the hash backwards regardless of what the inner type does. Use <code>forward</code>
20 instead of <code>backward</code> to force displaying forward.</p>
21 <p>You can add arbitrary doc comments or other attributes to the struct or it’s field. Note that
22 the macro already derives <a href="https://doc.rust-lang.org/nightly/core/marker/trait.Copy.html" title="trait core::marker::Copy"><code>Copy</code></a>, <a href="https://doc.rust-lang.org/nightly/core/clone/trait.Clone.html" title="trait core::clone::Clone"><code>Clone</code></a>, <a href="https://doc.rust-lang.org/nightly/core/cmp/trait.Eq.html" title="trait core::cmp::Eq"><code>Eq</code></a>, <a href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialEq.html" title="trait core::cmp::PartialEq"><code>PartialEq</code></a>,
23 <a href="https://doc.rust-lang.org/nightly/core/hash/trait.Hash.html" title="trait core::hash::Hash"><code>Hash</code></a>, <a href="https://doc.rust-lang.org/nightly/core/cmp/trait.Ord.html" title="trait core::cmp::Ord"><code>Ord</code></a>, <a href="https://doc.rust-lang.org/nightly/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd"><code>PartialOrd</code></a>. With the <code>serde</code> feature on, this also adds
24 <code>Serialize</code> and <code>Deserialize</code> implementations.</p>
25 <p>You can also define multiple newtypes within one macro call:</p>
26
27 <div class="example-wrap"><pre class="rust rust-example-rendered"><code>
28 <span class="macro">hash_newtype!</span> {
29 <span class="doccomment">/// My custom type 1
30 </span><span class="kw">pub struct </span>Newtype1(sha256::Hash);
31
32 <span class="doccomment">/// My custom type 2
33 </span><span class="kw">struct </span>Newtype2(hash160::Hash);
34 }</code></pre></div>
35 <p>Note: the macro is internally recursive. If you use too many attributes (&gt; 256 tokens) you may
36 hit recursion limit. If you have so many attributes for a good reason, just raising the limit
37 should be OK. Note however that attribute-processing part has to use <a href="https://danielkeep.github.io/tlborm/book/pat-incremental-tt-munchers.html">TT muncher</a> which has
38 quadratic complexity, so having many attributes may blow up compile time. This should be rare.</p>
39 </div></details></section></div></main></body></html>