From 9d142a8025992954842df894fe12407eb0ba2ff9 Mon Sep 17 00:00:00 2001 From: Matty Weatherley Date: Tue, 3 Dec 2024 11:35:14 -0800 Subject: [PATCH] Use `IntoIterator` instead of `Into>` in cubic splines interfaces (#16402) # Objective This was always a bit weird; `IntoIterator` is considered more idiomatic in Rust. The reason these used `Into>` in the first place was (to my knowledge) because of concerns that passing an already-owned vector would cause a redundant allocation if the iterator API was used instead. However, I have looked at simple examples for this scenario and the generated assembly is identical (i.e. `into_iter().collect()` is effectively converted to a no-op). ## Solution As described in the title. ## Testing It compiles. Ran existing tests. ## Migration Guide The cubic splines API now uses `IntoIterator` in places where it used `Into>`. For most users, this will have little to no effect (it is largely more permissive). However, in case you were using some unusual input type that implements `Into>` without implementing `IntoIterator`, you can migrate by converting the input to a `Vec<..>` before passing it into the interface. --- crates/bevy_math/src/cubic_splines.rs | 42 +++++++++++++-------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/crates/bevy_math/src/cubic_splines.rs b/crates/bevy_math/src/cubic_splines.rs index 8e6e880108..31fbc8824b 100644 --- a/crates/bevy_math/src/cubic_splines.rs +++ b/crates/bevy_math/src/cubic_splines.rs @@ -63,9 +63,9 @@ pub struct CubicBezier { #[cfg(feature = "alloc")] impl CubicBezier

{ /// Create a new cubic Bezier curve from sets of control points. - pub fn new(control_points: impl Into>) -> Self { + pub fn new(control_points: impl IntoIterator) -> Self { Self { - control_points: control_points.into(), + control_points: control_points.into_iter().collect(), } } } @@ -287,18 +287,18 @@ pub struct CubicCardinalSpline { #[cfg(feature = "alloc")] impl CubicCardinalSpline

{ /// Build a new Cardinal spline. - pub fn new(tension: f32, control_points: impl Into>) -> Self { + pub fn new(tension: f32, control_points: impl IntoIterator) -> Self { Self { tension, - control_points: control_points.into(), + control_points: control_points.into_iter().collect(), } } /// Build a new Catmull-Rom spline, the special case of a Cardinal spline where tension = 1/2. - pub fn new_catmull_rom(control_points: impl Into>) -> Self { + pub fn new_catmull_rom(control_points: impl IntoIterator) -> Self { Self { tension: 0.5, - control_points: control_points.into(), + control_points: control_points.into_iter().collect(), } } @@ -444,9 +444,9 @@ pub struct CubicBSpline { #[cfg(feature = "alloc")] impl CubicBSpline

{ /// Build a new B-Spline. - pub fn new(control_points: impl Into>) -> Self { + pub fn new(control_points: impl IntoIterator) -> Self { Self { - control_points: control_points.into(), + control_points: control_points.into_iter().collect(), } } @@ -630,11 +630,11 @@ impl CubicNurbs

{ /// /// At least 4 points must be provided, otherwise an error will be returned. pub fn new( - control_points: impl Into>, - weights: Option>>, - knots: Option>>, + control_points: impl IntoIterator, + weights: Option>, + knots: Option>, ) -> Result { - let mut control_points: Vec

= control_points.into(); + let mut control_points: Vec

= control_points.into_iter().collect(); let control_points_len = control_points.len(); if control_points_len < 4 { @@ -643,11 +643,11 @@ impl CubicNurbs

{ }); } - let weights = weights - .map(Into::into) + let weights: Vec = weights + .map(|ws| ws.into_iter().collect()) .unwrap_or_else(|| vec![1.0; control_points_len]); - let mut knots: Vec = knots.map(Into::into).unwrap_or_else(|| { + let mut knots: Vec = knots.map(|ks| ks.into_iter().collect()).unwrap_or_else(|| { Self::open_uniform_knots(control_points_len) .expect("The amount of control points was checked") }); @@ -846,9 +846,9 @@ pub struct LinearSpline { #[cfg(feature = "alloc")] impl LinearSpline

{ /// Create a new linear spline from a list of points to be interpolated. - pub fn new(points: impl Into>) -> Self { + pub fn new(points: impl IntoIterator) -> Self { Self { - points: points.into(), + points: points.into_iter().collect(), } } } @@ -1139,8 +1139,8 @@ pub struct CubicCurve { impl CubicCurve

{ /// Create a new curve from a collection of segments. If the collection of segments is empty, /// a curve cannot be built and `None` will be returned instead. - pub fn from_segments(segments: impl Into>>) -> Option { - let segments: Vec<_> = segments.into(); + pub fn from_segments(segments: impl IntoIterator>) -> Option { + let segments: Vec<_> = segments.into_iter().collect(); if segments.is_empty() { None } else { @@ -1455,8 +1455,8 @@ pub struct RationalCurve { impl RationalCurve

{ /// Create a new curve from a collection of segments. If the collection of segments is empty, /// a curve cannot be built and `None` will be returned instead. - pub fn from_segments(segments: impl Into>>) -> Option { - let segments: Vec<_> = segments.into(); + pub fn from_segments(segments: impl IntoIterator>) -> Option { + let segments: Vec<_> = segments.into_iter().collect(); if segments.is_empty() { None } else {