rustborrowingowned-types

Do I have to create distinct structs for both owned (easy-to-use) and borrowed (more efficient) data structures?


I have a Message<'a> which has &'a str references on a mostly short-lived buffer. Those references mandate a specific program flow as they are guaranteed to never outlive the lifetime 'a of the buffer.

Now I also want to have an owned version of Message, such that it can be moved around, sent via threads, etc.

Is there an idiomatic way to achieve this? I thought that Cow<'a, str> might help, but unfortunately, Cow does not magically allocate in case &'a str would outlive the buffer's lifetime.

AFAIK, Cow is not special in the sense that no matter if Cow holds an Owned variant, it must still pass the borrow checker on 'a.

Definition of std::borrow::Cow.

pub enum Cow<'a, B> {
    Borrowed(&'a B),
    Owned(<B as ToOwned>::Owned),
}

Is there an idiomatic way to have an owned variant of Message? For some reason we have &str and String, &[u8] and Vec<u8>, ... does that mean people generally would go for &msg and Message?

I suppose I still have to think about if an owned variant is really, really needed, but my experience shows that having an escape hatch for owned variants generally improves prototyping speed.


Solution

  • Yes, you need to have multiple types, one representing the owned concept and one representing the borrowed concept.

    You'll see the same technique throughout the standard library and third-party crates.

    See also: