#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Sub<L> {
#[default]
None,
Single(L),
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
Many(Vec<L>),
}
impl<L> Sub<L> {
#[inline]
pub const fn none() -> Self {
Self::None
}
#[inline]
pub const fn single(listener: L) -> Self {
Self::Single(listener)
}
#[cfg(feature = "alloc")]
pub fn many(items: impl IntoIterator<Item = L>) -> Self {
let v: alloc::vec::Vec<L> = items.into_iter().collect();
match v.len() {
0 => Self::None,
1 => Self::Single(v.into_iter().next().expect("len 1")),
_ => Self::Many(v),
}
}
pub fn iter(&self) -> SubIter<'_, L> {
match self {
Self::None => SubIter {
one: None,
#[cfg(feature = "alloc")]
rest: None,
},
Self::Single(l) => SubIter {
one: Some(l),
#[cfg(feature = "alloc")]
rest: None,
},
#[cfg(feature = "alloc")]
Self::Many(v) => SubIter {
one: None,
rest: Some(v.iter()),
},
}
}
#[inline]
pub const fn is_none(&self) -> bool {
matches!(self, Self::None)
}
pub fn len(&self) -> usize {
match self {
Self::None => 0,
Self::Single(_) => 1,
#[cfg(feature = "alloc")]
Self::Many(v) => v.len(),
}
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn and(self, other: Self) -> Self {
match (self, other) {
(Self::None, x) | (x, Self::None) => x,
#[cfg(feature = "alloc")]
(a, b) => {
let mut v = Vec::new();
match a {
Self::None => {}
Self::Single(x) => v.push(x),
Self::Many(xs) => v.extend(xs),
}
match b {
Self::None => {}
Self::Single(x) => v.push(x),
Self::Many(xs) => v.extend(xs),
}
Self::many(v)
}
#[cfg(not(feature = "alloc"))]
(Self::Single(_), Self::Single(_)) => {
panic!("Sub::and of two listeners needs feature `alloc`")
}
}
}
pub fn contains(&self, needle: &L) -> bool
where
L: PartialEq,
{
self.iter().any(|l| l == needle)
}
pub fn diff(&self, new: &Self) -> Diff<L>
where
L: PartialEq + Clone,
{
let mut start = Self::none();
let mut stop = Self::none();
for l in new.iter() {
if !self.contains(l) {
start = start.and(Self::single(l.clone()));
}
}
for l in self.iter() {
if !new.contains(l) {
stop = stop.and(Self::single(l.clone()));
}
}
Diff { start, stop }
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Diff<L> {
pub start: Sub<L>,
pub stop: Sub<L>,
}
pub struct SubIter<'a, L> {
one: Option<&'a L>,
#[cfg(feature = "alloc")]
rest: Option<core::slice::Iter<'a, L>>,
}
impl<'a, L> Iterator for SubIter<'a, L> {
type Item = &'a L;
fn next(&mut self) -> Option<Self::Item> {
if let Some(l) = self.one.take() {
return Some(l);
}
#[cfg(feature = "alloc")]
if let Some(ref mut rest) = self.rest {
return rest.next();
}
None
}
}
#[cfg(test)]
mod tests {
use super::Sub;
#[test]
fn diff_none_to_one() {
let old = Sub::<&str>::none();
let new = Sub::single("clock");
let d = old.diff(&new);
assert_eq!(d.start, Sub::single("clock"));
assert!(d.stop.is_none());
}
#[test]
fn diff_one_to_none() {
let old = Sub::single("clock");
let new = Sub::<&str>::none();
let d = old.diff(&new);
assert!(d.start.is_none());
assert_eq!(d.stop, Sub::single("clock"));
}
#[test]
fn diff_unchanged() {
let s = Sub::single("clock");
let d = s.diff(&s);
assert!(d.start.is_none());
assert!(d.stop.is_none());
}
#[cfg(feature = "alloc")]
#[test]
fn diff_swap() {
let old = Sub::many(["clock", "feed"]);
let new = Sub::many(["feed", "keys"]);
let d = old.diff(&new);
assert!(d.start.contains(&"keys"));
assert!(!d.start.contains(&"feed"));
assert!(d.stop.contains(&"clock"));
assert!(!d.stop.contains(&"feed"));
}
}