I am writing a struct that will wrap a value that cannot be cloneable (e.g. a database connection). I looked for similar examples of what I am trying to achieve but in all cases the wrapped value is cloneable and the Cow smart pointer is used. I couldn't find any smarter pointer that would satisfy my use case, so I am using something along the lines of the following code:
I would like to ask if there is a more idiomatic way in Rust for the code I wrote above, perhaps some existing std struct or library I am not aware of.
If you don’t need both owned and borrowed values to have exactly the same type, you can make your wrapper hold a generic Ptr:Borrow<NonCloneableValue>. This will work for owned values, references, and most smart pointers.
I'm curious regarding what it was in your codebase that was requiring that your type be cloneable in the first place?.. i.e., the code that was trying to clone something (e.g., the db connection) you did not want to clone.
Cow requires the type to implement ToOwned, which is pretty much an extension of Clone (in fact ToOwned is implemented for any type that implements Clone).
So the Cow feature for a db connection provides features that make the wrapper work-around worth it? Where I'm going with this is what is triggering the need to copy the db connection?
It doesn't need to copy the db connection, but just to use the type Cow<'a, DbConnection> somewhere DbConnection need to implement ToOwned. The definition itself just uses the ToOwned::Owned associated type, but by requiring ToOwned to be implemented it also requires the method ToOwned::to_owned to be implemented which in turn requires making a clone of the implementer (in this case DbConnection). However ToOwned::to_owned is used only in Cow::to_mut, Cow::into_owned and Cow::clone, so it's a bad API that it's also required for any other method.
Yes. My understanding was that what triggers the need to copy is a need to write (copy on write). If the copy feature is disabled using the work-around, then how accomplish managing single write access? At that point, what benefit does Cow still provide?