-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SystemParam derive leaks private fields #4200
Comments
I would like to tackle this issue. A minimal example to reproduce would be nice. |
Thanks for the quick reply! I cooked up a min example here. Let me know if that's not exactly what you were looking for. |
Yesterday I came up with one and was hoping to solve the issue with some slight change to the After looking at your example today, now I understand it and think of it as a legitimate feature to request. I believe this can be achieved by introducing, say, a new This only means that implementing this will take a while, if someone else does not do it before me :) |
Yet again, I am not even sure that such a filter is feasible. |
So I've tried looking at the generated code using impl<'w, 's> bevy::ecs::system::SystemParam for OpaqueParams<'w, 's> {
type Fetch = OpaqueParamsState<(
<Query<'w, 's, Read<Private>> as bevy::ecs::system::SystemParam>::Fetch,
)>;
} I think a fairly easy fix would be to just use an opaque type for #[doc(hidden)]
pub type OpaqueFetch = impl for<'w, 's> SystemParamFetch<'w, 's>;
// the purpose of this entire module is just to fulfill the "defining scope"
// for the above opaque type.
// We simply define a function that never gets called in order to clue the compiler
// in on what the concrete type of `OpaqueFetch` is.
mod define {
use super::*;
#[allow(unreachable_code)]
#[allow(unused)]
fn dummy<'w, 's>() -> OpaqueFetch {
use bevy::ecs::system::SystemParamState as _;
<OpaqueParamsState<
(<Query<'w, 's, Read<Private>> as bevy::ecs::system::SystemParam>::Fetch,),
>>::init(unreachable!(), unreachable!(), unreachable!())
}
}
impl<'w, 's> bevy::ecs::system::SystemParam for OpaqueParams<'w, 's> {
type Fetch = OpaqueFetch;
} This almost works, but fails when I try to actually use it as a SystemParam due to some inscrutable type errors involving
Hopefully that should be fixable by someone who knows more about the inner-workings of bevy. Otherwise, it works perfectly, satisfying type inference without leaking private fields. Of course, none of this is possible yet since |
If you have params, say for a So does it make sense to have T private ? If you make T public, that does not mean T fields are public. You can hide all your fields and consumer can only use public method that encapsulate all your logic that you want to expose. |
Unfortunately though I don't know if this would be enough to solve your problem as Whether this is a problem depends on exactly the intent of the privacy system. |
# Objective - Fix bevyengine#4200 Currently, `#[derive(SystemParam)]` publicly exposes each field type, which makes it impossible to encapsulate private fields. ## Solution Previously, the fields were leaked because they were used as an input generic type to the macro-generated `SystemParam::State` struct. That type has been changed to store its state in a field with a specific type, instead of a generic type. --- ## Changelog - Fixed a bug that caused `#[derive(SystemParam)]` to leak the types of private fields.
# Objective - Fix bevyengine#4200 Currently, `#[derive(SystemParam)]` publicly exposes each field type, which makes it impossible to encapsulate private fields. ## Solution Previously, the fields were leaked because they were used as an input generic type to the macro-generated `SystemParam::State` struct. That type has been changed to store its state in a field with a specific type, instead of a generic type. --- ## Changelog - Fixed a bug that caused `#[derive(SystemParam)]` to leak the types of private fields.
Bevy version
0.6
What you did
Made a
SystemParam
struct with the following definition:What you expected to happen
I would have a publicly-accessible
SystemParam
struct with properly encapsulated private fields. This would allow a module to let its parent pass queries or resources into it without exposing the specific implementation details of the module.What actually happened
Got an error due to private types being leaked.
I suspect that this is due to the derived impl inserting a
: SystemParam
trait bound for each field, which leaks the private types since trait bounds are public.Additional information
In my case the fix is fairly easy: I can just add
pub(super)
to each struct definition and not worry. However, this solution is not ideal for plugin authors who want to make customSystemParam
s, as it forces them to expose their private types. The best they could do is#[doc(hidden)]
.The text was updated successfully, but these errors were encountered: