Skip to content

Commit

Permalink
Use MaybeSend in inbox
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed May 5, 2024
1 parent 9819240 commit 5de76e9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion crates/egui_inbox/src/broadcast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use hello_egui_utils::MaybeSend;
use parking_lot::Mutex;

use crate::{UiInbox, UiInboxSender};
Expand Down Expand Up @@ -43,7 +44,7 @@ impl<T> Broadcast<T> {
/// Send a message of type [T] to all subscribers.
pub fn send(&self, message: T)
where
T: Clone + Send + 'static,
T: Clone + MaybeSend + 'static,
{
let mut senders = self.senders.lock();
senders.retain(|tx| tx.send(message.clone()).is_ok());
Expand Down
5 changes: 3 additions & 2 deletions crates/egui_inbox/src/type_broadcast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use hello_egui_utils::MaybeSend;
use parking_lot::Mutex;
use type_map::concurrent::TypeMap;

Expand All @@ -22,7 +23,7 @@ impl TypeBroadcast {
}

/// Subscribe to a broadcast, receiving a [BroadcastReceiver] of type [T].
pub fn subscribe<T: Send + 'static>(&self) -> BroadcastReceiver<T> {
pub fn subscribe<T: MaybeSend + 'static>(&self) -> BroadcastReceiver<T> {
self.broadcasts
.lock()
.entry()
Expand All @@ -32,7 +33,7 @@ impl TypeBroadcast {

/// Send a message of type [T] to all subscribers.
/// If there are any subscribers with a [crate::RequestRepaintContext] attached, a repaint will be requested.
pub fn send<T: Send + Clone + 'static>(&self, message: T) {
pub fn send<T: MaybeSend + Clone + 'static>(&self, message: T) {
let mut broadcasts = self.broadcasts.lock();
let entry = broadcasts.entry().or_insert_with(|| Broadcast::new());
entry.send(message);
Expand Down
5 changes: 3 additions & 2 deletions crates/egui_inbox/src/type_inbox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::sync::Arc;

use hello_egui_utils::MaybeSend;
use parking_lot::Mutex;
use type_map::concurrent::TypeMap;

Expand Down Expand Up @@ -43,15 +44,15 @@ impl TypeInbox {

/// Send a message of type [T].
/// A repaint will be requested.
pub fn send<T: Send + 'static>(&self, message: T) {
pub fn send<T: MaybeSend + 'static>(&self, message: T) {
let mut guard = self.0.lock();
let entry = guard.map.entry().or_insert_with(TypeInboxEntry::<T>::new);
entry.sender.send(message).ok();
guard.ctx.request_repaint();
}

/// Read the inbox, returning an iterator over all pending messages.
pub fn read<T: Send + 'static>(&self) -> impl Iterator<Item = T> {
pub fn read<T: MaybeSend + 'static>(&self) -> impl Iterator<Item = T> {
let mut guard = self.0.lock();

let iter = guard
Expand Down

0 comments on commit 5de76e9

Please sign in to comment.