Просмотр исходного кода

system: Fully port to smol runtime.

parazyd 3 лет назад
Родитель
Сommit
f782c236a4
3 измененных файлов с 22 добавлено и 15 удалено
  1. 5 2
      src/system/mod.rs
  2. 13 9
      src/system/stoppable_task.rs
  3. 4 4
      src/system/subscriber.rs

+ 5 - 2
src/system/mod.rs

@@ -16,8 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+/// Implementation of async background task spawning which are stoppable
+/// using channel signalling.
 pub mod stoppable_task;
-pub mod subscriber;
-
 pub use stoppable_task::{StoppableTask, StoppableTaskPtr};
+
+/// Simple broadcast (publish-subscribe) class
+pub mod subscriber;
 pub use subscriber::{Subscriber, SubscriberPtr, Subscription};

+ 13 - 9
src/system/stoppable_task.rs

@@ -16,22 +16,25 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use async_std::sync::Arc;
+use std::sync::Arc;
 
-use futures::{Future, FutureExt};
-use smol::Executor;
+use smol::{
+    channel,
+    future::{self, Future},
+    Executor,
+};
 
 pub type StoppableTaskPtr = Arc<StoppableTask>;
 
 #[derive(Debug)]
 pub struct StoppableTask {
-    stop_send: smol::channel::Sender<()>,
-    stop_recv: smol::channel::Receiver<()>,
+    stop_send: channel::Sender<()>,
+    stop_recv: channel::Receiver<()>,
 }
 
 impl StoppableTask {
     pub fn new() -> Arc<Self> {
-        let (stop_send, stop_recv) = smol::channel::unbounded();
+        let (stop_send, stop_recv) = channel::unbounded();
         Arc::new(Self { stop_send, stop_recv })
     }
 
@@ -54,11 +57,12 @@ impl StoppableTask {
     {
         executor
             .spawn(async move {
-                let result = futures::select! {
-                    _ = self.stop_recv.recv().fuse() => Err(stop_value),
-                    result = main.fuse() => result
+                let stop_fut = async {
+                    let _ = self.stop_recv.recv().await;
+                    Err(stop_value)
                 };
 
+                let result = future::or(main, stop_fut).await;
                 stop_handler(result).await;
             })
             .detach();

+ 4 - 4
src/system/subscriber.rs

@@ -16,11 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
-use std::collections::HashMap;
+use std::{collections::HashMap, sync::Arc};
 
-use async_std::sync::{Arc, Mutex};
 use log::warn;
 use rand::{rngs::OsRng, Rng};
+use smol::lock::Mutex;
 
 pub type SubscriberPtr<T> = Arc<Subscriber<T>>;
 pub type SubscriptionId = usize;
@@ -43,12 +43,12 @@ impl<T: Clone> Subscription<T> {
         match message_result {
             Ok(message_result) => message_result,
             Err(err) => {
-                panic!("MessageSubscription::receive() recv_queue failed! {}", err);
+                panic!("Subscription::receive() recv_queue failed! {}", err);
             }
         }
     }
 
-    // Must be called manually since async Drop is not possible in Rust
+    /// Must be called manually since async Drop is not possible in Rust
     pub async fn unsubscribe(&self) {
         self.parent.clone().unsubscribe(self.id).await
     }