1

I'm trying to implement a class based format in Rust, which doesn't have classes or object-oriented concepts like inheritance.

Consider the following example:

class Creature {
  int id;
  bool alive
};

class Human: public Creature {
  int salary;
};

class Dog: public Creature {
  bool has_fur;
};

If I want to implement the same structure in Rust I would do something like this:

struct Human {
    id: i32,
    alive: bool,
    salary: i32,
}

struct Dog {
    id: i32,
    alive: bool,
    has_fur: bool,
}

Will the duplicated structs make the application require more memory than if they were written in C++ and inherited from a base class (or maybe used prototypes in JavaScript)? The application will hold millions of objects.

I guess it will be a lot of duplicated member names in the code, which is maybe a problem in itself. But how would you translate the data structure in Rust (with the least memory implication as possible)? Or is this the way to go?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
mottosson
  • 3,283
  • 4
  • 35
  • 73
  • 1
    See [How to get the size of a user defined struct? (sizeof)](https://stackoverflow.com/q/36664327/155423) and you can tell yourself what the size of each is. – Shepmaster Jan 13 '20 at 16:50
  • Either in C++, in Rust or in whatever language you want, the information must be stored somewhere. You cannot have that for free. – Boiethios Jan 13 '20 at 16:50
  • @FrenchBoiethios but in C++ and Rust the information is not saved in the executables. – mcarton Jan 13 '20 at 16:52
  • 4
    Why not create `Creature` and have `Human` contain a variable of type `Creature`? – Shepmaster Jan 13 '20 at 16:52
  • @Shepmaster that would be a good idea if the inheritance tree wasn't so deep. In the real project it's not uncommon with 7-10 inheritance levels (don't blame me, it's a standardized file format I'm implementing). And to access a member of the base struct would be really cumbersome. – mottosson Jan 13 '20 at 18:19

2 Answers2

3

No.

Type information is not kept at runtime, therefore structs are free to create.

mcarton
  • 27,633
  • 5
  • 85
  • 95
2

C++ does the following: Each Human occupies some sufficiently many bytes to store id, alive, salary -- in that order (if you do not use any packing attributes (see Force C++ structure to pack tightly)). For memory alignment, the struct fields may be padded, so they actually might occupy some bytes more than you expect.

Similarily for Dog.

Inheriting from a common base class reduces code, but - as you wrote it - does not save memory.

In Rust, each Human occupies sufficiently many bytes to store the fields, but Rust is - without memory layout attributes - allowed to reorder fields to save memory.

Repeating id and alive in two different structs makes code a bit more repetitive, but will not consume more bytes.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
phimuemue
  • 34,669
  • 9
  • 84
  • 115
  • 2
    *but will not consume more bytes* — and based on your logic it might even *reduce* the size in comparison. – Shepmaster Jan 13 '20 at 18:40