Canonical way to have owning/non-owning sibling types


what is an idiomatic way to express the close connection between two types, which are basically the same with the only difference being one is owning, one is not?

struct MyDataOwned{
    name: String,
    numbers: Vec<i32>

struct MyDataView{
    name: &str,
    numbers: &[i32]

Is there maybe a trait that creates MyDataView automatically?
Is something like an enum advisable?

enum MyData{

Doesn't &MyDataOwned works?

So my use case is I am parsing a memmapped file, so I have bytes in memory and then I construct views on that memory. Which means I start with the view and not the owner.
Unless I can project the owned type onto the memmapped file?
C++ would let me do such things :wink:

In this case you can define the type as below.

struct MyData<'a> {
    name: Cow<'a, str>,
    numbers: Cow<'a, [i32]>,

and use `MyData<'static> as a owned type.

As a side note, reading file as &[i32] doesn't seems like a good idea, because that file can be transferred to the machine with different endianness. Most file/network formats enforces big endian to handle this portability problem. The only major format I know that doesn't follows this practice is UTF-16 which is mostly used within MS windows. They inserts BOM(Byte Order Mark) character at the start of every UTF-16 file to detect the file's endianness. But it makes parsing a bit harder, you can easily find a realm of BOM-haters on the internet.

Awesome! Thanks!
Yeah that kind of stuff was lost in my minimal example :slight_smile: