Files
google_speech_rs/docs/pyo3/marker/trait.Ungil.html
T
2025-04-02 13:41:38 +00:00

37 lines
6.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="Types that are safe to access while the GIL is not held."><title>Ungil in pyo3::marker - 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="pyo3" data-themes="" data-resource-suffix="" data-rustdoc-version="1.85.1 (4eb161250 2025-03-15)" data-channel="1.85.1" data-search-js="search-75f5ac3e.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 trait"><!--[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="../../pyo3/index.html">pyo3</a><span class="version">0.24.1</span></h2></div><div class="sidebar-elems"><section id="rustdoc-toc"><h2 class="location"><a href="#">Ungil</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#safety" title="Safety">Safety</a></li><li><a href="#examples" title="Examples">Examples</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In pyo3::<wbr>marker</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">pyo3</a>::<wbr><a href="index.html">marker</a></span><h1>Trait <span class="trait">Ungil</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/pyo3/marker.rs.html#190">Source</a> </span></div><pre class="rust item-decl"><code>pub unsafe trait Ungil { }</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Types that are safe to access while the GIL is not held.</p>
<h2 id="safety"><a class="doc-anchor" href="#safety">§</a>Safety</h2>
<p>The type must not carry borrowed Python references or, if it does, not allow access to them if
the GIL is not held.</p>
<p>See the <a href="index.html" title="mod pyo3::marker">module-level documentation</a> for more information.</p>
<h2 id="examples"><a class="doc-anchor" href="#examples">§</a>Examples</h2>
<p>This tracking is currently imprecise as it relies on the <a href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Send.html" title="trait core::marker::Send"><code>Send</code></a> auto trait on stable Rust.
For example, an <code>Rc</code> smart pointer should be usable without the GIL, but we currently prevent that:</p>
<div class="example-wrap compile_fail"><a href="#" class="tooltip" title="This example deliberately fails to compile"></a><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::rc::Rc;
Python::with_gil(|py| {
<span class="kw">let </span>rc = Rc::new(<span class="number">42</span>);
py.allow_threads(|| {
<span class="macro">println!</span>(<span class="string">"{:?}"</span>, rc);
});
});</code></pre></div>
<p>This also implies that the interplay between <code>with_gil</code> and <code>allow_threads</code> is unsound, for example
one can circumvent this protection using the <a href="https://docs.rs/send_wrapper/"><code>send_wrapper</code></a> crate:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>send_wrapper::SendWrapper;
Python::with_gil(|py| {
<span class="kw">let </span>string = PyString::new(py, <span class="string">"foo"</span>);
<span class="kw">let </span>wrapped = SendWrapper::new(string);
py.allow_threads(|| {
<span class="kw">let </span>sneaky: <span class="kw-2">&amp;</span>Bound&lt;<span class="lifetime">'_</span>, PyString&gt; = <span class="kw-2">&amp;*</span>wrapped;
<span class="macro">println!</span>(<span class="string">"{:?}"</span>, sneaky);
});
});</code></pre></div>
<p>Fixing this loophole on stable Rust has significant ergonomic issues, but it is fixed when using
nightly Rust and the <code>nightly</code> feature, c.f. <a href="https://github.com/PyO3/pyo3/issues/2141">#2141</a>.</p>
</div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-Ungil-for-T" class="impl"><a class="src rightside" href="../../src/pyo3/marker.rs.html#194">Source</a><a href="#impl-Ungil-for-T" class="anchor">§</a><h3 class="code-header">impl&lt;T: <a class="trait" href="https://doc.rust-lang.org/1.85.1/core/marker/trait.Send.html" title="trait core::marker::Send">Send</a>&gt; <a class="trait" href="trait.Ungil.html" title="trait pyo3::marker::Ungil">Ungil</a> for T</h3></section></div><script src="../../trait.impl/pyo3/marker/trait.Ungil.js" async></script></section></div></main></body></html>