Live data from Hacker News

Native Reflection in Rust

jack.wrenn.fyi

1–10 of 68 posts

Re: Native Reflection in Rust

#2
This is a beautiful (hacky) demo of something that I didn't think was possible in Rust (yet). I hope other applications don't accidentally start using it just to discover that it doesn't work in release mode.

Very impressive work!

Re: Native Reflection in Rust

#4
post #2

This is a beautiful (hacky) demo of something that I didn't think was possible in Rust (yet). I hope other applications don't accidentally start using it just to discover that it doesn't work in release mode. Very impressive work!

Oh, I should add a note about that. Fortunately, it's quite easy to tell Rust to generate debuginfo even in release mode.

Re: Native Reflection in Rust

#5
post #3

Today I learn that Rust does not have reflection.

Reflection is usually not available in AoT compiled languages. The prevalent Rust coding styles rely heavily on monomorphic data types and functions, meaning there's nothing left to reflect at runtime. But if you want to deal with trait objects and need to access the underlying type, you need to use Any::downcast or rely on annotations on every type you want to reflect on. Or now, leverage DWARF info on Linux with deflect.

Re: Native Reflection in Rust

#6
post #5
post #3

Today I learn that Rust does not have reflection.

Reflection is usually not available in AoT compiled languages. The prevalent Rust coding styles rely heavily on monomorphic data types and functions, meaning there's nothing left to reflect at runtime. But if you want to deal with trait objects and need to access the underlying type, you need to use Any::downcast or rely on annotations on every type you want to reflect on. Or now, leverage DWARF info on Linux with de…

What are monomorphic data types? What should be my first read on the subject?

Re: Native Reflection in Rust

#7
post #3

Today I learn that Rust does not have reflection.

Reflection is typically provided by a runtime, and languages that don't have runtimes usually don't have it. You shouldn't expect a low-level systems language to have reflection. There is no zero-cost way of implementing it.

Re: Native Reflection in Rust

#8
post #3

Today I learn that Rust does not have reflection.

Reflection is typically provided by a runtime, and languages that don't have runtimes usually don't have it. You shouldn't expect a low-level systems language to have reflection. There is no zero-cost way of implementing it.

Except Rust has runtime: [0]. And so, usually, does C (in hosted implementations).

[0] https://doc.rust-lang.org/reference/runtime.html

Re: Native Reflection in Rust

#9
post #5

Earlier quoted context omitted.

Reflection is usually not available in AoT compiled languages. The prevalent Rust coding styles rely heavily on monomorphic data types and functions, meaning there's nothing left to reflect at runtime. But if you want to deal with trait objects and need to access the underlying type, you need to use Any::downcast or rely on annotations on every type you want to reflect on. Or now, leverage DWARF info on Linux with de…

What are monomorphic data types? What should be my first read on the subject?

It's a fancy way of saying "every time this type is used, replace all the generic type params with what was used and generate code for it". It's how generics are implemented in Rust. If you have

    struct Foo(T);
And you create Foo(42i32) and Foo(0.0f64), the compiler will create the equivalent to

    struct Fooi32(i32);
    struct Foof64(f64);
In other languages like Java, generics are implemented the way that Rust does "trait objects" (&dyn Trait).

Rust is not the only language that does this, to be clear.

If you're interested in a quick intro on the compiler side of this, you can read https://rustc-dev-guide.rust-lang.org/backend/monomorph.html

Re: Native Reflection in Rust

#10
Does using DWARF info imply that this will break when you strip the resulting executable? I often strip my Rust binaries because it practically halves the application size, which can become quite a lot in a language where you're statically linking everything.

Regardless, quite an ingenious use of standard ELF features, I didn't think this would be possible in Rust without adding some kind of VM around reflection code.

Post reply on HN