TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Rust, Reflection and Access Rules

40 pointsby misonic5 months ago

7 comments

lilyball5 months ago
It seems a bit weird to me to dedicate all this space to talking about access to private fields and respecting field access rules, and not even suggest the idea that a reflection API should actually provide different levels of information depending on where you&#x27;re invoking it from. Which is to say, if I&#x27;m writing code inside the module that defines a type, then I should be able to reflect on that type&#x27;s private fields as my code can otherwise access those private fields. And if I&#x27;m writing code from outside the module then I shouldn&#x27;t be able to reflect on those private fields.<p>One way to accomplish this would be to have reflection add a private method to a reflectable type that returns a mirror that includes information on the private fields. This does require the capability to reflect to be opt-in (and I think this article is assuming that reflection is globally enabled on all types rather than being opt-in), but reflection could plausibly be done as something like<p><pre><code> #[derive(Reflectable)] struct Foo { pub i: i32, x: i32 } </code></pre> such that this expands to something like<p><pre><code> struct Foo { pub i: i32, x: i32 } impl Foo { fn private_mirror() -&gt; Mirror { &#x2F;* construct mirror for all fields *&#x2F; } } impl Reflectable for Foo { fn mirror() -&gt; Mirror { &#x2F;* construct mirror for public fields *&#x2F; } } </code></pre> This way code from within the module can call `private_mirror()` to get a mirror to pass to reflection APIs (and Mirror could implement Reflectable to return itself like how Iterator implements IntoIterator) in order to do things like implement serialization.
alkonaut5 months ago
If I learned right now that Rust had a reflection API then I’d assume that it’s fundamentally unsafe when it comes to handling non public fields. For exactly the reasons the article lists.<p>I think the API with safe public reflection and unsafe private reflection is easy to motivate.<p>The author doesn’t really provide any reason why it’s a bad idea other than “people might abuse it”. The SemVer argument I don’t understand: the versioning is for the public API. Nobody promises that somelib 1.0.0 and 1.0.1 behave remotely the same if I peek behind the curtain.
评论 #42590794 未加载
diggan5 months ago
&gt; Reflection interacts with the safety features of Rust in a somewhat counter-intuitive way. Those interactions force any reflection API to obey certain rules.<p>I know enough Bevy and Rust to know that Bevy does have their own Reflection library (<a href="https:&#x2F;&#x2F;docs.rs&#x2F;bevy_reflect&#x2F;latest&#x2F;bevy_reflect&#x2F;" rel="nofollow">https:&#x2F;&#x2F;docs.rs&#x2F;bevy_reflect&#x2F;latest&#x2F;bevy_reflect&#x2F;</a>), but I don&#x27;t know the internals of it, anyone happens to know that who can compare it to what the author is ideaing about? As a Bevy user and library author, the Reflect API Bevy uses is simple enough at least.
sesm5 months ago
Isn&#x27;t reflection at odds with &quot;zero-cost abstraction&quot; principle?
评论 #42601493 未加载
Animats5 months ago
Not clear on the problem.<p>In Rust, each struct type, to be serialized, must have a serialization and deserialization function of its own. There&#x27;s a macro to generate these by calling the appropriate serialization or deserialization function for each field.<p>For the common types, there&#x27;s a set of standard serialize and deserialize functions. Here&#x27;s the list.[1] Those handle the special cases around Vec, Mutex, and such.<p>The author doesn&#x27;t make a strong case for reflection for other purposes.<p>[1] <a href="https:&#x2F;&#x2F;docs.rs&#x2F;serde&#x2F;latest&#x2F;serde&#x2F;trait.Serialize.html" rel="nofollow">https:&#x2F;&#x2F;docs.rs&#x2F;serde&#x2F;latest&#x2F;serde&#x2F;trait.Serialize.html</a>
cmrx645 months ago
I prototyped <a href="https:&#x2F;&#x2F;github.com&#x2F;emberian&#x2F;serde-reflect">https:&#x2F;&#x2F;github.com&#x2F;emberian&#x2F;serde-reflect</a> if anyone is interested
j-krieger5 months ago
I get what the article is saying. I enjoy Rust, as I enjoy dabbling in other more esoteric programming languages, like Zig. But one thing I miss from the days of early programming is the hacker spirit, in a way. As it master, I <i>should</i> be able to force my machine into giving me access of private fields. I want to be able to poke holes in stuff, to break things and at times, I want the ability to do something stupid just because I want to see what would happen. I feel the same for overly strict compilers. The number one thing I hate about Zig is the compiler treating me like a child when I have unreferenced variables.<p>Modern languages should <i>enable</i> what older languages couldn‘t. They shouldn‘t get in my way needlessly.
评论 #42591059 未加载
评论 #42590623 未加载
评论 #42590698 未加载