Skip to content

Commit

Permalink
listen: Split Listen::listen() into provided and required methods.
Browse files Browse the repository at this point in the history
This allows `dyn Listen` to exist and simplifies the code required in a
basic boilerplate `impl Listen`. The main motivation for it is that
I think there might be a better version of `ListenableSource` to be had
which is based on an `Arc<dyn SomethingThatHasListenAsASupertrait>` and
thus doesn't require the data source to be exactly a `ListenableCell`.
  • Loading branch information
kpreid committed Dec 10, 2024
1 parent 2d0433c commit fddc6e4
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 23 deletions.
4 changes: 2 additions & 2 deletions all-is-cubes-desktop/src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,9 @@ impl Recorder {
impl listen::Listen for Recorder {
type Msg = Status;

fn listen<L: listen::Listener<Self::Msg> + Send + Sync + 'static>(&self, listener: L) {
fn listen_raw(&self, listener: listen::DynListener<Self::Msg>) {
if let Some(notifier) = self.status_notifier.upgrade() {
notifier.listen(listener)
notifier.listen_raw(listener)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion all-is-cubes-gpu/src/in_wgpu/shaders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl Shaders {

impl listen::Listen for Shaders {
type Msg = ();
fn listen<L: listen::Listener<Self::Msg> + 'static>(&self, listener: L) {
fn listen_raw(&self, listener: listen::DynListener<()>) {
self.modules_changed.listen(listener)
}
}
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-ui/src/inv_watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ impl InventoryWatcher {
impl listen::Listen for InventoryWatcher {
type Msg = WatcherChange;

fn listen<L: listen::Listener<Self::Msg> + 'static>(&self, listener: L) {
self.notifier.listen(listener)
fn listen_raw(&self, listener: listen::DynListener<Self::Msg>) {
self.notifier.listen_raw(listener)
}
}

Expand Down
8 changes: 4 additions & 4 deletions all-is-cubes/src/block/block_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,13 @@ impl fmt::Debug for BlockDef {
}
}

/// Registers a listener for whenever the result of evaluation of this block definition changes.
/// Note that this only occurs when the owning [`Universe`] is being stepped.
impl Listen for BlockDef {
type Msg = BlockChange;

/// Registers a listener for whenever the result of evaluation of this block definition changes.
/// Note that this only occurs when the owning [`Universe`] is being stepped.
fn listen<L: Listener<BlockChange> + 'static>(&self, listener: L) {
self.notifier.listen(listener)
fn listen_raw(&self, listener: listen::DynListener<Self::Msg>) {
self.notifier.listen_raw(listener)
}
}

Expand Down
6 changes: 3 additions & 3 deletions all-is-cubes/src/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use num_traits::float::Float as _;
use crate::behavior::{self, Behavior, BehaviorSet, BehaviorSetTransaction};
use crate::camera::ViewTransform;
use crate::inv::{self, Inventory, InventoryTransaction, Slot, Tool};
use crate::listen::{Listen, Listener, Notifier};
use crate::listen::{self, Listen, Notifier};
use crate::math::{Aab, Cube, Face6, Face7, FreeCoordinate, FreePoint, FreeVector};
use crate::physics;
use crate::physics::{Body, BodyStepInfo, BodyTransaction, Contact, Velocity};
Expand Down Expand Up @@ -521,10 +521,10 @@ impl VisitHandles for Character {
}
}

/// Registers a listener for mutations of this character.
impl Listen for Character {
type Msg = CharacterChange;
/// Registers a listener for mutations of this character.
fn listen<L: Listener<CharacterChange> + 'static>(&self, listener: L) {
fn listen_raw(&self, listener: listen::DynListener<Self::Msg>) {
self.notifier.listen(listener)
}
}
Expand Down
21 changes: 20 additions & 1 deletion all-is-cubes/src/listen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ pub trait Listen {
/// Note that listeners are removed only via their returning [`false`] from
/// [`Listener::receive()`]; there is no operation to remove a listener,
/// nor are subscriptions deduplicated.
fn listen<L: Listener<Self::Msg> + 'static>(&self, listener: L);
fn listen<L: Listener<Self::Msg> + 'static>(&self, listener: L)
where
Self: Sized,
{
self.listen_raw(listener.erased())
}

/// Subscribe the given [`Listener`] to this source of messages,
/// without automatic type conversion.
///
/// Note that listeners are removed only via their returning [`false`] from
/// [`Listener::receive()`]; there is no operation to remove a listener,
/// nor are subscriptions deduplicated.
fn listen_raw(&self, listener: DynListener<Self::Msg>);
}

impl<T: Listen> Listen for &T {
Expand All @@ -54,13 +67,19 @@ impl<T: Listen> Listen for &T {
fn listen<L: Listener<Self::Msg> + 'static>(&self, listener: L) {
(**self).listen(listener)
}
fn listen_raw(&self, listener: DynListener<Self::Msg>) {
(**self).listen_raw(listener)
}
}
impl<T: Listen> Listen for Arc<T> {
type Msg = T::Msg;

fn listen<L: Listener<Self::Msg> + 'static>(&self, listener: L) {
(**self).listen(listener)
}
fn listen_raw(&self, listener: DynListener<Self::Msg>) {
(**self).listen_raw(listener)
}
}

// -------------------------------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions all-is-cubes/src/listen/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::fmt;

use alloc::sync::Arc;

use crate::listen::{Listen, Listener, Notifier};
use crate::listen::{self, Listen, Notifier};
use crate::util::maybe_sync::{Mutex, MutexGuard};

/// A interior-mutable container for a value which can notify that the value changed,
Expand Down Expand Up @@ -157,9 +157,9 @@ impl<T> Clone for ListenableSource<T> {
impl<T> Listen for ListenableSource<T> {
type Msg = ();

fn listen<L: Listener<Self::Msg> + 'static>(&self, listener: L) {
fn listen_raw(&self, listener: listen::DynListener<Self::Msg>) {
if let Some(notifier) = &self.storage.notifier {
notifier.listen(listener);
notifier.listen_raw(listener);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes/src/listen/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ impl<M> Notifier<M> {
}
}

impl<M> Listen for Notifier<M> {
impl<M: 'static> Listen for Notifier<M> {
type Msg = M;

fn listen<L: Listener<M> + 'static>(&self, listener: L) {
fn listen_raw(&self, listener: DynListener<Self::Msg>) {
if !listener.receive(&[]) {
// skip adding it if it's already dead
return;
Expand Down
10 changes: 5 additions & 5 deletions all-is-cubes/src/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::character::Spawn;
use crate::drawing::DrawingPlane;
use crate::fluff::{self, Fluff};
use crate::inv::{EphemeralOpaque, InventoryTransaction};
use crate::listen::{Listen, Listener, Notifier};
use crate::listen::{self, Listen, Notifier};
use crate::math::{
notnan, rgb_const, Cube, FreeCoordinate, GridAab, GridCoordinate, GridRotation, Gridgid,
NotNan, Vol,
Expand Down Expand Up @@ -1000,11 +1000,11 @@ impl VisitHandles for Space {
}
}

/// Registers a listener for mutations of this space.
impl Listen for Space {
type Msg = SpaceChange;
/// Registers a listener for mutations of this space.
fn listen<L: Listener<SpaceChange> + 'static>(&self, listener: L) {
self.change_notifier.listen(listener)
fn listen_raw(&self, listener: listen::DynListener<Self::Msg>) {
self.change_notifier.listen_raw(listener)
}
}

Expand Down Expand Up @@ -1424,7 +1424,7 @@ impl<'s> Extract<'s> {
}

// TODO: Tune this buffer size parameter, and validate it isn't overly large on the stack.
type ChangeBuffer<'notifier> = crate::listen::Buffer<'notifier, SpaceChange, 16>;
type ChangeBuffer<'notifier> = listen::Buffer<'notifier, SpaceChange, 16>;

/// Argument passed to [`Space`] mutation methods that are used in bulk mutations.
struct MutationCtx<'a, 'n> {
Expand Down

0 comments on commit fddc6e4

Please sign in to comment.