bevy/crates/bevy_math
Multirious 70da903cec
Add methods to return the inner value for direction types (#12516)
# Objective

Currently in order to retrieve the inner values from direction types is
that you need to use the `Deref` trait or `From`/`Into`. `Deref` that is
currently implemented is an anti-pattern that I believe should be less
relied upon.
This pull-request add getters for retrieving the inner values for
direction types.

Advantages of getters:
- Let rust-analyzer to list out available methods for users to
understand better to on how to get the inner value. (This happens to me.
I really don't know how to get the value until I look through the source
code.)
- They are simple.
- Generally won't be ambiguous in most context. Traits such as
`From`/`Into` will require fully qualified syntax from time to time.
- Unsurprising result.

Disadvantages of getters:
- More verbose

Advantages of deref polymorphism:
- You reduce boilerplate for getting the value and call inner methods
by:
  ```rust
  let dir = Dir3::new(Vec3::UP).unwrap();
  // getting value
  let value = *dir;
  // instead of using getters
  let value = dir.vec3();

  // calling methods for the inner vector
  dir.xy();
  // instead of using getters
  dir.vec3().xy();
  ```

Disadvantages of deref polymorphism:
- When under more level of indirection, it will requires more
dereferencing which will get ugly in some part:
  ```rust
  // getting value
  let value = **dir;
  // instead of using getters
  let value = dir.vec3();

  // calling methods for the inner vector
  dir.xy();
  // instead of using getters
  dir.vec3().xy();
  ```

[More detail
here](https://rust-unofficial.github.io/patterns/anti_patterns/deref.html).


Edit: Update information for From/Into trait.
Edit: Advantages and disadvantages.

## Solution

Add `vec2` method for Dir2.
Add `vec3` method for Dir3.
Add `vec3a` method for Dir3A.
2024-03-18 17:49:58 +00:00
..
src Add methods to return the inner value for direction types (#12516) 2024-03-18 17:49:58 +00:00
Cargo.toml Uniform point sampling methods for some primitive shapes. (#12484) 2024-03-17 14:48:16 +00:00