Smart pointer which owns its target

Not sure if it's so clear. Depending on how specialization turns out, Owned might still be a case for std::borrow. Anyway, I shifted this discussion to URLO.

I would like to thank particularly @CAD97, @conradludgate, and @SkiFire13 for their input. :heart:

I think I gained a better understanding of the whole issue throughout this discussion. :smiley:

For those who are interested in my conclusion in this matter, you may take a look at the solution of the URLO thread that was created out of this one.

One case:

fn generic_fn(arg: impl GenericCow<Borrowed = str>) {
    let reference: &str = &*arg; // or: arg.borrow()
    assert_eq!(reference, "Hello");
    let owned: String = arg.into_owned();
    assert_eq!(owned, "Hello".to_string());
}

(source of test)

Here GenericCow::into_owned will not perform an unnecessary clone if it's known at compile time that the function is called with an Owned value. Note that .into_owned() might be in a branch that's not always executed (the typical copy-on-write scenario).

The proposed wrapper (Owned<B>) is no longer owning its deref target but owns a value of type <B as ToOwned>::Owned and uses the same semantics as Cow::<'_, B>::Owned to provide a Deref<Target = B> implementation (see source in std and compare with source of deref_owned::Owned::deref implementation).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.