Skip to content
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

WIP: Automagic number converting #247

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could also be written as:

Suggested change
f64: From<T>,
Distance: 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