-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Reduce code duplication between Transform and GlobalTransform #2026
Description
What problem does this solve or what need does it fill?
Currently, Transform and GlobalTransform have effectively the same interface, just as different components. They are implemented as completely different types. Code is duplicated between the two and there is no simple way to write functions that are generic over both.
What solution would you like?
Make the definition of GlobalTransform:
#[repr(transparent)]
struct GlobalTransform(pub Transform);What alternative(s) have you considered?
Creating a trait implemented by both transform types. I don't think there's any real reason for this because the actual implementation details are identical, as far as I know. A trait would make sense if the implementation differed, but since they don't I believe composition makes more sense.
Another thing we could add is a Deref<Target = Transform> impl for GlobalTransform. This would get rid of some global_transform.0 calls, but I think it's not worth it. Also, even though this is a common pattern in the rust ecosystem, it goes directly against the standard library's documentation for the intended usage of the Deref trait:
Implementing Deref for smart pointers makes accessing the data behind them convenient, which is why they implement Deref. On the other hand, the rules regarding Deref and DerefMut were designed specifically to accommodate smart pointers. Because of this, Deref should only be implemented for smart pointers to avoid confusion.
Additional context
This would be a breaking change, and would require a small modification to any code that uses global transforms.
The #[repr(transparent)] isn't strictly necessary, but there's no real reason not to add it.
I'm happy to write a PR for this if people agree that it's a good idea.