Live data from Hacker News

Storing unboxed trait objects in Rust

guiand.xyz

21–25 of 25 posts

Re: Storing unboxed trait objects in Rust

#21
post #7

IMHO, Rust just should take size of the largest implementation of Animal for `impl Animal`, and viola — we can make them static. No heap is good for embedded.

Would this not require all impls to be in the same translation unit or the linker has to be explicitly aware of it?

In case of embedded firmware, all impls will be in same unit. In case of a program with linked libraries, only main program will know sizes of all possible impls.

Re: Storing unboxed trait objects in Rust

#23
post #14

> Rust, not being an object-oriented language, doesn't quite do inheritence like the others. In C++ or Java, subclasses extend superclasses. In Rust, structs implement traits. I wouldn't say that Rust isn't object oriented. It just takes a different approach to some things. Inheritance isn't really a strictly required feature of the object oriented code design. Many for instance pointed out, that composition is prefe…

Rust is clearly not object-oriented. It has very minimal support for late binding, and no support for reflection or dynamic libraries. This isn't a critique, it's just a statement of fact. No Rust designer will say they set out to build a new object-oriented language.

> no support for reflection

Rust does have some support for reflection, though it is quite limited. https://doc.rust-lang.org/std/any

Re: Storing unboxed trait objects in Rust

#24
post #11
post #9

Earlier quoted context omitted.

Your situation is already sufficiently impossible because it shows that for a given trait, the set of implementors is often (countably) infinite.

However, the number of implementors actually instantiated in any given program is necessarily finite. You could write a program that would make it infinite, e.g. trait Foo { fn foo(); } impl Foo for T { fn foo() { as Foo>::foo(); } } ...but the compiler can't compile it: since Rust implements generics solely through monomorphization, that would require generating an infinitely large binary :)

> Rust implements generics solely through monomorphization

That doesn't sound right. Trait objects use dynamic dispatch.

Re: Storing unboxed trait objects in Rust

#25
post #14

> Rust, not being an object-oriented language, doesn't quite do inheritence like the others. In C++ or Java, subclasses extend superclasses. In Rust, structs implement traits. I wouldn't say that Rust isn't object oriented. It just takes a different approach to some things. Inheritance isn't really a strictly required feature of the object oriented code design. Many for instance pointed out, that composition is prefe…

Rust is clearly not object-oriented. It has very minimal support for late binding, and no support for reflection or dynamic libraries. This isn't a critique, it's just a statement of fact. No Rust designer will say they set out to build a new object-oriented language.

You can do compile-time reflection, which is very popular.
Post reply on HN