1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
extern crate tokio; use hyper::rt::{Future, Stream}; use super::error::CBError; pub trait Adapter<T> { type Result; fn process<F>(f: F) -> Self::Result where F: Future<Item = T, Error = CBError> + 'static; } pub struct Sync; impl<T> Adapter<T> for Sync { type Result = Result<T, CBError>; fn process<F>(f: F) -> Self::Result where F: Future<Item = T, Error = CBError> + 'static { let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap(); rt.block_on(f) } } pub struct ASync; impl<T> Adapter<T> for ASync { type Result = Box<Future<Item = T, Error = CBError>>; fn process<F>(f: F) -> Self::Result where F: Future<Item = T, Error = CBError> + 'static { Box::new(f) } } #[cfg(test)] mod tests { use super::super::public::*; use super::*; #[test] fn test_sync() { let client: Public<Sync> = Public::new(); let time = client.get_time().unwrap(); let time_str = format!("{:?}", time); assert!(time_str.starts_with("Time {")); assert!(time_str.contains("iso:")); assert!(time_str.contains("epoch:")); assert!(time_str.ends_with("}")); } #[test] fn test_async() { let client: Public<ASync> = Public::new(); let time = client.get_time() .and_then(|time| { let time_str = format!("{:?}", time); assert!(time_str.starts_with("Time {")); assert!(time_str.contains("iso:")); assert!(time_str.contains("epoch:")); assert!(time_str.ends_with("}")); Ok(()) }); let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap(); rt.block_on(time); } }