1<!DOCTYPE HTML> 2<html lang="en" class="sidebar-visible no-js light"> 3 <head> 4 <!-- Book generated using mdBook --> 5 <meta charset="UTF-8"> 6 <title>Welcome - Dyner - More dyn than ever</title> 7 8 9 10 11 <!-- Custom HTML head --> 12 13 14 15 <meta content="text/html; charset=utf-8" http-equiv="Content-Type"> 16 <meta name="description" content=""> 17 <meta name="viewport" content="width=device-width, initial-scale=1"> 18 <meta name="theme-color" content="#ffffff" /> 19 20 21 <link rel="icon" href="favicon.svg"> 22 23 24 <link rel="shortcut icon" href="favicon.png"> 25 26 <link rel="stylesheet" href="css/variables.css"> 27 <link rel="stylesheet" href="css/general.css"> 28 <link rel="stylesheet" href="css/chrome.css"> 29 30 <link rel="stylesheet" href="css/print.css" media="print"> 31 32 33 <!-- Fonts --> 34 <link rel="stylesheet" href="FontAwesome/css/font-awesome.css"> 35 36 <link rel="stylesheet" href="fonts/fonts.css"> 37 38 39 <!-- Highlight.js Stylesheets --> 40 <link rel="stylesheet" href="highlight.css"> 41 <link rel="stylesheet" href="tomorrow-night.css"> 42 <link rel="stylesheet" href="ayu-highlight.css"> 43 44 <!-- Custom theme stylesheets --> 45 46 47 48 </head> 49 <body> 50 <!-- Provide site root to javascript --> 51
51<script type="text/javascript"> 52 var path_to_root = ""; 53 var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light"; 54 </script>
54 55 56 <!-- Work around some values being stored in localStorage wrapped in quotes --> 57
57<script type="text/javascript"> 58 try { 59 var theme = localStorage.getItem('mdbook-theme'); 60 var sidebar = localStorage.getItem('mdbook-sidebar'); 61 62 if (theme.startsWith('"') && theme.endsWith('"')) { 63 localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1)); 64 } 65 66 if (sidebar.startsWith('"') && sidebar.endsWith('"')) { 67 localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1)); 68 } 69 } catch (e) { } 70 </script>
70 71 72 <!-- Set the theme before any content is loaded, prevents flash --> 73
73<script type="text/javascript"> 74 var theme; 75 try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { } 76 if (theme === null || theme === undefined) { theme = default_theme; } 77 var html = document.querySelector('html'); 78 html.classList.remove('no-js') 79 html.classList.remove('light') 80 html.classList.add(theme); 81 html.classList.add('js'); 82 </script>
82 83 84 <!-- Hide / unhide sidebar before it is displayed --> 85
85<script type="text/javascript"> 86 var html = document.querySelector('html'); 87 var sidebar = 'hidden'; 88 if (document.body.clientWidth >= 1080) { 89 try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { } 90 sidebar = sidebar || 'visible'; 91 } 92 html.classList.remove('sidebar-visible'); 93 html.classList.add("sidebar-" + sidebar); 94 </script>
94 95 96 <nav id="sidebar" class="sidebar" aria-label="Table of contents"> 97 <div class="sidebar-scrollbox"> 98 <ol class="chapter"><li class="chapter-item expanded "><a class='active' href='/welcome'><strong aria-hidden="true">1.</strong> Welcome</a></li><li class="chapter-item expanded "><a href='/guide'><strong aria-hidden="true">2.</strong> User's guide</a></li><li class="chapter-item expanded "><a href='/plumbing'><strong aria-hidden="true">3.</strong> Plumbing</a></li><li class="chapter-item expanded "><a href='/faq'><strong aria-hidden="true">4.</strong> FAQ</a></li></ol> 99 </div> 100 <div id="sidebar-resize-handle" class="sidebar-resize-handle"></div> 101 </nav> 102 103 <div id="page-wrapper" class="page-wrapper"> 104 105 <div class="page"> 106 107 <div id="menu-bar-hover-placeholder"></div> 108 <div id="menu-bar" class="menu-bar sticky bordered"> 109 <div class="left-buttons"> 110 <button id="sidebar-toggle" class="icon-button" type="button" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar"> 111 <i class="fa fa-bars"></i> 112 </button> 113 <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list"> 114 <i class="fa fa-paint-brush"></i> 115 </button> 116 <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu"> 117 <li role="none"><button role="menuitem" class="theme" id="light">Light (default)</button></li> 118 <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li> 119 <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li> 120 <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li> 121 <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li> 122 </ul> 123 124 <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar"> 125 <i class="fa fa-search"></i> 126 </button> 127 128 </div> 129 130 <h1 class="menu-title">Dyner - More dyn than ever</h1> 131 132 <div class="right-buttons"> 133 134 <a aria-label='Print this book' href='/print' title='Print this book'> 135 <i id="print-button" class="fa fa-print"></i> 136 </a> 137 138 139 <a href="https://github.com/nikomatsakis/dyner" title="Git repository" aria-label="Git repository"> 140 <i id="git-repository-button" class="fa fa-github"></i> 141 </a> 142 143 144 <a href="https://github.com/nikomatsakis/dyner/edit/master/src/welcome.md" title="Suggest an edit" aria-label="Suggest an edit"> 145 <i id="git-edit-button" class="fa fa-edit"></i> 146 </a> 147 148 149 </div> 150 </div> 151 152 153 <div id="search-wrapper" class="hidden"> 154 <form id="searchbar-outer" class="searchbar-outer"> 155 <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header"> 156 </form> 157 <div id="searchresults-outer" class="searchresults-outer hidden"> 158 <div id="searchresults-header" class="searchresults-header"></div> 159 <ul id="searchresults"> 160 </ul> 161 </div> 162 </div> 163 164 165 <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM --> 166
166<script type="text/javascript"> 167 document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible'); 168 document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible'); 169 Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) { 170 link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1); 171 }); 172 </script>
172 173 174 <div id="content" class="content"> 175 <main> 176 <h1 id="welcome"><a class="header" href="#welcome">Welcome</a></h1> 177<p>The <code>dyner</code> crate is an experimental crate that is exploring the "outer reaches" of how we can improve trait objects (<code>dyn</code> types) in Rust.</p> 178<p><code>dyner</code> makes it possible to use dynamic dispatch on traits that...</p> 179<ul> 180<li>...have async functions.</li> 181<li>...use <code>impl Trait</code> in argument or return position, so long as <code>Trait</code> is also processed with <code>dyner</code>.</li> 182<li>...have by-value <code>self</code> methods.</li> 183</ul> 184<p>More dyner features:</p> 185<ul> 186<li>No need to worry about <code>?Sized</code>! ð§âð³ ð</li> 187</ul> 188<h2 id="quick-start"><a class="header" href="#quick-start">Quick start</a></h2> 189<p>To give you an idea for how dyner works, consider this pair of traits:</p> 190<pre><pre class="playground"><code class="language-rust"> 191<span class="boring">#![allow(unused)] 192</span><span class="boring">fn main() { 193</span>#[dyner] 194trait Screen { 195 fn put(&mut self, ch: char, x: u32, y: u32); 196} 197 198#[dyner] 199trait Draw { 200 fn draw(&self, screen: &mut impl Screen); 201} 202<span class="boring">} 203</span></code></pre></pre> 204<p>You can't use ordinary <code>dyn</code> types with these traits. But because we added the <code>#[dyner]</code> annotation, we have access to two types, <code>DynScreen</code> and <code>DynDraw</code>, that we can use to get dynamic dispatch. For example, we might collect a vector of "drawable things":</p> 205<pre><pre class="playground"><code class="language-rust"> 206<span class="boring">#![allow(unused)] 207</span><span class="boring">fn main() { 208</span>fn make_drawables() -> Vec<DynDraw<'static>> { 209 let r = Rectangle::new(0, 0, 10, 10); 210 let c = Circle::new(22, 44, 66); 211 vec![DynDraw::new(r), DynDraw::new(c)] 212} 213<span class="boring">} 214</span></code></pre></pre> 215<p>The <code>DynDraw<'static></code> type here means "some <code>Draw</code> object that has no references". You could also write a routine that draws these things onto a screen:</p> 216<pre><pre class="playground"><code class="language-rust"> 217<span class="boring">#![allow(unused)] 218</span><span class="boring">fn main() { 219</span>fn draw_all(draws: &[DynDraw<'_>], screen: &mut impl Screen) { 220 for draw in draws { 221 draws.draw(screen); 222 } 223} 224<span class="boring">} 225</span></code></pre></pre> 226<p>Note that this version of <code>draw_all</code> used an <code>impl Screen</code>, and hence we would create a distinct version of this method for every kind of screen. To avoid that, maybe we want to use dynamic dispatch there too. No problem, just change <code>impl Screen</code> to <code>DynScreen<'_></code>, and everything still works:</p> 227<pre><pre class="playground"><code class="language-rust"> 228<span class="boring">#![allow(unused)] 229</span><span class="boring">fn main() { 230</span>fn draw_all(draws: &[DynDraw<'_>], screen: &mut DynScreen<'_>) { 231 for draw in draws { 232 draws.draw(screen); 233 } 234} 235<span class="boring">} 236</span></code></pre></pre> 237<h2 id="objects-from-references"><a class="header" href="#objects-from-references">Objects from references</a></h2> 238<p>In the previous examples, we used <code>DynDraw::new</code> to construct an object; the <code>new</code> method takes ownership of the data in the object. But sometimes we just have an <code>&impl Trait</code> and we'd like to get dynamic dispatch from that. You can use the <code>from_ref</code> method to do that. In this code, the <code>draw_four</code> method is implemented with <code>impl Trait</code>, but it calls into the <code>draw_two</code> method, which is implemented with dynamic dispatch:</p> 239<pre><pre class="playground"><code class="language-rust"> 240<span class="boring">#![allow(unused)] 241</span><span class="boring">fn main() { 242</span>fn draw_four(draw: &impl Draw, screen: &mut impl Screen) { 243 let draw_ref = DynDraw::from_ref(draw); 244 let screen_mut = DynScreen:from_mut(screen); 245 draw_two(&draw_ref, screen); 246 draw_two(&draw_ref, screen); 247} 248 249fn draw_two(draw: &DynDraw<'_>, screen: &mut DynScreen<'_>) { 250 draw.draw(screen); 251 draw.draw(screen); 252} 253<span class="boring">} 254</span></code></pre></pre> 255<h2 id="no-std-no-problem"><a class="header" href="#no-std-no-problem">No std? No problem.</a></h2> 256<p>By default, <code>DynDraw</code> depends on std, since <code>DynDraw::new</code> needs to allocate a <code>Box</code> behind the scenes. If you opt out from the default features, you will lose access to <code>DynDraw::new</code>, but you can still use <code>DynDraw::from_ref</code> and <code>DynDraw::from_mut</code>.</p> 257 258 </main> 259 260 <nav class="nav-wrapper" aria-label="Page navigation"> 261 <!-- Mobile navigation buttons --> 262 263 264 265 <a aria-keyshortcuts='Right' aria-label='Next chapter' class='mobile-nav-chapter
265s next' href='/guide' rel='next' title='Next chapter'> 266 <i class="fa fa-angle-right"></i> 267 </a> 268 269 270 <div style="clear: both"></div> 271 </nav> 272 </div> 273 </div> 274 275 <nav class="nav-wide-wrapper" aria-label="Page navigation"> 276 277 278 279 <a aria-keyshortcuts='Right' aria-label='Next chapter' class='nav-chapters next' href='/guide' rel='next' title='Next chapter'> 280 <i class="fa fa-angle-right"></i> 281 </a> 282 283 </nav> 284 285 </div> 286 287 288 289 290 291 292 293 294
294<script type="text/javascript"> 295 window.playground_copyable = true; 296 </script>
296 297 298 299 300 301 302
302<script src="elasticlunr.min.js" type="text/javascript" charset="utf-8"></script>
302 303
303<script src="mark.min.js" type="text/javascript" charset="utf-8"></script>
303 304
304<script src="searcher.js" type="text/javascript" charset="utf-8"></script>
304 305 306 307
307<script src="clipboard.min.js" type="text/javascript" charset="utf-8"></script>
307 308
308<script src="highlight.js" type="text/javascript" charset="utf-8"></script>
308 309
309<script src="book.js" type="text/javascript" charset="utf-8"></script>
309 310 311 <!-- Custom JS scripts --> 312 313 314 315 316 </body> 317</html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.