Skip to content

Commit

Permalink
Merge pull request #71 from iqlusioninc/components/partial-ord
Browse files Browse the repository at this point in the history
component: impl PartialOrd for Box<dyn Component>
  • Loading branch information
tony-iqlusion authored Jun 29, 2019
2 parents 56ab3cc + 6ade741 commit d05c078
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
30 changes: 29 additions & 1 deletion src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod registry;

pub use self::{name::Name, registry::Registry};
use crate::{application::Application, error::FrameworkError, shutdown::Shutdown, Version};
use std::{fmt::Debug, slice::Iter};
use std::{cmp::Ordering, fmt::Debug, slice::Iter};

/// Application components.
///
Expand Down Expand Up @@ -47,3 +47,31 @@ where
Ok(())
}
}

impl<A> PartialEq for Box<dyn Component<A>>
where
A: Application,
{
fn eq(&self, other: &Self) -> bool {
self.name() == other.name()
}
}

impl<A> PartialOrd for Box<dyn Component<A>>
where
A: Application,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if other.dependencies().any(|dep| *dep == self.name()) {
if self.dependencies().any(|dep| *dep == other.name()) {
None
} else {
Some(Ordering::Greater)
}
} else if self.dependencies().any(|dep| *dep == other.name()) {
Some(Ordering::Less)
} else {
Some(Ordering::Equal)
}
}
}
16 changes: 3 additions & 13 deletions src/component/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
error::{FrameworkError, FrameworkErrorKind::ComponentError},
shutdown::Shutdown,
};
use std::{borrow::Borrow, cmp::Ordering, collections::HashSet, slice};
use std::{borrow::Borrow, collections::HashSet, slice};

/// The component registry provides a system for runtime registration of
/// application components which can interact with each other dynamically.
Expand Down Expand Up @@ -86,20 +86,10 @@ where
/// on others after their dependencies.
///
/// Exits the application if the ordering cannot be resolved.
// TODO(tarcieri): impl `PartialOrd` on `Component`
fn sort(&mut self) {
self.components.sort_by(|a, b| {
if b.dependencies().any(|dep| *dep == a.name()) {
if a.dependencies().any(|dep| *dep == b.name()) {
application::exit::bad_component_order(a.borrow(), b.borrow());
} else {
Ordering::Greater
}
} else if a.dependencies().any(|dep| *dep == b.name()) {
Ordering::Less
} else {
Ordering::Equal
}
a.partial_cmp(b)
.unwrap_or_else(|| application::exit::bad_component_order(a.borrow(), b.borrow()))
})
}
}

0 comments on commit d05c078

Please sign in to comment.