39 lines
10 KiB
HTML
39 lines
10 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="Returns a borrowed pointer to a Python object."><title>AsPyPointer in pyo3::conversion - 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="#">AsPy<wbr>Pointer</a></h2><h3><a href="#">Sections</a></h3><ul class="block top-toc"><li><a href="#examples" title="Examples">Examples</a></li><li><a href="#safety" title="Safety">Safety</a></li></ul><h3><a href="#required-methods">Required Methods</a></h3><ul class="block"><li><a href="#tymethod.as_ptr" title="as_ptr">as_ptr</a></li></ul><h3><a href="#foreign-impls">Implementations on Foreign Types</a></h3><ul class="block"><li><a href="#impl-AsPyPointer-for-Option%3CT%3E" title="Option<T>">Option<T></a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><div id="rustdoc-modnav"><h2><a href="index.html">In pyo3::<wbr>conversion</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">conversion</a></span><h1>Trait <span class="trait">AsPyPointer</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/conversion.rs.html#64-67">Source</a> </span></div><pre class="rust item-decl"><code>pub unsafe trait AsPyPointer {
|
|
// Required method
|
|
fn <a href="#tymethod.as_ptr" class="fn">as_ptr</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.pointer.html">*mut </a><a class="struct" href="../ffi/struct.PyObject.html" title="struct pyo3::ffi::PyObject">PyObject</a>;
|
|
}</code></pre><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Returns a borrowed pointer to a Python object.</p>
|
|
<p>The returned pointer will be valid for as long as <code>self</code> is. It may be null depending on the
|
|
implementation.</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="kw">use </span>pyo3::prelude::<span class="kw-2">*</span>;
|
|
<span class="kw">use </span>pyo3::ffi;
|
|
|
|
Python::with_gil(|py| {
|
|
<span class="kw">let </span>s = <span class="string">"foo"</span>.into_pyobject(py)<span class="question-mark">?</span>;
|
|
<span class="kw">let </span>ptr = s.as_ptr();
|
|
|
|
<span class="kw">let </span>is_really_a_pystring = <span class="kw">unsafe </span>{ ffi::PyUnicode_CheckExact(ptr) };
|
|
<span class="macro">assert_eq!</span>(is_really_a_pystring, <span class="number">1</span>);
|
|
})</code></pre></div>
|
|
<h2 id="safety"><a class="doc-anchor" href="#safety">§</a>Safety</h2>
|
|
<p>For callers, it is your responsibility to make sure that the underlying Python object is not dropped too
|
|
early. For example, the following code will cause undefined behavior:</p>
|
|
|
|
<div class="example-wrap"><pre class="rust rust-example-rendered"><code>Python::with_gil(|py| {
|
|
<span class="comment">// ERROR: calling `.as_ptr()` will throw away the temporary object and leave `ptr` dangling.
|
|
</span><span class="kw">let </span>ptr: <span class="kw-2">*mut </span>ffi::PyObject = <span class="number">0xabad1dea_u32</span>.into_pyobject(py)<span class="question-mark">?</span>.as_ptr();
|
|
|
|
<span class="kw">let </span>isnt_a_pystring = <span class="kw">unsafe </span>{
|
|
<span class="comment">// `ptr` is dangling, this is UB
|
|
</span>ffi::PyUnicode_CheckExact(ptr)
|
|
};
|
|
})</code></pre></div>
|
|
<p>This happens because the pointer returned by <code>as_ptr</code> does not carry any lifetime information
|
|
and the Python object is dropped immediately after the <code>0xabad1dea_u32.into_pyobject(py).as_ptr()</code>
|
|
expression is evaluated. To fix the problem, bind Python object to a local variable like earlier
|
|
to keep the Python object alive until the end of its scope.</p>
|
|
<p>Implementors must ensure this returns a valid pointer to a Python object, which borrows a reference count from <code>&self</code>.</p>
|
|
</div></details><h2 id="required-methods" class="section-header">Required Methods<a href="#required-methods" class="anchor">§</a></h2><div class="methods"><details class="toggle method-toggle" open><summary><section id="tymethod.as_ptr" class="method"><a class="src rightside" href="../../src/pyo3/conversion.rs.html#66">Source</a><h4 class="code-header">fn <a href="#tymethod.as_ptr" class="fn">as_ptr</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.pointer.html">*mut </a><a class="struct" href="../ffi/struct.PyObject.html" title="struct pyo3::ffi::PyObject">PyObject</a></h4></section></summary><div class="docblock"><p>Returns the underlying FFI pointer as a borrowed pointer.</p>
|
|
</div></details></div><h2 id="foreign-impls" class="section-header">Implementations on Foreign Types<a href="#foreign-impls" class="anchor">§</a></h2><details class="toggle implementors-toggle"><summary><section id="impl-AsPyPointer-for-Option%3CT%3E" class="impl"><a class="src rightside" href="../../src/pyo3/conversions/std/option.rs.html#77-86">Source</a><a href="#impl-AsPyPointer-for-Option%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="trait.AsPyPointer.html" title="trait pyo3::conversion::AsPyPointer">AsPyPointer</a> for <a class="enum" href="https://doc.rust-lang.org/1.85.1/core/option/enum.Option.html" title="enum core::option::Option">Option</a><T><div class="where">where
|
|
T: <a class="trait" href="trait.AsPyPointer.html" title="trait pyo3::conversion::AsPyPointer">AsPyPointer</a>,</div></h3><div class="docblock"><p>Convert <code>None</code> into a null pointer.</p>
|
|
</div></section></summary><div class="impl-items"><section id="method.as_ptr" class="method trait-impl"><a class="src rightside" href="../../src/pyo3/conversions/std/option.rs.html#82-85">Source</a><a href="#method.as_ptr" class="anchor">§</a><h4 class="code-header">fn <a href="#tymethod.as_ptr" class="fn">as_ptr</a>(&self) -> <a class="primitive" href="https://doc.rust-lang.org/1.85.1/std/primitive.pointer.html">*mut </a><a class="struct" href="../ffi/struct.PyObject.html" title="struct pyo3::ffi::PyObject">PyObject</a></h4></section></div></details><h2 id="implementors" class="section-header">Implementors<a href="#implementors" class="anchor">§</a></h2><div id="implementors-list"><section id="impl-AsPyPointer-for-Bound%3C'_,+T%3E" class="impl"><a class="src rightside" href="../../src/pyo3/instance.rs.html#616-621">Source</a><a href="#impl-AsPyPointer-for-Bound%3C'_,+T%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="trait.AsPyPointer.html" title="trait pyo3::conversion::AsPyPointer">AsPyPointer</a> for <a class="struct" href="../struct.Bound.html" title="struct pyo3::Bound">Bound</a><'_, T></h3></section><section id="impl-AsPyPointer-for-Py%3CT%3E" class="impl"><a class="src rightside" href="../../src/pyo3/instance.rs.html#1799-1805">Source</a><a href="#impl-AsPyPointer-for-Py%3CT%3E" class="anchor">§</a><h3 class="code-header">impl<T> <a class="trait" href="trait.AsPyPointer.html" title="trait pyo3::conversion::AsPyPointer">AsPyPointer</a> for <a class="struct" href="../struct.Py.html" title="struct pyo3::Py">Py</a><T></h3></section><section id="impl-AsPyPointer-for-PyRef%3C'_,+T%3E" class="impl"><a class="src rightside" href="../../src/pyo3/pycell.rs.html#486-490">Source</a><a href="#impl-AsPyPointer-for-PyRef%3C'_,+T%3E" class="anchor">§</a><h3 class="code-header">impl<T: <a class="trait" href="../pyclass/trait.PyClass.html" title="trait pyo3::pyclass::PyClass">PyClass</a>> <a class="trait" href="trait.AsPyPointer.html" title="trait pyo3::conversion::AsPyPointer">AsPyPointer</a> for <a class="struct" href="../pycell/struct.PyRef.html" title="struct pyo3::pycell::PyRef">PyRef</a><'_, T></h3></section></div><script src="../../trait.impl/pyo3/conversion/trait.AsPyPointer.js" data-ignore-extern-crates="core" async></script></section></div></main></body></html> |