Skip to content

Commit

Permalink
Prefer FromIter<*AxisProcessor> over From<Vec<*AxisProcessor>>
Browse files Browse the repository at this point in the history
  • Loading branch information
Shute052 committed Apr 29, 2024
1 parent 654d6c8 commit ad15bdc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion examples/input_processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ fn spawn_player(mut commands: Commands) {
.insert(
Action::LookAround,
// You can also use a sequence of processors as the processing pipeline.
DualAxis::mouse_motion().replace_processor(DualAxisProcessor::from(vec![
DualAxis::mouse_motion().replace_processor(DualAxisProcessor::from_iter([
// The first processor is a circular deadzone.
CircleDeadZone::new(0.1).into(),
// The next processor doubles inputs normalized by the deadzone.
Expand Down
16 changes: 8 additions & 8 deletions src/input_processing/dual_axis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub enum DualAxisProcessor {
///
/// assert_eq!(
/// expected,
/// DualAxisProcessor::from(vec![
/// DualAxisProcessor::from_iter([
/// DualAxisInverted::ALL.into(),
/// DualAxisSensitivity::all(2.0).into(),
/// ])
Expand Down Expand Up @@ -136,9 +136,9 @@ impl DualAxisProcessor {
}
}

impl From<Vec<DualAxisProcessor>> for DualAxisProcessor {
fn from(value: Vec<DualAxisProcessor>) -> Self {
Self::Pipeline(value.into_iter().map(Arc::new).collect())
impl FromIterator<DualAxisProcessor> for DualAxisProcessor {
fn from_iter<T: IntoIterator<Item = DualAxisProcessor>>(iter: T) -> Self {
Self::Pipeline(iter.into_iter().map(Arc::new).collect())
}
}

Expand Down Expand Up @@ -313,19 +313,19 @@ mod tests {
}

#[test]
fn test_dual_axis_processor_from_list() {
fn test_dual_axis_processor_from_iter() {
assert_eq!(
DualAxisProcessor::from(vec![]),
DualAxisProcessor::from_iter([]),
DualAxisProcessor::Pipeline(vec![])
);

assert_eq!(
DualAxisProcessor::from(vec![DualAxisInverted::ALL.into()]),
DualAxisProcessor::from_iter([DualAxisInverted::ALL.into()]),
DualAxisProcessor::Pipeline(vec![Arc::new(DualAxisInverted::ALL.into())])
);

assert_eq!(
DualAxisProcessor::from(vec![
DualAxisProcessor::from_iter([
DualAxisInverted::ALL.into(),
DualAxisSensitivity::all(2.0).into(),
]),
Expand Down
4 changes: 2 additions & 2 deletions src/input_processing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
//!
//! You can also use these methods to create a pipeline.
//!
//! - [`AxisProcessor::with_processor`] or [`From<Vec<AxisProcessor>>::from`] for [`AxisProcessor::Pipeline`].
//! - [`DualAxisProcessor::with_processor`] or [`From<Vec<DualAxisProcessor>>::from`] for [`DualAxisProcessor::Pipeline`].
//! - [`AxisProcessor::with_processor`] or [`FromIterator<AxisProcessor>::from_iter`] for [`AxisProcessor::Pipeline`].
//! - [`DualAxisProcessor::with_processor`] or [`FromIterator<DualAxisProcessor>::from_iter`] for [`DualAxisProcessor::Pipeline`].
//!
//! ## Inversion
//!
Expand Down
22 changes: 11 additions & 11 deletions src/input_processing/single_axis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub enum AxisProcessor {
///
/// assert_eq!(
/// expected,
/// AxisProcessor::from(vec![
/// AxisProcessor::from_iter([
/// AxisProcessor::Inverted,
/// AxisProcessor::Sensitivity(2.0),
/// ])
Expand Down Expand Up @@ -141,9 +141,9 @@ impl AxisProcessor {
}
}

impl From<Vec<AxisProcessor>> for AxisProcessor {
fn from(value: Vec<AxisProcessor>) -> Self {
Self::Pipeline(value.into_iter().map(Arc::new).collect())
impl FromIterator<AxisProcessor> for AxisProcessor {
fn from_iter<T: IntoIterator<Item = AxisProcessor>>(iter: T) -> Self {
Self::Pipeline(iter.into_iter().map(Arc::new).collect())
}
}

Expand Down Expand Up @@ -208,19 +208,19 @@ mod tests {
}

#[test]
fn test_axis_processor_from_list() {
assert_eq!(AxisProcessor::from(vec![]), AxisProcessor::Pipeline(vec![]));
fn test_axis_processor_from_iter() {
assert_eq!(
AxisProcessor::from_iter([]),
AxisProcessor::Pipeline(vec![])
);

assert_eq!(
AxisProcessor::from(vec![AxisProcessor::Inverted]),
AxisProcessor::from_iter([AxisProcessor::Inverted]),
AxisProcessor::Pipeline(vec![Arc::new(AxisProcessor::Inverted)]),
);

assert_eq!(
AxisProcessor::from(vec![
AxisProcessor::Inverted,
AxisProcessor::Sensitivity(2.0),
]),
AxisProcessor::from_iter([AxisProcessor::Inverted, AxisProcessor::Sensitivity(2.0)]),
AxisProcessor::Pipeline(vec![
Arc::new(AxisProcessor::Inverted),
Arc::new(AxisProcessor::Sensitivity(2.0)),
Expand Down

0 comments on commit ad15bdc

Please sign in to comment.