MovingTurtle

Trait MovingTurtle 

Source
pub trait MovingTurtle {
    type Item;

    // Required methods
    fn inner(&self) -> &BaseTurtle;
    fn inner_mut(&mut self) -> &mut BaseTurtle;
    fn forward(&mut self, distance: Self::Item);
}
Expand description

A simple trait for an integer-valued Turtle.

Any implementation of this trait should contain a BaseTurtle struct which is referred to by the inner and inner_mut methods. This BaseTurtle deals with storing the turtle’s current position, and drawing lines as appropriate.

The real meat and potatoes of this trait is the forward method, which is how someone would actually move your turtle. Your implementation should be responsible for keeping track of the turtle’s heading, and forward should move your turtle in that direction (using self.inner_mut().delta_move(dx, dy)).

§Example

The following DumbTurtle only moves to the right.

use dcc_lsystem::turtle::{BaseTurtle, MovingTurtle};

struct DumbTurtle {
    inner: BaseTurtle,
}

impl MovingTurtle for DumbTurtle {
    type Item = i32;

    fn inner(&self) -> &BaseTurtle {
        &self.inner
    }

    fn inner_mut(&mut self) -> &mut BaseTurtle {
        &mut self.inner
    }

    fn forward(&mut self, distance: i32) {
        self.inner_mut().delta_move(distance, 0);
    }
}

Required Associated Types§

Required Methods§

Source

fn inner(&self) -> &BaseTurtle

Returns a reference to the wrapped BaseTurtle.

Source

fn inner_mut(&mut self) -> &mut BaseTurtle

Returns a mutable reference to the wrapped BaseTurtle.

Source

fn forward(&mut self, distance: Self::Item)

Moves the turtle forward by distance.

Trait Implementations§

Source§

impl<T> TurtleContainer for dyn MovingTurtle<Item = T>

Every turtle contains a turtle.

Source§

type Item = T

Source§

fn inner(&self) -> &dyn MovingTurtle<Item = Self::Item>

Implementors§