Skip to content

Commit

Permalink
WIP: Proof of concept: Auto convert numbers to f64
Browse files Browse the repository at this point in the history
  • Loading branch information
enaut committed Jul 30, 2021
1 parent c9918c4 commit 1bbd751
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
22 changes: 18 additions & 4 deletions examples/square.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
use turtle::Turtle;

struct MyCoord {
x: u8,
}

impl From<MyCoord> for f64 {
fn from(source: MyCoord) -> Self {
f64::from(source.x)
}
}

fn main() {
let mut turtle = Turtle::new();

for _ in 0..4 {
turtle.forward(200.0);
turtle.right(90.0);
}
turtle.forward(200);
turtle.right(90);
turtle.forward(200u8);
turtle.right(90f32);
turtle.forward(200.0);
turtle.right(90.0);
turtle.forward(MyCoord { x: 200 });
turtle.right(MyCoord { x: 90 });
}
12 changes: 10 additions & 2 deletions src/async_turtle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ impl AsyncTurtle {
Self {client, id, angle_unit}
}

pub async fn forward(&mut self, distance: Distance) {
pub async fn forward<T>(&mut self, distance: T)
where
f64: From<T>,
{
let distance = f64::from(distance);
self.client.move_forward(self.id, distance).await
}

Expand All @@ -80,7 +84,11 @@ impl AsyncTurtle {
self.client.move_forward(self.id, -distance).await
}

pub async fn right(&mut self, angle: Angle) {
pub async fn right<Angle>(&mut self, angle: Angle)
where
f64: From<Angle>,
{
let angle = f64::from(angle);
let angle = self.angle_unit.to_radians(angle);
self.client.rotate_in_place(self.id, angle, RotationDirection::Clockwise).await
}
Expand Down
10 changes: 8 additions & 2 deletions src/turtle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ impl Turtle {
/// turtle.forward(-223.0);
/// # assert_eq!(turtle.position().y.round(), -113.0);
/// ```
pub fn forward(&mut self, distance: Distance) {
pub fn forward<Distance>(&mut self, distance: Distance)
where
f64: From<Distance>,
{
block_on(self.turtle.forward(distance))
}

Expand Down Expand Up @@ -154,7 +157,10 @@ impl Turtle {
/// # let expected = (expected * 1e5).trunc();
/// # assert_eq!((turtle.heading() * 1e5).trunc(), expected);
/// ```
pub fn right(&mut self, angle: Angle) {
pub fn right<Angle>(&mut self, angle: Angle)
where
f64: From<Angle>,
{
block_on(self.turtle.right(angle))
}

Expand Down

0 comments on commit 1bbd751

Please sign in to comment.