Returning a borrow from an owned resource
<p>I am trying to write a function that maps an <code>Arc&lt;[T]&gt;</code> into an <code>Iterable</code>, for use with <code>flat_map</code> (that is, I want to call <code>i.flat_map(my_iter)</code> for some other <code>i: Iterator&lt;Item=Arc&lt;[T]&gt;&gt;</code>).</p>

<pre><code>fn my_iter&lt;'a, T&gt;(n: Arc&lt;[T]&gt;) -&gt; slice::Iter&lt;'a, T&gt; {
    let t: &amp;'a [T] = &amp;*n.clone();
    t.into_iter()
}
</code></pre>

<p>The function above does not work because <code>n.clone()</code> produces an owned value of type <code>Arc&lt;[T]&gt;</code>, which I can dereference to <code>[T]</code> and then borrow to get <code>&amp;[T]</code>, but the lifetime of the borrow only lasts until the end of the function, while the <code>'a</code> lifetime lasts until the client drops the returned iterator.</p>

<p>How do I clone the <code>Arc</code> in such a way that the client takes ownership of the clone, so that the value is only dropped after the client is done with the iterator (assuming no one else is using the <code>Arc</code>)?</p>

<hr>

<p>Here's some sample code for the source iterator:</p>

<pre><code>struct BaseIter&lt;T&gt;(Arc&lt;[T]&gt;);

impl&lt;T&gt; Iterator for BaseIter&lt;T&gt; {
    type Item = Arc&lt;[T]&gt;;

    fn next(&amp;mut self) -&gt; Option&lt;Self::Item&gt; {
        Some(self.0.clone())
    }
}
</code></pre>

<p>How do I implement the result of <code>BaseIter(data).flat_map(my_iter)</code> (which is of type <code>Iterator&lt;&amp;T&gt;</code>) given that <code>BaseIter</code> is <em>producing</em> data, not just borrowing it? (The real thing is more complicated than this, it's not always the same result, but the ownership semantics are the same.)</p>