Skip to content

Commit

Permalink
Replace Nop with ()
Browse files Browse the repository at this point in the history
  • Loading branch information
evenorog committed Apr 17, 2023
1 parent 6fa09fd commit 1c14144
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use checkpoint::Checkpoint;
pub use display::Display;
pub use queue::Queue;

use crate::socket::{Nop, Signal, Slot};
use crate::socket::{Signal, Slot};
use crate::{At, Edit, Entry, Record};
use alloc::collections::{BTreeMap, VecDeque};
use alloc::string::{String, ToString};
Expand Down Expand Up @@ -52,7 +52,7 @@ use serde::{Deserialize, Serialize};
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct History<E, S = Nop> {
pub struct History<E, S = ()> {
root: usize,
next: usize,
saved: Option<At>,
Expand Down
4 changes: 2 additions & 2 deletions src/history/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::record::Builder as RecordBuilder;
use crate::{History, Nop};
use crate::History;

/// Builder for a [`History`].
///
Expand All @@ -18,7 +18,7 @@ use crate::{History, Nop};
/// # }
/// ```
#[derive(Debug)]
pub struct Builder<E, S = Nop>(RecordBuilder<E, S>);
pub struct Builder<E, S = ()>(RecordBuilder<E, S>);

impl<E, S> Builder<E, S> {
/// Sets the capacity for the history.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub use join::Join;
#[cfg(feature = "alloc")]
pub use record::Record;
#[cfg(feature = "alloc")]
pub use socket::{Nop, Signal, Slot};
pub use socket::{Signal, Slot};

/// Base functionality for all edit commands.
pub trait Edit {
Expand Down
4 changes: 2 additions & 2 deletions src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use checkpoint::Checkpoint;
pub use display::Display;
pub use queue::Queue;

use crate::socket::{Nop, Slot, Socket};
use crate::socket::{Slot, Socket};
use crate::{Edit, Entry, History, Merged, Signal};
use alloc::collections::VecDeque;
use alloc::string::{String, ToString};
Expand Down Expand Up @@ -59,7 +59,7 @@ use serde::{Deserialize, Serialize};
/// ```
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug)]
pub struct Record<E, S = Nop> {
pub struct Record<E, S = ()> {
pub(crate) entries: VecDeque<Entry<E>>,
pub(crate) limit: NonZeroUsize,
pub(crate) index: usize,
Expand Down
4 changes: 2 additions & 2 deletions src/record/builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Socket;
use crate::{Nop, Record};
use crate::Record;
use alloc::collections::VecDeque;
use core::marker::PhantomData;
use core::num::NonZeroUsize;
Expand All @@ -21,7 +21,7 @@ use core::num::NonZeroUsize;
/// # }
/// ```
#[derive(Debug)]
pub struct Builder<E, S = Nop> {
pub struct Builder<E, S = ()> {
capacity: usize,
limit: NonZeroUsize,
saved: bool,
Expand Down
41 changes: 18 additions & 23 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ impl<S: Slot> Socket<S> {
}
}

/// The `Signal` describes the state change done to the data structures.
///
/// See [`Slot`] for more information.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Signal {
/// Says if the structures can undo.
Undo(bool),
/// Says if the structures can redo.
Redo(bool),
/// Says if the target is in a saved state.
Saved(bool),
}

/// Use this to handle signals emitted.
///
/// This allows you to trigger events on certain state changes.
Expand Down Expand Up @@ -80,6 +94,10 @@ pub trait Slot {
fn on_emit(&mut self, signal: Signal);
}

impl Slot for () {
fn on_emit(&mut self, _: Signal) {}
}

impl<F: FnMut(Signal)> Slot for F {
fn on_emit(&mut self, signal: Signal) {
self(signal)
Expand All @@ -99,26 +117,3 @@ impl Slot for SyncSender<Signal> {
self.send(signal).ok();
}
}

/// The `Signal` describes the state change done to the data structures.
///
/// See [`Slot`] for more information.
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum Signal {
/// Says if the structures can undo.
Undo(bool),
/// Says if the structures can redo.
Redo(bool),
/// Says if the target is in a saved state.
Saved(bool),
}

/// Default [`Slot`] that does nothing.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
pub struct Nop;

impl Slot for Nop {
fn on_emit(&mut self, _: Signal) {}
}

0 comments on commit 1c14144

Please sign in to comment.