diff --git a/bin/u_agent/src/main.rs b/bin/u_agent/src/main.rs index 57f19fb..2f4179c 100644 --- a/bin/u_agent/src/main.rs +++ b/bin/u_agent/src/main.rs @@ -26,7 +26,7 @@ async fn main() { retry_until_ok!(instance.init(&cli_info).await); loop { let jobs = retry_until_ok!(instance.get_jobs().await); - if jobs.len() > 0 { + if jobs.0.len() > 0 { println!("{:?}", jobs); } sleep(Duration::from_secs(2)); @@ -35,7 +35,7 @@ async fn main() { #[macro_export] macro_rules! retry_until_ok { - ( $body:stmt ) => { + ( $body:expr ) => { loop { match $body { Ok(r) => break r, diff --git a/bin/u_server/src/handlers.rs b/bin/u_server/src/handlers.rs index 41b1623..505b7bf 100644 --- a/bin/u_server/src/handlers.rs +++ b/bin/u_server/src/handlers.rs @@ -12,7 +12,7 @@ pub async fn add_client( let mut clients = db.lock().await; if clients.contains_key(&new_cli.id) { Ok(warp::reply::json( - &RawMsg("Already exist".to_string()).into_message() + &RawMsg("Already exist".to_string()).as_message() )) } else { clients.insert( @@ -20,7 +20,7 @@ pub async fn add_client( UClient::new(new_cli.into_owned()) ); Ok(warp::reply::json( - &RawMsg("Added".to_string()).into_message() + &RawMsg("Added".to_string()).as_message() )) } } @@ -42,7 +42,7 @@ pub async fn get_jobs( pub async fn set_jobs( uid: Option, - msg: Message<'_, JobStorageWrapper>, + msg: Message<'_, CollectionWrapper>, db: Storage) -> Result { let mut clients = db.lock().await; @@ -64,6 +64,6 @@ pub async fn listing(db: Storage) -> Result { result.push(cli.client_info.clone()); } Ok(warp::reply::json( - &Message::new_owned(result) + &CollectionWrapper(result).as_message() )) } \ No newline at end of file diff --git a/bin/u_server/src/main.rs b/bin/u_server/src/main.rs index 4184022..1bd3dcb 100644 --- a/bin/u_server/src/main.rs +++ b/bin/u_server/src/main.rs @@ -46,14 +46,14 @@ async fn main() { let set_jobs = warp::post() .and(warp::path(Paths::set_jobs)) .and(warp::path::param::().map(Some)) - .and(get_content::()) + .and(get_content::>()) .and(db.clone()) .and_then(handlers::set_jobs); let update_own_jobs = warp::post() .and(warp::path(Paths::set_jobs)) .and(warp::path::param::().map(Some)) - .and(get_content::()) + .and(get_content::>()) .and(db.clone()) .and_then(handlers::set_jobs); diff --git a/lib/u_lib/src/client/network.rs b/lib/u_lib/src/client/network.rs index 951f23a..f24e5d2 100644 --- a/lib/u_lib/src/client/network.rs +++ b/lib/u_lib/src/client/network.rs @@ -104,7 +104,7 @@ impl ClientHandler { } build_handler!(POST init(ClientInfo) -> RawMsg); -build_handler!(GET ls() -> Vec); +build_handler!(GET ls() -> CollectionWrapper>); build_handler!(POST del() -> ()); -build_handler!(GET get_jobs() -> JobStorageWrapper); -build_handler!(POST set_jobs(JobStorageWrapper) -> ()); +build_handler!(GET get_jobs() -> CollectionWrapper); +build_handler!(POST set_jobs(CollectionWrapper) -> ()); diff --git a/lib/u_lib/src/contracts/datatypes.rs b/lib/u_lib/src/contracts/datatypes.rs index 0f97109..065bf84 100644 --- a/lib/u_lib/src/contracts/datatypes.rs +++ b/lib/u_lib/src/contracts/datatypes.rs @@ -13,9 +13,31 @@ use { pub type CliStorage = HashMap; pub type JobStorage = HashMap; -// because can't impl From> for Cow +// because can't impl From> for Cow #[derive(Serialize, Deserialize, Debug, Clone)] -pub struct JobStorageWrapper(pub JobStorage); +pub struct CollectionWrapper(pub T); + +impl From for CollectionWrapper { + fn from(t: T) -> Self { + CollectionWrapper(t) + } +} + +impl ToMsg for CollectionWrapper {} + +impl<'cow, T: Clone> From> for Cow<'cow, CollectionWrapper> { + #[inline] + fn from(obj: CollectionWrapper) -> Cow<'cow, CollectionWrapper> { + Cow::Owned(obj) + } +} + +impl<'cow, T: Clone> From<&'cow CollectionWrapper> for Cow<'cow, CollectionWrapper> { + #[inline] + fn from(obj: &'cow CollectionWrapper) -> Cow<'cow, CollectionWrapper> { + Cow::Borrowed(obj) + } +} #[derive(Clone)] diff --git a/lib/u_lib/src/contracts/messaging.rs b/lib/u_lib/src/contracts/messaging.rs index 80cb367..0fa2fc7 100644 --- a/lib/u_lib/src/contracts/messaging.rs +++ b/lib/u_lib/src/contracts/messaging.rs @@ -14,10 +14,6 @@ where Self: Clone { where Cow<'m, Self>: From<&'m Self> { Message::new(self) } - - fn into_message(self) -> Message<'static, Self> { - Message::new_owned(self) - } } #[derive(Serialize, Deserialize, Debug)] @@ -37,14 +33,14 @@ where I: Clone { } } - // crutch + /* crutch // because Vec is stored as &[] (wtf?) pub fn new_owned(item: I) -> Self { Self { id: *UID, item: Cow::Owned(item) } - } + }*/ pub fn into_item(self) -> Cow<'cow, I> { self.item diff --git a/lib/u_lib/src/contracts/mod.rs b/lib/u_lib/src/contracts/mod.rs index fdbc66c..369798a 100644 --- a/lib/u_lib/src/contracts/mod.rs +++ b/lib/u_lib/src/contracts/mod.rs @@ -34,4 +34,4 @@ macro_rules! to_message { } } -to_message!(ClientInfo, RawMsg, JobStorageWrapper); \ No newline at end of file +to_message!(ClientInfo, RawMsg); \ No newline at end of file