From 59d86cc954efeaa3d4b6637f857e555ead8b8101 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Sat, 14 Sep 2024 01:53:57 +0900 Subject: [PATCH] Rename `MessageChannel` to `SignalChannel` --- flutter_package/bin/src/message.dart | 16 ++++++------- rust_crate/src/channel.rs | 34 ++++++++++++++-------------- rust_crate/src/lib.rs | 2 +- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/flutter_package/bin/src/message.dart b/flutter_package/bin/src/message.dart index 056ecfbe..edbbb6eb 100644 --- a/flutter_package/bin/src/message.dart +++ b/flutter_package/bin/src/message.dart @@ -290,9 +290,9 @@ import 'package:rinf/rinf.dart'; use prost::Message; use rinf::{ - debug_print, message_channel, send_rust_signal, - DartSignal, MessageReceiver, MessageSender, - RinfError, + debug_print, send_rust_signal, signal_channel, + DartSignal, RinfError, SignalReceiver, + SignalSender, }; use std::sync::LazyLock; @@ -312,14 +312,14 @@ use std::sync::LazyLock; rustPath, ''' type ${messageName}Cell = LazyLock<( - MessageSender>, - MessageReceiver>, + SignalSender>, + SignalReceiver>, )>; pub static ${snakeName.toUpperCase()}_CHANNEL: ${messageName}Cell = - LazyLock::new(message_channel); + LazyLock::new(signal_channel); impl ${normalizePascal(messageName)} { - pub fn get_dart_signal_receiver() -> MessageReceiver> { + pub fn get_dart_signal_receiver() -> SignalReceiver> { ${snakeName.toUpperCase()}_CHANNEL.1.clone() } } @@ -424,7 +424,7 @@ impl ${normalizePascal(messageName)} { #![allow(unused_mut)] use prost::Message; -use rinf::{debug_print, message_channel, DartSignal, RinfError}; +use rinf::{debug_print, signal_channel, DartSignal, RinfError}; use std::collections::HashMap; use std::error::Error; use std::sync::OnceLock; diff --git a/rust_crate/src/channel.rs b/rust_crate/src/channel.rs index 9dd52a74..3c049b90 100644 --- a/rust_crate/src/channel.rs +++ b/rust_crate/src/channel.rs @@ -4,34 +4,34 @@ use std::pin::Pin; use std::sync::{Arc, Mutex}; use std::task::{Context, Poll, Waker}; -/// The `MessageSender` is used to send messages into a shared message queue. +/// The `SignalSender` is used to send messages into a shared message queue. /// It is clonable, and multiple senders can be created to send messages into /// the same queue. Each message is sent to a receiver, but only the currently /// active receiver can receive messages. #[derive(Clone)] -pub struct MessageSender { - inner: Arc>>, +pub struct SignalSender { + inner: Arc>>, } -/// The `MessageReceiver` is used to asynchronously receive messages from the +/// The `SignalReceiver` is used to asynchronously receive messages from the /// shared message queue. Only one receiver can be active at a time; new /// receivers are created by cloning the original. When a receiver is cloned, /// it becomes the active receiver, and the previous receiver will no longer /// receive messages. -pub struct MessageReceiver { - inner: Arc>>, +pub struct SignalReceiver { + inner: Arc>>, id: usize, // Each receiver has a unique ID } /// A channel holding a message queue and managing the current active receiver. /// Only the active receiver can receive messages. -struct MessageChannel { +struct SignalChannel { queue: VecDeque, waker: Option, active_receiver_id: usize, // Track the active receiver by ID } -impl MessageSender { +impl SignalSender { /// Sends a message to the shared queue. If a receiver is waiting for a /// message, it will be woken up. This method does not fail if the mutex /// is poisoned but simply ignores the failure. @@ -50,7 +50,7 @@ impl MessageSender { } } -impl MessageReceiver { +impl SignalReceiver { /// Asynchronously receives the next message from the queue. Only the active /// receiver is allowed to receive messages. If there are no messages in the /// queue, the receiver will wait until a new message is sent. If this receiver @@ -65,13 +65,13 @@ impl MessageReceiver { } // Automatically make the cloned receiver the active one -impl Clone for MessageReceiver { +impl Clone for SignalReceiver { /// Clones the receiver and makes the new receiver the active one. The /// original receiver will no longer receive messages after this clone. /// This ensures only the most recent receiver can access the message queue. fn clone(&self) -> Self { let mut inner = self.inner.lock().unwrap(); - let new_receiver = MessageReceiver { + let new_receiver = SignalReceiver { inner: self.inner.clone(), id: inner.active_receiver_id + 1, // Increment ID for new receiver }; @@ -83,11 +83,11 @@ impl Clone for MessageReceiver { } } -/// A future that represents the attempt of a `MessageReceiver` to receive a +/// A future that represents the attempt of a `SignalReceiver` to receive a /// message. This future is only completed when the active receiver receives /// a message from the queue. struct RecvFuture { - inner: Arc>>, + inner: Arc>>, receiver_id: usize, // Track which receiver is polling } @@ -127,17 +127,17 @@ impl Future for RecvFuture { /// used to send messages, and the receiver can be used to receive them /// asynchronously. Only one receiver is active at a time, and new receivers /// are created by cloning the original receiver. -pub fn message_channel() -> (MessageSender, MessageReceiver) { - let channel = Arc::new(Mutex::new(MessageChannel { +pub fn signal_channel() -> (SignalSender, SignalReceiver) { + let channel = Arc::new(Mutex::new(SignalChannel { queue: VecDeque::new(), waker: None, active_receiver_id: 0, // Start with receiver ID 0 })); - let sender = MessageSender { + let sender = SignalSender { inner: channel.clone(), }; - let receiver = MessageReceiver { + let receiver = SignalReceiver { inner: channel, id: 0, }; diff --git a/rust_crate/src/lib.rs b/rust_crate/src/lib.rs index 345fb2d7..059c6325 100644 --- a/rust_crate/src/lib.rs +++ b/rust_crate/src/lib.rs @@ -9,7 +9,7 @@ mod interface_os; #[cfg(target_family = "wasm")] mod interface_web; -pub use channel::{message_channel, MessageReceiver, MessageSender}; +pub use channel::{signal_channel, SignalReceiver, SignalSender}; pub use error::RinfError; pub use interface::{send_rust_signal, start_rust_logic, DartSignal}; pub use shutdown::get_shutdown_receiver;