27 lines
7.1 KiB
HTML
27 lines
7.1 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="Creates a new task."><title>spawn in async_task - 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="async_task" 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="icon" href="https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png"></head><body class="rustdoc fn"><!--[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="../async_task/index.html"><img src="https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../async_task/index.html"><img src="https://raw.githubusercontent.com/smol-rs/smol/master/assets/images/logo_fullsize_transparent.png" alt="logo"></a><h2><a href="../async_task/index.html">async_<wbr>task</a><span class="version">4.7.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">spawn</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li></ul></section><div id="rustdoc-modnav"><h2 class="in-crate"><a href="index.html">In crate async_<wbr>task</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">async_task</a></span><h1>Function <span class="fn">spawn</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/async_task/runnable.rs.html#567-574">Source</a> </span></div><pre class="rust item-decl"><code>pub fn spawn<F, S>(future: F, schedule: S) -> (<a class="struct" href="struct.Runnable.html" title="struct async_task::Runnable">Runnable</a>, <a class="struct" href="struct.Task.html" title="struct async_task::Task">Task</a><F::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.0/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>>)<div class="where">where
|
||
F: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,
|
||
F::<a class="associatedtype" href="https://doc.rust-lang.org/1.84.0/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>: <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + 'static,
|
||
S: <a class="trait" href="trait.Schedule.html" title="trait async_task::Schedule">Schedule</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a> + <a class="trait" href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Sync.html" title="trait core::marker::Sync">Sync</a> + 'static,</div></code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Creates a new task.</p>
|
||
<p>The returned <a href="struct.Runnable.html" title="struct async_task::Runnable"><code>Runnable</code></a> is used to poll the <code>future</code>, and the <a href="struct.Task.html" title="struct async_task::Task"><code>Task</code></a> is used to await its
|
||
output.</p>
|
||
<p>Method <a href="struct.Runnable.html#method.run" title="method async_task::Runnable::run"><code>run()</code></a> polls the task’s future once. Then, the <a href="struct.Runnable.html" title="struct async_task::Runnable"><code>Runnable</code></a>
|
||
vanishes and only reappears when its <a href="https://doc.rust-lang.org/1.84.0/core/task/wake/struct.Waker.html" title="struct core::task::wake::Waker"><code>Waker</code></a> wakes the task, thus scheduling it to be run
|
||
again.</p>
|
||
<p>When the task is woken, its <a href="struct.Runnable.html" title="struct async_task::Runnable"><code>Runnable</code></a> is passed to the <code>schedule</code> function.
|
||
The <code>schedule</code> function should not attempt to run the <a href="struct.Runnable.html" title="struct async_task::Runnable"><code>Runnable</code></a> nor to drop it. Instead, it
|
||
should push it into a task queue so that it can be processed later.</p>
|
||
<p>If you need to spawn a future that does not implement <a href="https://doc.rust-lang.org/1.84.0/core/marker/trait.Send.html" title="trait core::marker::Send"><code>Send</code></a> or isn’t <code>'static</code>, consider
|
||
using <a href="fn.spawn_local.html" title="fn async_task::spawn_local"><code>spawn_local()</code></a> or <a href="fn.spawn_unchecked.html" title="fn async_task::spawn_unchecked"><code>spawn_unchecked()</code></a> instead.</p>
|
||
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="comment">// The future inside the task.
|
||
</span><span class="kw">let </span>future = <span class="kw">async </span>{
|
||
<span class="macro">println!</span>(<span class="string">"Hello, world!"</span>);
|
||
};
|
||
|
||
<span class="comment">// A function that schedules the task when it gets woken up.
|
||
</span><span class="kw">let </span>(s, r) = flume::unbounded();
|
||
<span class="kw">let </span>schedule = <span class="kw">move </span>|runnable| s.send(runnable).unwrap();
|
||
|
||
<span class="comment">// Create a task with the future and the schedule function.
|
||
</span><span class="kw">let </span>(runnable, task) = async_task::spawn(future, schedule);</code></pre></div>
|
||
</div></details></section></div></main></body></html> |