Skip to content

Commit

Permalink
deploy: 864cb4e
Browse files Browse the repository at this point in the history
  • Loading branch information
chinedufn committed Feb 6, 2025
1 parent 1a46e9a commit 820d444
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
13 changes: 5 additions & 8 deletions built-in/string/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ <h1 class="menu-title">The swift-bridge Book</h1>

<div id="content" class="content">
<main>
<h1 id="string-----string"><a class="header" href="#string-----string">String &lt;---&gt; String</a></h1>
<h1 id="rust-stdstringstring-----swift-string"><a class="header" href="#rust-stdstringstring-----swift-string">Rust <code>std::string::String</code> &lt;---&gt; Swift <code>String</code></a></h1>
<p>Rust's <code>std::string::String</code> can be passed to Swift as an owned <code>String</code>, a referenced <code>&amp;String</code> or a mutably referenced <code>&amp;mut String</code>.</p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
Expand Down Expand Up @@ -194,13 +194,10 @@ <h1 id="string-----string"><a class="header" href="#string-----string">String &l
}
</code></pre>
<h2 id="ruststring"><a class="header" href="#ruststring">RustString</a></h2>
<p>Since both Rust and Swift have their own <code>String</code> type, it is technically possible for us to automatically
convert owned Rust <code>std::string::String</code>s into Swift <code>String</code>s.</p>
<p>However, we do not do this since Swift does not currently have a way to construct a Swift <code>String</code> without copying.</p>
<p>This means that if we were to automatically convert from a Rust <code>std::string::String</code> to a Swift <code>String</code> we would have to create a new
Swift allocation and copy all of the Rust <code>std::string::String</code> bytes to that allocation.</p>
<p><code>swift-bridge</code> seeks to avoid unnecessary allocations, so instead of performing this implicit allocation
we pass a <code>RustString</code> type from Rust to Swift.</p>
<p>It is technically possible for us to automatically convert owned Rust <code>std::string::String</code>s into Swift <code>String</code>s,
but we do not do this because it would require copying the Rust <code>std::string::String</code> bytes to a new <code>Swift</code> allocation.</p>
<p>This is because there is no way to construct a Swift <code>String</code> without copying.</p>
<p><code>swift-bridge</code> seeks to avoid unnecessary allocations, so instead of performing an implicit allocation we pass a <code>RustString</code> type from Rust to Swift.</p>
<p>The <code>RustString</code>'s <code>.toString()</code> method can then be called on the Swift side to get a Swift <code>String</code>.</p>

</main>
Expand Down
13 changes: 5 additions & 8 deletions print.html
Original file line number Diff line number Diff line change
Expand Up @@ -2235,7 +2235,7 @@ <h2 id="inline-annotations"><a class="header" href="#inline-annotations">Inline
<tr><td>Have a Rust standard library type in mind?<br /> Open an issue!</td><td></td><td></td></tr>
<tr><td></td><td>Have a Swift standard library type in mind?<br /> Open an issue!</td><td></td></tr>
</tbody></table>
<div style="break-before: page; page-break-before: always;"></div><h1 id="string-----string"><a class="header" href="#string-----string">String &lt;---&gt; String</a></h1>
<div style="break-before: page; page-break-before: always;"></div><h1 id="rust-stdstringstring-----swift-string"><a class="header" href="#rust-stdstringstring-----swift-string">Rust <code>std::string::String</code> &lt;---&gt; Swift <code>String</code></a></h1>
<p>Rust's <code>std::string::String</code> can be passed to Swift as an owned <code>String</code>, a referenced <code>&amp;String</code> or a mutably referenced <code>&amp;mut String</code>.</p>
<pre><pre class="playground"><code class="language-rust">
<span class="boring">#![allow(unused)]
Expand Down Expand Up @@ -2283,13 +2283,10 @@ <h2 id="inline-annotations"><a class="header" href="#inline-annotations">Inline
}
</code></pre>
<h2 id="ruststring"><a class="header" href="#ruststring">RustString</a></h2>
<p>Since both Rust and Swift have their own <code>String</code> type, it is technically possible for us to automatically
convert owned Rust <code>std::string::String</code>s into Swift <code>String</code>s.</p>
<p>However, we do not do this since Swift does not currently have a way to construct a Swift <code>String</code> without copying.</p>
<p>This means that if we were to automatically convert from a Rust <code>std::string::String</code> to a Swift <code>String</code> we would have to create a new
Swift allocation and copy all of the Rust <code>std::string::String</code> bytes to that allocation.</p>
<p><code>swift-bridge</code> seeks to avoid unnecessary allocations, so instead of performing this implicit allocation
we pass a <code>RustString</code> type from Rust to Swift.</p>
<p>It is technically possible for us to automatically convert owned Rust <code>std::string::String</code>s into Swift <code>String</code>s,
but we do not do this because it would require copying the Rust <code>std::string::String</code> bytes to a new <code>Swift</code> allocation.</p>
<p>This is because there is no way to construct a Swift <code>String</code> without copying.</p>
<p><code>swift-bridge</code> seeks to avoid unnecessary allocations, so instead of performing an implicit allocation we pass a <code>RustString</code> type from Rust to Swift.</p>
<p>The <code>RustString</code>'s <code>.toString()</code> method can then be called on the Swift side to get a Swift <code>String</code>.</p>
<div style="break-before: page; page-break-before: always;"></div><h1 id="str-----ruststr"><a class="header" href="#str-----ruststr">&amp;str &lt;---&gt; RustStr</a></h1>
<p>Rust's <code>std::str</code> can be passed to Swift as a <code>RustStr</code>.</p>
Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion searchindex.json

Large diffs are not rendered by default.

0 comments on commit 820d444

Please sign in to comment.