Skip to content

Commit

Permalink
Merge v0.1.1 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtc committed Feb 8, 2019
1 parent 974f880 commit 3acd378
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mode"
version = "0.1.0"
version = "0.1.1"
authors = ["Andrew Thomas Christensen <[email protected]>"]
edition = "2018"

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Mode for Working {
fn as_base_mut(&mut self) -> &mut Self::Base { self }

// This function allows the current Mode to swap to another Mode, when ready.
fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
if self.hours_worked == 4 || self.hours_worked >= 8 {
// To swap to another Mode, a Transition function is returned, which will consume
// the current Mode and return a new Mode to be swapped in as active.
Expand All @@ -61,7 +61,7 @@ impl Mode for Eating {
type Base = Activity;
fn as_base(&self) -> &Self::Base { self }
fn as_base_mut(&mut self) -> &mut Self::Base { self }
fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
if self.calories_consumed >= 500 {
if self.hours_worked >= 8 {
// Time for bed!
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Mode for Sleeping {
type Base = Activity;
fn as_base(&self) -> &Self::Base { self }
fn as_base_mut(&mut self) -> &mut Self::Base { self }
fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
if self.hours_rested >= 8 {
// Time for breakfast!
Some(Box::new(|_| { Eating { hours_worked: 0, calories_consumed: 0 } }))
Expand Down
6 changes: 3 additions & 3 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Mode for UpMode {
type Base = CounterMode;
fn as_base(&self) -> &Self::Base { self }
fn as_base_mut(&mut self) -> &mut Self::Base { self }
fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
if self.counter == self.target {
// If we've reached the target value, start counting down to (roughly) the median value.
Some(Box::new(
Expand Down Expand Up @@ -68,7 +68,7 @@ impl Mode for DownMode {
type Base = CounterMode;
fn as_base(&self) -> &Self::Base { self }
fn as_base_mut(&mut self) -> &mut Self::Base { self }
fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
const GOAL : i32 = 10;
if self.counter == GOAL {
// When we finally count down to the goal value, end the program by swapping in a "finished" state.
Expand Down Expand Up @@ -107,7 +107,7 @@ impl Mode for FinishedMode {
type Base = CounterMode;
fn as_base(&self) -> &Self::Base { self }
fn as_base_mut(&mut self) -> &mut Self::Base { self }
fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
// We're finished calculating, so we never want to transition.
None
}
Expand Down
8 changes: 4 additions & 4 deletions src/automaton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use std::fmt;
/// # type Base = MyMode;
/// # fn as_base(&self) -> &Self::Base { self }
/// # fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// # fn get_transition(&mut self) -> Option<Box<Transition<Self>>> { None }
/// # fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> { None }
/// # }
/// #
/// // Use with_initial_mode() to create the Automaton with an initial state.
Expand All @@ -65,7 +65,7 @@ use std::fmt;
pub struct Automaton<Base>
where Base : ?Sized
{
current_mode : Box<AnyModeWrapper<Base = Base>>,
current_mode : Box<dyn AnyModeWrapper<Base = Base>>,
}

impl<Base> Automaton<Base>
Expand Down Expand Up @@ -130,7 +130,7 @@ impl<Base> Automaton<Base>
/// type Base = Self;
/// fn as_base(&self) -> &Self { self }
/// fn as_base_mut(&mut self) -> &mut Self { self }
/// fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
/// fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
/// // TODO: Logic for transitioning between states goes here.
/// Some(Box::new(
/// |previous : Self| {
Expand Down Expand Up @@ -195,7 +195,7 @@ impl<Base> Default for Automaton<Base>
/// type Base = MyBase;
/// fn as_base(&self) -> &Self::Base { self }
/// fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// fn get_transition(&mut self) -> Option<Box<Transition<Self>>> { None } // TODO
/// fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> { None } // TODO
/// }
///
/// let automaton = Automaton::with_initial_mode(MyMode { foo: 3, bar: "Hello, World!" });
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
//! fn as_base_mut(&mut self) -> &mut Self::Base { self }
//!
//! // This function allows the current Mode to swap to another Mode, when ready.
//! fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
//! fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
//! if self.hours_worked == 4 || self.hours_worked >= 8 {
//! // To swap to another Mode, a Transition function is returned, which will consume
//! // the current Mode and return a new Mode to be swapped in as active.
Expand All @@ -64,7 +64,7 @@
//! type Base = Activity;
//! fn as_base(&self) -> &Self::Base { self }
//! fn as_base_mut(&mut self) -> &mut Self::Base { self }
//! fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
//! fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
//! if self.calories_consumed >= 500 {
//! if self.hours_worked >= 8 {
//! // Time for bed!
Expand Down Expand Up @@ -93,7 +93,7 @@
//! type Base = Activity;
//! fn as_base(&self) -> &Self::Base { self }
//! fn as_base_mut(&mut self) -> &mut Self::Base { self }
//! fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
//! fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
//! if self.hours_rested >= 8 {
//! // Time for breakfast!
//! Some(Box::new(|_| { Eating { hours_worked: 0, calories_consumed: 0 } }))
Expand Down
10 changes: 5 additions & 5 deletions src/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::Transition;
/// type Base = MyMode;
/// fn as_base(&self) -> &Self::Base { self }
/// fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
/// fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
/// // Transition to ModeB. ModeA can swap to ModeB because both share the same Base.
/// Some(Box::new(|previous : Self| { ModeB }))
/// }
Expand All @@ -51,7 +51,7 @@ use crate::Transition;
/// type Base = MyMode;
/// fn as_base(&self) -> &Self::Base { self }
/// fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// fn get_transition(&mut self) -> Option<Box<Transition<Self>>> { None } // None means don't transition.
/// fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> { None } // None means don't transition.
/// }
/// ```
///
Expand Down Expand Up @@ -80,8 +80,8 @@ use crate::Transition;
pub trait Mode : 'static {
/// Represents the user-facing interface for the `Mode` that will be exposed via the `Automaton`. In order to be
/// used with an `Automaton`, the `Base` type of the `Mode` **must** match the `Base` type of the `Automaton`. This
/// is so that the `Automaton` can provide `get_mode()` and `get_mode_mut()` functions that return a reference to
/// the `Mode` as the `Base` type.
/// is so that the `Automaton` can provide `borrow_mode()` and `borrow_mode_mut()` functions that return a reference
/// to the `Mode` as the `Base` type.
///
type Base : ?Sized;

Expand All @@ -100,5 +100,5 @@ pub trait Mode : 'static {
///
/// See [`Transition`](trait.Transition.html) for more details.
///
fn get_transition(&mut self) -> Option<Box<Transition<Self>>>;
fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>>;
}
4 changes: 2 additions & 2 deletions src/mode_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait AnyModeWrapper {
/// this yields a `Transition`, the `Transition` will be called on the inner `Mode` and a new `ModeWrapper` around
/// the `Mode` to be swapped in will be returned.
///
fn perform_transitions(&mut self) -> Option<Box<AnyModeWrapper<Base = Self::Base>>>;
fn perform_transitions(&mut self) -> Option<Box<dyn AnyModeWrapper<Base = Self::Base>>>;
}

/// Wraps a specific instance of `Mode`, allowing the parent `Automaton` to handle `Transition`s between that instance
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<T> AnyModeWrapper for ModeWrapper<T>
self.mode.as_mut().unwrap().as_base_mut()
}

fn perform_transitions(&mut self) -> Option<Box<AnyModeWrapper<Base = Self::Base>>> {
fn perform_transitions(&mut self) -> Option<Box<dyn AnyModeWrapper<Base = Self::Base>>> {
// Retrieve the desired transition, if any, from the inner Mode.
match self.mode.as_mut().unwrap().get_transition() {
None => None,
Expand Down
16 changes: 8 additions & 8 deletions src/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{AnyModeWrapper, Mode, ModeWrapper};
/// # fn as_base(&self) -> &Self::Base { self }
/// # fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// // ...
/// fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
/// fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
/// # let some_condition = true;
/// # let some_other_condition = true;
/// // ...
Expand All @@ -65,7 +65,7 @@ use crate::{AnyModeWrapper, Mode, ModeWrapper};
/// # type Base = MyMode;
/// # fn as_base(&self) -> &Self::Base { self }
/// # fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// # fn get_transition(&mut self) -> Option<Box<Transition<Self>>> { None }
/// # fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> { None }
/// # }
/// #
/// # struct YetAnotherMode;
Expand All @@ -75,7 +75,7 @@ use crate::{AnyModeWrapper, Mode, ModeWrapper};
/// # type Base = MyMode;
/// # fn as_base(&self) -> &Self::Base { self }
/// # fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// # fn get_transition(&mut self) -> Option<Box<Transition<Self>>> { None }
/// # fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> { None }
/// # }
/// ```
///
Expand Down Expand Up @@ -103,7 +103,7 @@ use crate::{AnyModeWrapper, Mode, ModeWrapper};
/// # }
/// #
/// struct SomeMode {
/// queued_transition : Option<Box<Transition<Self>>>
/// queued_transition : Option<Box<dyn Transition<Self>>>
/// }
///
/// impl MyMode for SomeMode {
Expand All @@ -124,7 +124,7 @@ use crate::{AnyModeWrapper, Mode, ModeWrapper};
/// # fn as_base(&self) -> &Self::Base { self }
/// # fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// // ...
/// fn get_transition(&mut self) -> Option<Box<Transition<Self>>> {
/// fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> {
/// # let ready_to_transition = true;
/// // ...
/// if ready_to_transition && self.queued_transition.is_some() {
Expand All @@ -142,7 +142,7 @@ use crate::{AnyModeWrapper, Mode, ModeWrapper};
/// # type Base = MyMode;
/// # fn as_base(&self) -> &Self::Base { self }
/// # fn as_base_mut(&mut self) -> &mut Self::Base { self }
/// # fn get_transition(&mut self) -> Option<Box<Transition<Self>>> { None }
/// # fn get_transition(&mut self) -> Option<Box<dyn Transition<Self>>> { None }
/// # }
/// ```
///
Expand All @@ -157,7 +157,7 @@ pub trait Transition<A>
/// implementation details of the `Automaton`. Unfortunately, that isn't possible, in this case, because the `Mode`
/// trait cannot be made into an object, and therefore cannot be boxed.
///
fn invoke(self : Box<Self>, mode : A) -> Box<AnyModeWrapper<Base = A::Base>>;
fn invoke(self : Box<Self>, mode : A) -> Box<dyn AnyModeWrapper<Base = A::Base>>;
}

impl<T, A, B> Transition<A> for T
Expand All @@ -166,7 +166,7 @@ impl<T, A, B> Transition<A> for T
A : Mode,
B : Mode<Base = A::Base>,
{
fn invoke(self : Box<Self>, mode : A) -> Box<AnyModeWrapper<Base = A::Base>> {
fn invoke(self : Box<Self>, mode : A) -> Box<dyn AnyModeWrapper<Base = A::Base>> {
// Call the transition function and wrap the result with a ModeWrapper.
Box::new(ModeWrapper::<B>::new((self)(mode)))
}
Expand Down

0 comments on commit 3acd378

Please sign in to comment.