Skip to content

Commit 6804ddb

Browse files
GH Actions RunnerGH Actions Runner
GH Actions Runner
authored and
GH Actions Runner
committed
publish: Merge pull request #29 from matthewjasper/spec-attributes
generated from commit 28e8f60
1 parent 5597bdb commit 6804ddb

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

code-considerations/using-unstable-lang/specialization.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,32 @@ <h1 id="using-specialization"><a class="header" href="#using-specialization">Usi
191191
}
192192
}</code></pre>
193193
<p>Only specialization using the <code>min_specialization</code> feature should be used. The full <code>specialization</code> feature is known to be unsound.</p>
194+
<h2 id="specialization-attributes"><a class="header" href="#specialization-attributes">Specialization attributes</a></h2>
195+
<p>There are two unstable attributes that can be used to allow a trait bound in a specializing implementation that does not appear in the default implementation.</p>
196+
<p><code>rustc_specialization_trait</code> restricts the implementations of a trait to be &quot;always applicable&quot;. Implementing traits annotated with <code>rustc_specialization_trait</code> is unstable, so this should not be used on any stable traits exported from the standard library. <code>Sized</code> is an exception, and can have this attribute because it already cannot be implemented by an <code>impl</code> block.
197+
<strong>Note</strong>: <code>rustc_specialization_trait</code> only prevents incorrect monomorphizations, it does not prevent a type from being coerced between specialized and unspecialized types which can be important when specialization must be applied consistently. See <a href="https://github.com/rust-lang/rust/issues/85863">rust-lang/rust#85863</a> for more details.</p>
198+
<p><code>rustc_unsafe_specialization_marker</code> allows specializing on a trait with no associated items. The attribute is <code>unsafe</code> because lifetime constraints from the implementations of the trait are not considered when specializing. The following example demonstrates a limitation of <code>rustc_unsafe_specialization_marker</code>, the specialized implementation is used for <em>all</em> shared reference types, not just those with <code>'static</code> lifetime. Because of this, new uses of <code>rustc_unsafe_specialization_marker</code> should be avoided.</p>
199+
<pre><code class="language-rust ignore">#[rustc_unsafe_specialization_marker]
200+
trait StaticRef {}
201+
202+
impl&lt;T&gt; StaticRef for &amp;'static T {}
203+
204+
trait DoThing: Sized {
205+
fn do_thing(self);
206+
}
207+
208+
impl&lt;T&gt; DoThing for T {
209+
default fn do_thing(self) {
210+
// slow impl
211+
}
212+
}
213+
214+
impl&lt;T: StaticRef&gt; DoThing for T {
215+
fn do_thing(self) {
216+
// fast impl
217+
}
218+
}</code></pre>
219+
<p><code>rustc_unsafe_specialization_marker</code> exists to allow existing specializations that are based on marker traits exported from <code>std</code>, such as <code>Copy</code>, <code>FusedIterator</code> or <code>Eq</code>.</p>
194220
<h2 id="for-reviewers"><a class="header" href="#for-reviewers">For reviewers</a></h2>
195221
<p>Look out for any <code>default</code> annotations on public trait implementations. These will need to be refactored into a private dispatch trait. Also look out for uses of specialization that do more than pick a more optimized implementation.</p>
196222

print.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,32 @@ <h2 id="for-reviewers-12"><a class="header" href="#for-reviewers-12">For reviewe
688688
}
689689
}</code></pre>
690690
<p>Only specialization using the <code>min_specialization</code> feature should be used. The full <code>specialization</code> feature is known to be unsound.</p>
691+
<h2 id="specialization-attributes"><a class="header" href="#specialization-attributes">Specialization attributes</a></h2>
692+
<p>There are two unstable attributes that can be used to allow a trait bound in a specializing implementation that does not appear in the default implementation.</p>
693+
<p><code>rustc_specialization_trait</code> restricts the implementations of a trait to be &quot;always applicable&quot;. Implementing traits annotated with <code>rustc_specialization_trait</code> is unstable, so this should not be used on any stable traits exported from the standard library. <code>Sized</code> is an exception, and can have this attribute because it already cannot be implemented by an <code>impl</code> block.
694+
<strong>Note</strong>: <code>rustc_specialization_trait</code> only prevents incorrect monomorphizations, it does not prevent a type from being coerced between specialized and unspecialized types which can be important when specialization must be applied consistently. See <a href="https://github.com/rust-lang/rust/issues/85863">rust-lang/rust#85863</a> for more details.</p>
695+
<p><code>rustc_unsafe_specialization_marker</code> allows specializing on a trait with no associated items. The attribute is <code>unsafe</code> because lifetime constraints from the implementations of the trait are not considered when specializing. The following example demonstrates a limitation of <code>rustc_unsafe_specialization_marker</code>, the specialized implementation is used for <em>all</em> shared reference types, not just those with <code>'static</code> lifetime. Because of this, new uses of <code>rustc_unsafe_specialization_marker</code> should be avoided.</p>
696+
<pre><code class="language-rust ignore">#[rustc_unsafe_specialization_marker]
697+
trait StaticRef {}
698+
699+
impl&lt;T&gt; StaticRef for &amp;'static T {}
700+
701+
trait DoThing: Sized {
702+
fn do_thing(self);
703+
}
704+
705+
impl&lt;T&gt; DoThing for T {
706+
default fn do_thing(self) {
707+
// slow impl
708+
}
709+
}
710+
711+
impl&lt;T: StaticRef&gt; DoThing for T {
712+
fn do_thing(self) {
713+
// fast impl
714+
}
715+
}</code></pre>
716+
<p><code>rustc_unsafe_specialization_marker</code> exists to allow existing specializations that are based on marker traits exported from <code>std</code>, such as <code>Copy</code>, <code>FusedIterator</code> or <code>Eq</code>.</p>
691717
<h2 id="for-reviewers-13"><a class="header" href="#for-reviewers-13">For reviewers</a></h2>
692718
<p>Look out for any <code>default</code> annotations on public trait implementations. These will need to be refactored into a private dispatch trait. Also look out for uses of specialization that do more than pick a more optimized implementation.</p>
693719
<div style="break-before: page; page-break-before: always;"></div><h1 id="performance"><a class="header" href="#performance">Performance</a></h1>

searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

searchindex.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)