-
Notifications
You must be signed in to change notification settings - Fork 162
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
rename with_*
to set_*
& add new with_*
methods for vector types
#597
base: main
Are you sure you want to change the base?
Conversation
Usually I've seen and expect These are all copy types anyway. The existing |
I don't think the current Current: /// Creates a 3D vector from `self` with the given value of `x`.
#[inline]
#[must_use]
pub fn with_x(mut self, x: f32) -> Self {
self.x = x;
self
} New: /// Returns a new version of this 3D vector with the given `x` value.
#[inline]
#[must_use]
pub fn with_x(&self, x: f32) -> Self {
Self { x, ..*self }
} I think both methods are pretty useful. The new // set_ method is better for initializing
let foo = Vec3::ZERO
.set_x(1.0);
// with_ method is better for cloning
let bar = foo.with_y(1.0);
let x = foo.with_z(1.0);
|
Yes, it creates a copy. That's what #[test]
fn vec3_copy() {
let a = Vec3::splat(1.0);
let b = a.with_x(2.0);
assert_eq!(1.0, a.x);
assert_eq!(2.0, b.x);
} |
Ah, sorry for the misunderstanding, I though you meant create a copy as in creating a new instance with the same value (with the modified field x, y, z, etc.).
Yeah, that is for the |
Your desired behavior for the way it works is how it already works. Look at the example unit test that I posted. The current |
Ah, I didn't understand how it work originally, thank you for explaining it. If both methods work, I think it would be better to explicitly create a copy instead of implicitly via the |
Objective:
The current behavior of the
with_*
methods for vector types is confusing since the name suggests creating and returning a modified copy of the vector without modifying the original. However, these methods currently mutate the original vector in place.There is also no method for creating and returning a copy of the original with modified value.
Solution:
with_*
method toset_*
to more accurately describe their mutating nature.with_*
method to create a copy with modified value.