Skip to content

Commit bbb7fad

Browse files
author
uniffi-docs[bot]
committed
Deployed 7f41fd8 to next with MkDocs 1.6.0 and mike 2.1.1
1 parent 85544cf commit bbb7fad

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

next/tutorial/Rust_scaffolding.html

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,17 @@
354354
</span>
355355
</a>
356356

357-
<nav class="md-nav" aria-label="Rust scaffolding code for UDL">
358-
<ul class="md-nav__list">
359-
360-
<li class="md-nav__item">
357+
</li>
358+
359+
<li class="md-nav__item">
361360
<a href="#setup-for-crates-using-only-proc-macros" class="md-nav__link">
362361
<span class="md-ellipsis">
363362
Setup for crates using only proc macros
364363
</span>
365364
</a>
366365

367-
</li>
366+
<nav class="md-nav" aria-label="Setup for crates using only proc macros">
367+
<ul class="md-nav__list">
368368

369369
<li class="md-nav__item">
370370
<a href="#libraries-that-depend-on-uniffi-components" class="md-nav__link">
@@ -1440,17 +1440,17 @@
14401440
</span>
14411441
</a>
14421442

1443-
<nav class="md-nav" aria-label="Rust scaffolding code for UDL">
1444-
<ul class="md-nav__list">
1445-
1446-
<li class="md-nav__item">
1443+
</li>
1444+
1445+
<li class="md-nav__item">
14471446
<a href="#setup-for-crates-using-only-proc-macros" class="md-nav__link">
14481447
<span class="md-ellipsis">
14491448
Setup for crates using only proc macros
14501449
</span>
14511450
</a>
14521451

1453-
</li>
1452+
<nav class="md-nav" aria-label="Setup for crates using only proc macros">
1453+
<ul class="md-nav__list">
14541454

14551455
<li class="md-nav__item">
14561456
<a href="#libraries-that-depend-on-uniffi-components" class="md-nav__link">
@@ -1485,8 +1485,8 @@
14851485

14861486

14871487
<h1 id="rust-scaffolding">Rust scaffolding</h1>
1488-
<p>As already noted UniFFI supports two methods of interface definitions: UDL files and proc macros.</p>
1489-
<p>If you use proc macros you can skip the next section, but if you use UDL files, you need to generate Rust "scaffolding" - code to implement what's described in the UDL.</p>
1488+
<p>UniFFI requires that our generated Rust "scaffolding" code is included in your project. For proc macros, most of this is conveniently generated by the macros.</p>
1489+
<p>If you use UDL files, you need to generate this "scaffolding" then include the generated .rs in your project.</p>
14901490
<h2 id="rust-scaffolding-code-for-udl">Rust scaffolding code for UDL</h2>
14911491
<p>Crates using UDL need a <code>build.rs</code> file next to <code>Cargo.toml</code>. This uses <code>uniffi</code> to generate the Rust scaffolding code.</p>
14921492
<div class="highlight"><pre><span></span><code><span class="k">fn</span><span class="w"> </span><span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span>
@@ -1497,14 +1497,13 @@ <h2 id="rust-scaffolding-code-for-udl">Rust scaffolding code for UDL</h2>
14971497
<p>Lastly, we include the generated scaffolding code in our <code>lib.rs</code> using this handy macro:</p>
14981498
<div class="highlight"><pre><span></span><code><span class="n">uniffi</span><span class="p">::</span><span class="n">include_scaffolding</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;math&quot;</span><span class="p">);</span>
14991499
</code></pre></div>
1500-
<h3 id="setup-for-crates-using-only-proc-macros">Setup for crates using only proc macros</h3>
1500+
<h2 id="setup-for-crates-using-only-proc-macros">Setup for crates using only proc macros</h2>
15011501
<p>If you are only using proc macros, you can skip <code>build.rs</code> entirely!
15021502
All you need to do is add this to the top of <code>lib.rs</code></p>
15031503
<div class="highlight"><pre><span></span><code><span class="n">uniffi</span><span class="p">::</span><span class="n">setup_scaffolding</span><span class="o">!</span><span class="p">();</span>
15041504
</code></pre></div>
1505-
<p>NOTE: This function takes an optional parameter, the <a href="../udl/namespace.html"><code>namespace</code></a> used by the component.
1506-
If not specified, the crate name will be used as the namespace.</p>
1507-
<p><strong>⚠ Warning ⚠</strong> Do not call both <code>uniffi::setup_scaffolding!()</code> and <code>uniffi::include_scaffolding!!()</code> in the same crate.</p>
1505+
<p>Don't use <code>uniffi::setup_scaffolding!()</code> in a crate which uses <code>uniffi::include_scaffolding!()</code>.</p>
1506+
<p>If you don't specify a <a href="../udl/namespace.html"><code>namespace</code></a> the crate name is used.</p>
15081507
<h3 id="libraries-that-depend-on-uniffi-components">Libraries that depend on UniFFI components</h3>
15091508
<p>Suppose you want to create a shared library that includes one or more
15101509
components using UniFFI. The typical way to achieve this is to create a new

next/tutorial/udl_file.html

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,18 @@
327327
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
328328

329329
<li class="md-nav__item">
330-
<a href="#the-udl-file" class="md-nav__link">
330+
<a href="#proc-macros" class="md-nav__link">
331331
<span class="md-ellipsis">
332-
The UDL File
332+
Proc macros
333+
</span>
334+
</a>
335+
336+
</li>
337+
338+
<li class="md-nav__item">
339+
<a href="#a-udl-file" class="md-nav__link">
340+
<span class="md-ellipsis">
341+
A UDL File
333342
</span>
334343
</a>
335344

@@ -1410,9 +1419,18 @@
14101419
<ul class="md-nav__list" data-md-component="toc" data-md-scrollfix>
14111420

14121421
<li class="md-nav__item">
1413-
<a href="#the-udl-file" class="md-nav__link">
1422+
<a href="#proc-macros" class="md-nav__link">
14141423
<span class="md-ellipsis">
1415-
The UDL File
1424+
Proc macros
1425+
</span>
1426+
</a>
1427+
1428+
</li>
1429+
1430+
<li class="md-nav__item">
1431+
<a href="#a-udl-file" class="md-nav__link">
1432+
<span class="md-ellipsis">
1433+
A UDL File
14161434
</span>
14171435
</a>
14181436

@@ -1437,10 +1455,14 @@
14371455

14381456

14391457
<h1 id="describing-the-interface">Describing the interface</h1>
1440-
<p>There are two ways of describing your interface:
1441-
1 - with a UDL file (a type of IDL, Interface Definition Language);
1442-
2 - with <a href="../proc_macro/index.html">Rust procmacros</a>, similarly to how <code>wasm-bindgen</code> work. This avoids repeating your interface definitions in a separate file. Unfortunately, our docs aren't quite as good for that yet though.</p>
1443-
<h2 id="the-udl-file">The UDL File</h2>
1458+
<p>There are two ways of describing your interface:</p>
1459+
<h2 id="proc-macros">Proc macros</h2>
1460+
<p><a href="../proc_macro/index.html">Rust proc macros</a> can describe your interface - you'd define the function as:</p>
1461+
<div class="highlight"><pre><span></span><code>#[uniffi::export]
1462+
fn add(a: u32, b: u32) -&gt; u32 { a + b }
1463+
</code></pre></div>
1464+
<p>You need to <a href="./Rust_scaffolding.md&quot;">set up the scaffolding</a> with <code>uniffi::setup_scaffolding!()</code> - but that's it.</p>
1465+
<h2 id="a-udl-file">A UDL File</h2>
14441466
<p>We describe in a UDL (a type of IDL, Interface Definition Language) file <em>what</em> is exposed and available to foreign-language bindings. In this case, we are only playing with primitive types (<code>u32</code>) and not custom data structures but we still want to expose the <code>add</code> method.<br />
14451467
Let's create a <code>math.udl</code> file in the <code>math</code> crate's <code>src/</code> folder:</p>
14461468
<div class="highlight"><pre><span></span><code>namespace<span class="w"> </span>math<span class="w"> </span>{

next/udl/namespace.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,15 +1381,16 @@
13811381

13821382

13831383
<h1 id="namespace">Namespace</h1>
1384+
<p>Every crate has a UniFFI "namespace". This is the name exposed to the foreign bindings, typically as a module.</p>
13841385
<p>Every UDL file <em>must</em> have a <code>namespace</code> block:</p>
13851386
<div class="highlight"><pre><span></span><code>namespace<span class="w"> </span>math<span class="w"> </span>{
13861387
<span class="w"> </span><span class="nb">double</span><span class="w"> </span><span class="nb">exp</span>(<span class="nb">double</span><span class="w"> </span>a);
13871388
};
13881389
</code></pre></div>
1389-
<p>It serves multiple purposes:
1390-
- It identifies the name of the generated Rust scaffolding file <code>&lt;namespace&gt;.uniffi.rs</code>.
1391-
- It identifies the package name of the generated foreign-language bindings (e.g. <code>uniffi.&lt;namespace&gt;</code> in Kotlin)
1392-
- It also contains all <a href="functions.html">top-level <em>functions</em></a> that get exposed to foreign-language bindings.</p>
1390+
<p>which might be used as <code>from math import exp</code>/<code>import math</code>/<code>import omg.wtf.math.exp</code> etc.</p>
1391+
<p>Proc macros use the crate name as the namespace by default, but it can be specified with</p>
1392+
<div class="highlight"><pre><span></span><code><span class="n">uniffi</span><span class="p">::</span><span class="n">setup_scaffolding</span><span class="o">!</span><span class="p">(</span><span class="s">&quot;namespace&quot;</span><span class="p">);</span>
1393+
</code></pre></div>
13931394

13941395

13951396

0 commit comments

Comments
 (0)