blob: 4d5cc9149382d5db6ad9b4381a94034f2d35b134 [file] [log] [blame]
<!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="A framework for Rust wrappers over C APIs."><meta name="keywords" content="rust, rustlang, rust-lang, foreign_types"><title>foreign_types - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../normalize.css"><link rel="stylesheet" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../ayu.css" disabled><link rel="stylesheet" href="../dark.css" disabled><link rel="stylesheet" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script defer src="../crates.js"></script><script defer src="../main.js"></script><noscript><link rel="stylesheet" href="../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../favicon.svg"></head><body class="rustdoc mod crate"><!--[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">&#9776;</button><a class="sidebar-logo" href="../foreign_types/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../foreign_types/index.html"><div class="logo-container"><img class="rust-logo" src="../rust-logo.svg" alt="logo"></div></a><h2 class="location"><a href="#">Crate foreign_types</a></h2><div class="sidebar-elems"><ul class="block"><li class="version">Version 0.3.2</li><li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#macros">Macros</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Crate <a class="mod" href="#">foreign_types</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../src/foreign_types/lib.rs.html#1-306">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">&#x2212;</span>]</a></span></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>A framework for Rust wrappers over C APIs.</p>
<p>Ownership is as important in C as it is in Rust, but the semantics are often implicit. In
particular, pointer-to-value is commonly used to pass C values both when transferring ownership
or a borrow.</p>
<p>This crate provides a framework to define a Rust wrapper over these kinds of raw C APIs in a way
that allows ownership semantics to be expressed in an ergonomic manner. The framework takes a
dual-type approach similar to APIs in the standard library such as <code>PathBuf</code>/<code>Path</code> or <code>String</code>/
<code>str</code>. One type represents an owned value and references to the other represent borrowed
values.</p>
<h2 id="examples"><a href="#examples">Examples</a></h2>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>foreign_types::{ForeignType, ForeignTypeRef, Opaque};
<span class="kw">use </span>std::ops::{Deref, DerefMut};
<span class="kw">mod </span>foo_sys {
<span class="kw">pub enum </span>FOO {}
<span class="kw">extern </span>{
<span class="kw">pub fn </span>FOO_free(foo: <span class="kw-2">*mut </span>FOO);
}
}
<span class="comment">// The borrowed type is a newtype wrapper around an `Opaque` value.
//
// `FooRef` values never exist; we instead create references to `FooRef`s
// from raw C pointers.
</span><span class="kw">pub struct </span>FooRef(Opaque);
<span class="kw">impl </span>ForeignTypeRef <span class="kw">for </span>FooRef {
<span class="kw">type </span>CType = foo_sys::FOO;
}
<span class="comment">// The owned type is simply a newtype wrapper around the raw C type.
//
// It dereferences to `FooRef`, so methods that do not require ownership
// should be defined there.
</span><span class="kw">pub struct </span>Foo(<span class="kw-2">*mut </span>foo_sys::FOO);
<span class="kw">impl </span>Drop <span class="kw">for </span>Foo {
<span class="kw">fn </span>drop(<span class="kw-2">&amp;mut </span><span class="self">self</span>) {
<span class="kw">unsafe </span>{ foo_sys::FOO_free(<span class="self">self</span>.<span class="number">0</span>) }
}
}
<span class="kw">impl </span>ForeignType <span class="kw">for </span>Foo {
<span class="kw">type </span>CType = foo_sys::FOO;
<span class="kw">type </span>Ref = FooRef;
<span class="kw">unsafe fn </span>from_ptr(ptr: <span class="kw-2">*mut </span>foo_sys::FOO) -&gt; Foo {
Foo(ptr)
}
<span class="kw">fn </span>as_ptr(<span class="kw-2">&amp;</span><span class="self">self</span>) -&gt; <span class="kw-2">*mut </span>foo_sys::FOO {
<span class="self">self</span>.<span class="number">0
</span>}
}
<span class="kw">impl </span>Deref <span class="kw">for </span>Foo {
<span class="kw">type </span>Target = FooRef;
<span class="kw">fn </span>deref(<span class="kw-2">&amp;</span><span class="self">self</span>) -&gt; <span class="kw-2">&amp;</span>FooRef {
<span class="kw">unsafe </span>{ FooRef::from_ptr(<span class="self">self</span>.<span class="number">0</span>) }
}
}
<span class="kw">impl </span>DerefMut <span class="kw">for </span>Foo {
<span class="kw">fn </span>deref_mut(<span class="kw-2">&amp;mut </span><span class="self">self</span>) -&gt; <span class="kw-2">&amp;mut </span>FooRef {
<span class="kw">unsafe </span>{ FooRef::from_ptr_mut(<span class="self">self</span>.<span class="number">0</span>) }
}
}</code></pre></div>
<p>The <code>foreign_type!</code> macro can generate this boilerplate for you:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[macro_use]
</span><span class="kw">extern crate </span>foreign_types;
<span class="kw">mod </span>foo_sys {
<span class="kw">pub enum </span>FOO {}
<span class="kw">extern </span>{
<span class="kw">pub fn </span>FOO_free(foo: <span class="kw-2">*mut </span>FOO);
<span class="kw">pub fn </span>FOO_duplicate(foo: <span class="kw-2">*mut </span>FOO) -&gt; <span class="kw-2">*mut </span>FOO; <span class="comment">// Optional
</span>}
}
<span class="macro">foreign_type! </span>{
<span class="kw">type </span>CType = foo_sys::FOO;
<span class="kw">fn </span>drop = foo_sys::FOO_free;
<span class="kw">fn </span>clone = foo_sys::FOO_duplicate; <span class="comment">// Optional
</span><span class="doccomment">/// A Foo.
</span><span class="kw">pub struct </span>Foo;
<span class="doccomment">/// A borrowed Foo.
</span><span class="kw">pub struct </span>FooRef;
}
</code></pre></div>
<p>If <code>fn clone</code> is specified, then it must take <code>CType</code> as an argument and return a copy of it as <code>CType</code>.
It will be used to implement <code>ToOwned</code> and <code>Clone</code>.</p>
<p><code>#[derive(…)] is permitted before the lines with </code>pub struct<code>. </code>#[doc(hidden)]<code>before the</code>type CType<code>line will hide the</code>foreign_type!` implementations from documentation.</p>
<p>Say we then have a separate type in our C API that contains a <code>FOO</code>:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">mod </span>foo_sys {
<span class="kw">pub enum </span>FOO {}
<span class="kw">pub enum </span>BAR {}
<span class="kw">extern </span>{
<span class="kw">pub fn </span>FOO_free(foo: <span class="kw-2">*mut </span>FOO);
<span class="kw">pub fn </span>BAR_free(bar: <span class="kw-2">*mut </span>BAR);
<span class="kw">pub fn </span>BAR_get_foo(bar: <span class="kw-2">*mut </span>BAR) -&gt; <span class="kw-2">*mut </span>FOO;
}
}</code></pre></div>
<p>The documentation for the C library states that <code>BAR_get_foo</code> returns a reference into the <code>BAR</code>
passed to it, which translates into a reference in Rust. It also says that we’re allowed to
modify the <code>FOO</code>, so we’ll define a pair of accessor methods, one immutable and one mutable:</p>
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="attribute">#[macro_use]
</span><span class="kw">extern crate </span>foreign_types;
<span class="kw">use </span>foreign_types::ForeignTypeRef;
<span class="kw">mod </span>foo_sys {
<span class="kw">pub enum </span>FOO {}
<span class="kw">pub enum </span>BAR {}
<span class="kw">extern </span>{
<span class="kw">pub fn </span>FOO_free(foo: <span class="kw-2">*mut </span>FOO);
<span class="kw">pub fn </span>BAR_free(bar: <span class="kw-2">*mut </span>BAR);
<span class="kw">pub fn </span>BAR_get_foo(bar: <span class="kw-2">*mut </span>BAR) -&gt; <span class="kw-2">*mut </span>FOO;
}
}
<span class="macro">foreign_type! </span>{
<span class="attribute">#[doc(hidden)]
</span><span class="kw">type </span>CType = foo_sys::FOO;
<span class="kw">fn </span>drop = foo_sys::FOO_free;
<span class="doccomment">/// A Foo.
</span><span class="kw">pub struct </span>Foo;
<span class="doccomment">/// A borrowed Foo.
</span><span class="kw">pub struct </span>FooRef;
}
<span class="macro">foreign_type! </span>{
<span class="kw">type </span>CType = foo_sys::BAR;
<span class="kw">fn </span>drop = foo_sys::BAR_free;
<span class="doccomment">/// A Foo.
</span><span class="kw">pub struct </span>Bar;
<span class="doccomment">/// A borrowed Bar.
</span><span class="kw">pub struct </span>BarRef;
}
<span class="kw">impl </span>BarRef {
<span class="kw">fn </span>foo(<span class="kw-2">&amp;</span><span class="self">self</span>) -&gt; <span class="kw-2">&amp;</span>FooRef {
<span class="kw">unsafe </span>{ FooRef::from_ptr(foo_sys::BAR_get_foo(<span class="self">self</span>.as_ptr())) }
}
<span class="kw">fn </span>foo_mut(<span class="kw-2">&amp;mut </span><span class="self">self</span>) -&gt; <span class="kw-2">&amp;mut </span>FooRef {
<span class="kw">unsafe </span>{ FooRef::from_ptr_mut(foo_sys::BAR_get_foo(<span class="self">self</span>.as_ptr())) }
}
}
</code></pre></div>
</div></details><h2 id="macros" class="small-section-header"><a href="#macros">Macros</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="macro" href="macro.foreign_type.html" title="foreign_types::foreign_type macro">foreign_type</a></div><div class="item-right docblock-short">A macro to easily define wrappers for foreign types.</div></div></div><h2 id="structs" class="small-section-header"><a href="#structs">Structs</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="struct" href="struct.Opaque.html" title="foreign_types::Opaque struct">Opaque</a></div><div class="item-right docblock-short">An opaque type used to define <code>ForeignTypeRef</code> types.</div></div></div><h2 id="traits" class="small-section-header"><a href="#traits">Traits</a></h2><div class="item-table"><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.ForeignType.html" title="foreign_types::ForeignType trait">ForeignType</a></div><div class="item-right docblock-short">A type implemented by wrappers over foreign types.</div></div><div class="item-row"><div class="item-left module-item"><a class="trait" href="trait.ForeignTypeRef.html" title="foreign_types::ForeignTypeRef trait">ForeignTypeRef</a></div><div class="item-right docblock-short">A trait implemented by types which reference borrowed foreign types.</div></div></div></section></div></main><div id="rustdoc-vars" data-root-path="../" data-current-crate="foreign_types" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html>