make the movement commands accept anything into the unit

This commit is contained in:
Dietrich 2022-12-19 09:43:38 +01:00
parent 7bdc83c760
commit 043e44b7f2

View File

@ -27,29 +27,52 @@ impl TurtlePlan {
pub fn new() -> TurtlePlan { pub fn new() -> TurtlePlan {
TurtlePlan { commands: vec![] } TurtlePlan { commands: vec![] }
} }
pub fn forward(&mut self, length: Length) { }
pub trait DirectionalMovement: WithCommands {
fn forward<IntoDistance>(&mut self, length: IntoDistance) -> &mut Self
where
Length: From<IntoDistance>,
{
let length: Length = length.into();
self.get_mut_commands() self.get_mut_commands()
.push(TurtleSegment::Single(DrawElement::Draw( .push(TurtleSegment::Single(DrawElement::Draw(
crate::commands::MoveCommand::Forward(length), crate::commands::MoveCommand::Forward(length),
))) )));
self
} }
pub fn backward(&mut self, length: Precision) { fn backward<IntoDistance>(&mut self, length: IntoDistance) -> &mut Self
where
Length: From<IntoDistance>,
{
let length: Length = length.into();
self.get_mut_commands() self.get_mut_commands()
.push(TurtleSegment::Single(DrawElement::Draw( .push(TurtleSegment::Single(DrawElement::Draw(
crate::commands::MoveCommand::Backward(Length(length)), crate::commands::MoveCommand::Backward(length),
))) )));
self
} }
} }
impl DirectionalMovement for TurtlePlan {}
pub trait Turnable: WithCommands { pub trait Turnable: WithCommands {
fn right(&mut self, angle: Angle<Precision>) -> &mut Self { fn right<IntoAngle>(&mut self, angle: IntoAngle) -> &mut Self
where
Angle<Precision>: From<IntoAngle>,
{
let angle: Angle<Precision> = angle.into();
self.get_mut_commands() self.get_mut_commands()
.push(TurtleSegment::Single(DrawElement::Orient( .push(TurtleSegment::Single(DrawElement::Orient(
crate::commands::OrientationCommand::Right(angle), crate::commands::OrientationCommand::Right(angle),
))); )));
self self
} }
fn left(&mut self, angle: Angle<Precision>) -> &mut Self { fn left<IntoAngle>(&mut self, angle: IntoAngle) -> &mut Self
where
Angle<Precision>: From<IntoAngle>,
{
let angle: Angle<Precision> = angle.into();
self.get_mut_commands() self.get_mut_commands()
.push(TurtleSegment::Single(DrawElement::Orient( .push(TurtleSegment::Single(DrawElement::Orient(
crate::commands::OrientationCommand::Left(angle), crate::commands::OrientationCommand::Left(angle),