Cow for Pair/Tuples

Is there an implementation for Cow<(str, str)>? I have a type that gets a list of string pairs, and renders this to text, in the best case. But it should be possible to replace pairs in the list, which makes it necessary to own these strings. Hence, I'm looking for something like Cow with (str, str) as borrowed and (String, String) as owned.

Would a tuple of Cows, (Cow<str>, Cow<str>), work for you?

1 Like

(str, str) cannot exist as a type (even inside Cow). The reason is simple: tuples store their components inline, and str is unsized, so the position of the second element would be unknown.


Yes, this would be a workaround, but not really address the nature of a pair.

Currently, I'm using my own CowPair:

pub enum CowPair<'a> {
    /// Borrowed data
    Borrowed(&'a str, &'a str),

    /// Owned data
    Owned(String, String),

You could use a newtype tuple struct Pair<'a>(&'a str, &'a str) with an owned version struct OwnedPair(String, String) and implement ToOwned and Borrow for the Pair tuple. Then you could have a Cow of your pair.

You can't. ToOwned::Owned: Borrow<Self>, which would require a borrow(&OwnedPair(String, String)) -> &Pair<'a>.