138 lines
11 KiB
HTML
138 lines
11 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="What `#[derive(From)]` generates"><title>From in derive_more - 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-42caa33d.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="derive_more" data-themes="" data-resource-suffix="" data-rustdoc-version="1.84.0 (9fc6b4312 2025-01-07)" data-channel="1.84.0" data-search-js="search-92e6798f.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 derive"><!--[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="../derive_more/index.html">derive_<wbr>more</a><span class="version">1.0.0</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">From</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#what-derivefrom-generates" title="What `#[derive(From)]` generates">What <code>#[derive(From)]</code> generates</a><ul><li><a href="#structs" title="Structs">Structs</a></li><li><a href="#enums" title="Enums">Enums</a></li><li><a href="#example-usage" title="Example usage">Example usage</a></li></ul></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate derive_<wbr>more</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">derive_more</a></span><h1>Derive Macro <span class="derive">From</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/derive_more_impl/lib.rs.html#103">Source</a> </span></div><pre class="rust item-decl"><code>#[derive(From)]
|
|
{
|
|
<span class="comment">// Attributes available to this derive:</span>
|
|
#[from]
|
|
}
|
|
</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><h2 id="what-derivefrom-generates"><a class="doc-anchor" href="#what-derivefrom-generates">§</a>What <code>#[derive(From)]</code> generates</h2>
|
|
<p>The point of deriving this type is that it makes it easy to create a new
|
|
instance of the type by using the <code>.into()</code> method on the value(s) that it
|
|
should contain. This is done by implementing the <code>From</code> trait for the type
|
|
that is passed to the derive.</p>
|
|
<h3 id="structs"><a class="doc-anchor" href="#structs">§</a>Structs</h3>
|
|
<p>For structs with a single field you can call <code>.into()</code> on the desired content
|
|
itself after deriving <code>From</code>.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Debug, From, PartialEq)]
|
|
</span><span class="kw">struct </span>Int(i32);
|
|
|
|
<span class="macro">assert_eq!</span>(Int(<span class="number">2</span>), <span class="number">2</span>.into());</code></pre></div>
|
|
<p>For structs that have multiple fields <code>.into()</code> needs to be called on a tuple
|
|
containing the desired content for each field.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Debug, From, PartialEq)]
|
|
</span><span class="kw">struct </span>Point(i32, i32);
|
|
|
|
<span class="macro">assert_eq!</span>(Point(<span class="number">1</span>, <span class="number">2</span>), (<span class="number">1</span>, <span class="number">2</span>).into());</code></pre></div>
|
|
<p>To specify concrete types to derive convert from use <code>#[from(<types>)]</code>.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Debug, From, PartialEq)]
|
|
#[from(Cow<<span class="lifetime">'static</span>, str>, String, <span class="kw-2">&</span><span class="lifetime">'static </span>str)]
|
|
</span><span class="kw">struct </span>Str(Cow<<span class="lifetime">'static</span>, str>);
|
|
|
|
<span class="macro">assert_eq!</span>(Str(<span class="string">"&str"</span>.into()), <span class="string">"&str"</span>.into());
|
|
<span class="macro">assert_eq!</span>(Str(<span class="string">"String"</span>.into()), <span class="string">"String"</span>.to_owned().into());
|
|
<span class="macro">assert_eq!</span>(Str(<span class="string">"Cow"</span>.into()), Cow::Borrowed(<span class="string">"Cow"</span>).to_owned().into());
|
|
|
|
<span class="attr">#[derive(Debug, From, PartialEq)]
|
|
#[from((i16, i16), (i32, i32))]
|
|
</span><span class="kw">struct </span>Point {
|
|
x: i32,
|
|
y: i32,
|
|
}
|
|
|
|
<span class="macro">assert_eq!</span>(Point { x: <span class="number">1_i32</span>, y: <span class="number">2_i32 </span>}, (<span class="number">1_i16</span>, <span class="number">2_i16</span>).into());
|
|
<span class="macro">assert_eq!</span>(Point { x: <span class="number">3_i32</span>, y: <span class="number">4_i32 </span>}, (<span class="number">3_i32</span>, <span class="number">4_i32</span>).into());</code></pre></div>
|
|
<p>Also, you can forward implementation to the inner type, which means deriving <code>From</code> for any type, that derives <code>From</code>
|
|
inner type.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Debug, From, PartialEq)]
|
|
#[from(forward)]
|
|
</span><span class="kw">struct </span>Str {
|
|
inner: Cow<<span class="lifetime">'static</span>, str>,
|
|
}
|
|
|
|
<span class="macro">assert_eq!</span>(Str { inner: <span class="string">"&str"</span>.into() }, <span class="string">"&str"</span>.into());
|
|
<span class="macro">assert_eq!</span>(Str { inner: <span class="string">"String"</span>.into() }, <span class="string">"String"</span>.to_owned().into());
|
|
<span class="macro">assert_eq!</span>(Str { inner: <span class="string">"Cow"</span>.into() }, Cow::Borrowed(<span class="string">"Cow"</span>).to_owned().into());</code></pre></div>
|
|
<h3 id="enums"><a class="doc-anchor" href="#enums">§</a>Enums</h3>
|
|
<p>For enums <code>.into()</code> works for each variant as if they were structs. This
|
|
includes specifying concrete types via <code>#[from(<types>)]</code> or forwarding
|
|
implementation with <code>#[from(forward)]</code>.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Debug, From, PartialEq)]
|
|
</span><span class="kw">enum </span>IntOrPoint {
|
|
Int(i32),
|
|
Point {
|
|
x: i32,
|
|
y: i32,
|
|
},
|
|
}
|
|
|
|
<span class="macro">assert_eq!</span>(IntOrPoint::Int(<span class="number">1</span>), <span class="number">1</span>.into());
|
|
<span class="macro">assert_eq!</span>(IntOrPoint::Point { x: <span class="number">1</span>, y: <span class="number">2 </span>}, (<span class="number">1</span>, <span class="number">2</span>).into());</code></pre></div>
|
|
<p>By default, <code>From</code> is generated for every enum variant, but you can skip some
|
|
variants via <code>#[from(skip)]</code> (or <code>#[from(ignore)]</code>) or only concrete fields via <code>#[from]</code>.</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attr">#[derive(Debug, From, PartialEq)]
|
|
</span><span class="kw">enum </span>Int {
|
|
<span class="attr">#[from]
|
|
</span>Derived(i32),
|
|
NotDerived(i32),
|
|
}
|
|
|
|
<span class="comment">// Is equivalent to:
|
|
|
|
</span><span class="attr">#[derive(Debug, From, PartialEq)]
|
|
</span><span class="kw">enum </span>Int {
|
|
Derived(i32),
|
|
<span class="attr">#[from(skip)] </span><span class="comment">// or #[from(ignore)]
|
|
</span>NotDerived(i32),
|
|
}</code></pre></div>
|
|
<h3 id="example-usage"><a class="doc-anchor" href="#example-usage">§</a>Example usage</h3>
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// Allow converting from i32
|
|
</span><span class="attr">#[derive(From, PartialEq)]
|
|
</span><span class="kw">struct </span>MyInt(i32);
|
|
|
|
<span class="comment">// Forward from call to the field, so allow converting
|
|
// from anything that can be converted into an i64 (so most integers)
|
|
</span><span class="attr">#[derive(From, PartialEq)]
|
|
#[from(forward)]
|
|
</span><span class="kw">struct </span>MyInt64(i64);
|
|
|
|
<span class="comment">// You can ignore a variant
|
|
</span><span class="attr">#[derive(From, PartialEq)]
|
|
</span><span class="kw">enum </span>MyEnum {
|
|
SmallInt(i32),
|
|
NamedBigInt { int: i64 },
|
|
<span class="attr">#[from(ignore)]
|
|
</span>NoFromImpl(i64),
|
|
}
|
|
|
|
<span class="comment">// Or explicitly annotate the ones you need
|
|
</span><span class="attr">#[derive(From, PartialEq)]
|
|
</span><span class="kw">enum </span>MyEnum2 {
|
|
<span class="attr">#[from]
|
|
</span>SmallInt(i32),
|
|
<span class="attr">#[from]
|
|
</span>NamedBigInt { int: i64 },
|
|
NoFromImpl(i64),
|
|
}
|
|
|
|
<span class="comment">// And even specify additional conversions for them
|
|
</span><span class="attr">#[derive(From, PartialEq)]
|
|
</span><span class="kw">enum </span>MyEnum3 {
|
|
<span class="attr">#[from(i8, i32)]
|
|
</span>SmallInt(i32),
|
|
<span class="attr">#[from(i16, i64)]
|
|
</span>NamedBigInt { int: i64 },
|
|
NoFromImpl(i64),
|
|
}
|
|
|
|
<span class="macro">assert!</span>(MyInt(<span class="number">2</span>) == <span class="number">2</span>.into());
|
|
<span class="macro">assert!</span>(MyInt64(<span class="number">6</span>) == <span class="number">6u8</span>.into());
|
|
<span class="macro">assert!</span>(MyEnum::SmallInt(<span class="number">123</span>) == <span class="number">123i32</span>.into());
|
|
<span class="macro">assert!</span>(MyEnum::SmallInt(<span class="number">123</span>) != <span class="number">123i64</span>.into());
|
|
<span class="macro">assert!</span>(MyEnum::NamedBigInt{int: <span class="number">123</span>} == <span class="number">123i64</span>.into());
|
|
<span class="macro">assert!</span>(MyEnum3::SmallInt(<span class="number">123</span>) == <span class="number">123i8</span>.into());
|
|
<span class="macro">assert!</span>(MyEnum3::NamedBigInt{int: <span class="number">123</span>} == <span class="number">123i16</span>.into());</code></pre></div>
|
|
</div></details></section></div></main></body></html> |