Turn Rust's ping/pong into libraries

We do this to add a pingpong_test.rs in the future that
uses the libraries.

Change-Id: Iab879ea06d40c2ef706f39d23e9b144aa266e0a5
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/events/BUILD b/aos/events/BUILD
index 036e378..4d5428b 100644
--- a/aos/events/BUILD
+++ b/aos/events/BUILD
@@ -4,7 +4,7 @@
 load("//aos:flatbuffers.bzl", "cc_static_flatbuffer")
 load("//aos:config.bzl", "aos_config")
 load("//tools/build_rules:autocxx.bzl", "autocxx_library")
-load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_test")
+load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_doc", "rust_doc_test", "rust_library", "rust_test")
 
 package(default_visibility = ["//visibility:public"])
 
@@ -273,16 +273,27 @@
         "//tools:has_msan": ["@platforms//:incompatible"],
     }),
     deps = [
-        ":event_loop_runtime",
-        ":ping_rust_fbs",
-        ":pong_rust_fbs",
+        ":ping_lib_rs",
         ":shm_event_loop_rs",
         "//aos:configuration_rs",
         "//aos:configuration_rust_fbs",
         "//aos:flatbuffers_rs",
         "//aos:init_rs",
-        "@com_github_google_flatbuffers//rust",
         "@crate_index//:clap",
+    ],
+)
+
+rust_library(
+    name = "ping_lib_rs",
+    srcs = [
+        "ping_lib.rs",
+    ],
+    crate_name = "ping_lib",
+    deps = [
+        ":event_loop_runtime",
+        ":ping_rust_fbs",
+        ":pong_rust_fbs",
+        "@com_github_google_flatbuffers//rust",
         "@crate_index//:futures",
     ],
 )
@@ -301,16 +312,27 @@
         "//tools:has_msan": ["@platforms//:incompatible"],
     }),
     deps = [
-        ":event_loop_runtime",
-        ":ping_rust_fbs",
-        ":pong_rust_fbs",
+        ":pong_lib_rs",
         ":shm_event_loop_rs",
         "//aos:configuration_rs",
         "//aos:configuration_rust_fbs",
         "//aos:flatbuffers_rs",
         "//aos:init_rs",
-        "@com_github_google_flatbuffers//rust",
         "@crate_index//:clap",
+    ],
+)
+
+rust_library(
+    name = "pong_lib_rs",
+    srcs = [
+        "pong_lib.rs",
+    ],
+    crate_name = "pong_lib",
+    deps = [
+        ":event_loop_runtime",
+        ":ping_rust_fbs",
+        ":pong_rust_fbs",
+        "@com_github_google_flatbuffers//rust",
         "@crate_index//:futures",
     ],
 )
diff --git a/aos/events/ping.rs b/aos/events/ping.rs
index d9673fa..09da114 100644
--- a/aos/events/ping.rs
+++ b/aos/events/ping.rs
@@ -1,17 +1,10 @@
 use aos_configuration as config;
-use aos_events_event_loop_runtime::{EventLoopRuntime, Sender, Watcher};
 use aos_events_shm_event_loop::ShmEventLoop;
 use aos_init::WithCppFlags;
-use clap::{CommandFactory, Parser};
-use core::cell::Cell;
-use core::time::Duration;
-use futures::never::Never;
-use std::borrow::Borrow;
+use clap::Parser;
+use ping_lib::PingTask;
 use std::path::Path;
 
-use ping_rust_fbs::aos::examples as ping;
-use pong_rust_fbs::aos::examples as pong;
-
 /// Ping portion of a ping/pong system.
 #[derive(Parser, Debug)]
 #[command(name = "ping")]
@@ -31,59 +24,3 @@
         runtime.spawn(ping.tasks(*runtime, app.sleep));
     });
 }
-
-#[derive(Debug)]
-struct PingTask {
-    counter: Cell<i32>,
-}
-
-impl PingTask {
-    pub fn new() -> Self {
-        Self {
-            counter: Cell::new(0),
-        }
-    }
-
-    /// Returns a future with all the tasks for the ping process
-    pub async fn tasks(&self, event_loop: EventLoopRuntime<'_>, sleep: u64) -> Never {
-        futures::join!(self.ping(&event_loop, sleep), self.handle_pong(&event_loop));
-        unreachable!("Let's hope `never_type` gets stabilized soon :)");
-    }
-
-    pub async fn ping(&self, event_loop: &EventLoopRuntime<'_>, sleep: u64) -> Never {
-        // The sender is used to send messages back to the pong channel.
-        let mut ping_sender: Sender<ping::Ping> = event_loop.make_sender("/test").unwrap();
-        let mut interval = event_loop.add_interval(Duration::from_micros(sleep));
-
-        let on_run = event_loop.on_run();
-        on_run.borrow().await;
-
-        loop {
-            interval.tick().await;
-            self.counter.set(self.counter.get() + 1);
-            let mut builder = ping_sender.make_builder();
-            let mut ping = ping::PingBuilder::new(builder.fbb());
-            let iter = self.counter.get();
-            ping.add_value(iter);
-            ping.add_send_time(event_loop.monotonic_now().into());
-            let ping = ping.finish();
-            builder.send(ping).expect("Can't send ping");
-        }
-    }
-
-    pub async fn handle_pong(&self, event_loop: &EventLoopRuntime<'_>) -> Never {
-        // The watcher gives us incoming ping messages.
-        let mut pong_watcher: Watcher<pong::Pong> = event_loop.make_watcher("/test").unwrap();
-
-        let on_run = event_loop.on_run();
-        on_run.borrow().await;
-        loop {
-            let pong = pong_watcher.next().await;
-            assert_eq!(
-                pong.message().unwrap().value(),
-                self.counter.get(),
-                "Missed a reply"
-            );
-        }
-    }
-}
diff --git a/aos/events/ping_lib.rs b/aos/events/ping_lib.rs
new file mode 100644
index 0000000..c4b3679
--- /dev/null
+++ b/aos/events/ping_lib.rs
@@ -0,0 +1,64 @@
+use aos_events_event_loop_runtime::{EventLoopRuntime, Sender, Watcher};
+use core::cell::Cell;
+use core::time::Duration;
+use futures::never::Never;
+use std::borrow::Borrow;
+
+use ping_rust_fbs::aos::examples as ping;
+use pong_rust_fbs::aos::examples as pong;
+
+#[derive(Debug)]
+pub struct PingTask {
+    counter: Cell<i32>,
+}
+
+impl PingTask {
+    pub fn new() -> Self {
+        Self {
+            counter: Cell::new(0),
+        }
+    }
+
+    /// Returns a future with all the tasks for the ping process
+    pub async fn tasks(&self, event_loop: EventLoopRuntime<'_>, sleep: u64) -> Never {
+        futures::join!(self.ping(&event_loop, sleep), self.handle_pong(&event_loop));
+        unreachable!("Let's hope `never_type` gets stabilized soon :)");
+    }
+
+    pub async fn ping(&self, event_loop: &EventLoopRuntime<'_>, sleep: u64) -> Never {
+        // The sender is used to send messages back to the pong channel.
+        let mut ping_sender: Sender<ping::Ping> = event_loop.make_sender("/test").unwrap();
+        let mut interval = event_loop.add_interval(Duration::from_micros(sleep));
+
+        let on_run = event_loop.on_run();
+        on_run.borrow().await;
+
+        loop {
+            interval.tick().await;
+            self.counter.set(self.counter.get() + 1);
+            let mut builder = ping_sender.make_builder();
+            let mut ping = ping::PingBuilder::new(builder.fbb());
+            let iter = self.counter.get();
+            ping.add_value(iter);
+            ping.add_send_time(event_loop.monotonic_now().into());
+            let ping = ping.finish();
+            builder.send(ping).expect("Can't send ping");
+        }
+    }
+
+    pub async fn handle_pong(&self, event_loop: &EventLoopRuntime<'_>) -> Never {
+        // The watcher gives us incoming ping messages.
+        let mut pong_watcher: Watcher<pong::Pong> = event_loop.make_watcher("/test").unwrap();
+
+        let on_run = event_loop.on_run();
+        on_run.borrow().await;
+        loop {
+            let pong = pong_watcher.next().await;
+            assert_eq!(
+                pong.message().unwrap().value(),
+                self.counter.get(),
+                "Missed a reply"
+            );
+        }
+    }
+}
diff --git a/aos/events/pong.rs b/aos/events/pong.rs
index 34da238..833cba2 100644
--- a/aos/events/pong.rs
+++ b/aos/events/pong.rs
@@ -1,13 +1,10 @@
 use aos_configuration as config;
-use aos_events_event_loop_runtime::{EventLoopRuntime, Sender, Watcher};
 use aos_events_shm_event_loop::ShmEventLoop;
 use aos_init::WithCppFlags;
 use clap::Parser;
-use futures::never::Never;
-use std::{borrow::Borrow, path::Path};
+use std::path::Path;
 
-use ping_rust_fbs::aos::examples as ping;
-use pong_rust_fbs::aos::examples as pong;
+use pong_lib::pong;
 
 /// Pong portion of a ping/pong system.
 #[derive(Parser, Debug)]
@@ -24,25 +21,3 @@
         runtime.spawn(task);
     });
 }
-
-/// Responds to ping messages with an equivalent pong.
-async fn pong(event_loop: EventLoopRuntime<'_>) -> Never {
-    // The watcher gives us incoming ping messages.
-    let mut ping_watcher: Watcher<ping::Ping> = event_loop.make_watcher("/test").unwrap();
-
-    // The sender is used to send messages back to the pong channel.
-    let mut pong_sender: Sender<pong::Pong> = event_loop.make_sender("/test").unwrap();
-
-    let on_run = event_loop.on_run();
-    on_run.borrow().await;
-    loop {
-        let ping = dbg!(ping_watcher.next().await);
-
-        let mut builder = pong_sender.make_builder();
-        let mut pong = pong::PongBuilder::new(builder.fbb());
-        pong.add_value(ping.message().unwrap().value());
-        pong.add_initial_send_time(event_loop.monotonic_now().into());
-        let pong = pong.finish();
-        builder.send(pong).expect("Can't send pong reponse");
-    }
-}
diff --git a/aos/events/pong_lib.rs b/aos/events/pong_lib.rs
new file mode 100644
index 0000000..fc1c162
--- /dev/null
+++ b/aos/events/pong_lib.rs
@@ -0,0 +1,28 @@
+use aos_events_event_loop_runtime::{EventLoopRuntime, Sender, Watcher};
+use futures::never::Never;
+use std::borrow::Borrow;
+
+use ping_rust_fbs::aos::examples as ping;
+use pong_rust_fbs::aos::examples as pong;
+
+/// Responds to ping messages with an equivalent pong.
+pub async fn pong(event_loop: EventLoopRuntime<'_>) -> Never {
+    // The watcher gives us incoming ping messages.
+    let mut ping_watcher: Watcher<ping::Ping> = event_loop.make_watcher("/test").unwrap();
+
+    // The sender is used to send messages back to the pong channel.
+    let mut pong_sender: Sender<pong::Pong> = event_loop.make_sender("/test").unwrap();
+
+    let on_run = event_loop.on_run();
+    on_run.borrow().await;
+    loop {
+        let ping = ping_watcher.next().await;
+
+        let mut builder = pong_sender.make_builder();
+        let mut pong = pong::PongBuilder::new(builder.fbb());
+        pong.add_value(ping.message().unwrap().value());
+        pong.add_initial_send_time(event_loop.monotonic_now().into());
+        let pong = pong.finish();
+        builder.send(pong).expect("Can't send pong reponse");
+    }
+}