Re:Storing unboxed trait objects in Rust
by v_lisivka on Saturday December 15, 2018 @06:53PM
(#18690992)
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.
[ Parent
| on HN ]
Re:Storing unboxed trait objects in Rust
by MHordecki on Saturday December 15, 2018 @07:10PM
(#18691032)
Note that it's quite possible for a trait to have infinitely many implementations in any given program. For example, Debug is implemented by u64, Box<u64>, Box<Box<u64>>, ..., and so on. While these happen to have the same size, it's not hard to imagine a situation where this is not the case.
Re:Storing unboxed trait objects in Rust
by majewsky on Saturday December 15, 2018 @07:13PM
(#18691043)
Your situation is already sufficiently impossible because it shows that for a given trait, the set of implementors is often (countably) infinite.
Re:Storing unboxed trait objects in Rust
by comex on Saturday December 15, 2018 @07:41PM
(#18691132)
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<T> Foo for T {
fn foo() {
<Box<T> 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 :)
Re:Storing unboxed trait objects in Rust
by oconnor663 on Tuesday December 18, 2018 @01:39PM
(#18709228)
> Rust implements generics solely through monomorphization
That doesn't sound right. Trait objects use dynamic dispatch.
Re:Storing unboxed trait objects in Rust
by Gladdyu on Saturday December 15, 2018 @06:58PM
(#18691002)
Would this not require all impls to be in the same translation unit or the linker has to be explicitly aware of it?
Re:Storing unboxed trait objects in Rust
by v_lisivka on Sunday December 16, 2018 @02:25PM
(#18694670)
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
by giornogiovanna on Saturday December 15, 2018 @11:55PM
(#18691807)
You can obviously use an `enum` for this, and it's not so clunky that I think Rust should add yet another feature for it.