use mopa crate to implement downcastng

This commit is contained in:
Adrian Malacoda 2017-03-12 18:22:36 -05:00
parent ff48a0501f
commit 552287bc94
6 changed files with 17 additions and 6 deletions

View File

@ -17,3 +17,4 @@ select = "0.3.0"
hyper = "0.9.18"
serde_json = "0.9"
retry = { git = "https://github.com/jimmycuadra/retry", rev = "3fa812e650d64ede61ea243fb83ef1a222ff0f84" }
mopa = "0.2.2"

View File

@ -3,9 +3,13 @@ extern crate select;
extern crate serde_json;
extern crate retry;
#[macro_use]
extern crate mopa;
pub mod searchers;
use std::any::Any;
//use std::any::Any;
use mopa::Any;
pub trait Link: Any {
fn label (&self) -> &str;
@ -13,6 +17,8 @@ pub trait Link: Any {
fn as_any(&self) -> &Any;
}
mopafy!(Link);
impl Link for Box<Link + 'static> {
fn label (&self) -> &str {
(**self).label()

View File

@ -21,11 +21,12 @@ fn main () {
searchers.add_searcher("gp", Box::new(MediawikiSearcher::new(String::from("https://gammapedia.monarch-pass.net/wiki/"))));
searchers.add_searcher("ip", Box::new(MediawikiSearcher::new(String::from("http://infinitypedia.org/wiki/"))));
searchers.add_searcher("wp", Box::new(MediawikiSearcher::new(String::from("https://en.wikipedia.org/wiki/"))));
match searchers.exact_search(&term) {
Some(item) => {
if let Some(card) = item.as_any().downcast_ref::<MtgCard>() {
if let Some(card) = item.downcast_ref::<MtgCard>() {
println!("{:?}", card);
} else if let Some(card) = item.as_any().downcast_ref::<YugiohCard>() {
} else if let Some(card) = item.downcast_ref::<YugiohCard>() {
println!("{:?}", card);
} else {
println!("{}: {}", item.label(), item.url());

View File

@ -15,7 +15,8 @@ use retry::delay::Fixed;
use std;
use std::io::Read;
use std::any::Any;
//use std::any::Any;
use mopa::Any;
const NUM_RETRIES: usize = 10;
const RETRY_WAIT_MILLIS: u64 = 500;

View File

@ -14,7 +14,8 @@ use retry::delay::Fixed;
use std;
use std::io::Read;
use std::any::Any;
//use std::any::Any;
use mopa::Any;
const NUM_RETRIES: usize = 10;
const RETRY_WAIT_MILLIS: u64 = 500;

View File

@ -14,7 +14,8 @@ use retry::delay::Fixed;
use std;
use std::io::Read;
use std::any::Any;
//use std::any::Any;
use mopa::Any;
const NUM_RETRIES: usize = 10;
const RETRY_WAIT_MILLIS: u64 = 500;