#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CollectionChange {
pub replaced: Vec<Range<usize>>,
pub inserted: Vec<Range<usize>>,
pub removed: Vec<Range<usize>>,
}
impl CollectionChange {
#[must_use]
pub const fn unchanged() -> Self {
Self {
replaced: Vec::new(),
inserted: Vec::new(),
removed: Vec::new(),
}
}
#[must_use]
pub fn replaced(range: Range<usize>) -> Self {
Self {
replaced: alloc::vec![range],
..Self::unchanged()
}
}
#[must_use]
pub fn inserted(range: Range<usize>) -> Self {
Self {
inserted: alloc::vec![range],
..Self::unchanged()
}
}
#[must_use]
pub fn removed(range: Range<usize>) -> Self {
Self {
removed: alloc::vec![range],
..Self::unchanged()
}
}
#[must_use]
pub fn everything(len: usize) -> Self {
Self::replaced(0..len)
}
#[must_use]
pub fn populated(start: usize, len: usize) -> Self {
Self::inserted(start..start + len)
}
#[must_use]
pub const fn is_empty(&self) -> bool {
self.replaced.is_empty() && self.inserted.is_empty() && self.removed.is_empty()
}
#[must_use]
pub fn is_replaced(&self, index: usize) -> bool {
self.replaced
.iter()
.any(|range| range.start <= index && index < range.end)
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` is not a reactive collection",
label = "expected a `Collection` implementation",
note = "use `nami::collection::List<T>` for an owned reactive sequence; for a sequence derived from a signal such as a `Binding<Vec<T>>`, wrap it in `nami::collection::SignalCollection`"
)]
pub trait Collection: 'static {
type Item: 'static;
type Guard: WatcherGuard;
fn get(&self, index: usize) -> Option<Self::Item>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn watch(
&self,
range: impl RangeBounds<usize>,
watcher: impl for<'a> Fn(Context<&'a [Self::Item]>, CollectionChange) + 'static, ) -> Self::Guard;
}
use core::ops::{Bound, Range, RangeBounds};
use alloc::{boxed::Box, rc::Rc, vec::Vec};
use crate::watcher::{BoxWatcherGuard, Context, WatcherGuard};
impl<T: Clone + 'static> Collection for Vec<T> {
type Item = T;
type Guard = ();
fn get(&self, index: usize) -> Option<Self::Item> {
self.as_slice().get(index).cloned()
}
fn len(&self) -> usize {
<[T]>::len(self)
}
fn watch(
&self,
_range: impl RangeBounds<usize>,
_watcher: impl for<'a> Fn(Context<&'a [Self::Item]>, CollectionChange) + 'static,
) -> Self::Guard {
}
}
impl<T: Clone + 'static> Collection for &'static [T] {
type Item = T;
type Guard = ();
fn get(&self, index: usize) -> Option<Self::Item> {
(*self).get(index).cloned()
}
fn len(&self) -> usize {
<[T]>::len(self)
}
fn watch(
&self,
_range: impl RangeBounds<usize>,
_watcher: impl for<'a> Fn(Context<&'a [Self::Item]>, CollectionChange) + 'static,
) -> Self::Guard {
}
}
impl<T: Clone + 'static, const N: usize> Collection for [T; N] {
type Item = T;
type Guard = ();
fn get(&self, index: usize) -> Option<Self::Item> {
self.as_slice().get(index).cloned()
}
fn len(&self) -> usize {
N
}
fn watch(
&self,
_range: impl RangeBounds<usize>,
_watcher: impl for<'a> Fn(Context<&'a [Self::Item]>, CollectionChange) + 'static,
) -> Self::Guard {
}
}
impl<T: Clone + 'static> Collection for alloc::rc::Rc<[T]> {
type Item = T;
type Guard = ();
fn get(&self, index: usize) -> Option<Self::Item> {
self.as_ref().get(index).cloned()
}
fn len(&self) -> usize {
self.as_ref().len()
}
fn watch(
&self,
_range: impl RangeBounds<usize>,
_watcher: impl for<'a> Fn(Context<&'a [Self::Item]>, CollectionChange) + 'static,
) -> Self::Guard {
}
}
impl<C> Collection for Box<C>
where
C: Collection,
{
type Item = C::Item;
type Guard = C::Guard;
fn get(&self, index: usize) -> Option<Self::Item> {
(**self).get(index)
}
fn len(&self) -> usize {
(**self).len()
}
fn watch(
&self,
range: impl RangeBounds<usize>,
watcher: impl for<'a> Fn(Context<&'a [Self::Item]>, CollectionChange) + 'static,
) -> Self::Guard {
(**self).watch(range, watcher)
}
}
impl<C> Collection for Rc<C>
where
C: Collection,
{
type Item = C::Item;
type Guard = C::Guard;
fn get(&self, index: usize) -> Option<Self::Item> {
(**self).get(index)
}
fn len(&self) -> usize {
(**self).len()
}
fn watch(
&self,
range: impl RangeBounds<usize>,
watcher: impl for<'a> Fn(Context<&'a [Self::Item]>, CollectionChange) + 'static,
) -> Self::Guard {
(**self).watch(range, watcher)
}
}
pub struct AnyCollection<T> {
inner: Box<dyn AnyCollectionImpl<Output = T>>,
}
impl<T> core::fmt::Debug for AnyCollection<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("AnyCollection").finish()
}
}
pub type BoxCollectionWatcher<T> =
Box<dyn for<'a> Fn(Context<&'a [T]>, CollectionChange) + 'static>;
trait AnyCollectionImpl {
type Output;
fn get(&self, index: usize) -> Option<Self::Output>;
fn len(&self) -> usize;
fn is_empty(&self) -> bool;
fn watch(
&self,
range: (Bound<usize>, Bound<usize>),
watcher: BoxCollectionWatcher<Self::Output>,
) -> BoxWatcherGuard;
}
impl<T> AnyCollectionImpl for T
where
T: Collection,
{
type Output = T::Item;
fn get(&self, index: usize) -> Option<Self::Output> {
<T as Collection>::get(self, index)
}
fn len(&self) -> usize {
<T as Collection>::len(self)
}
fn is_empty(&self) -> bool {
<T as Collection>::is_empty(self)
}
fn watch(
&self,
range: (Bound<usize>, Bound<usize>),
watcher: Box<dyn for<'a> Fn(Context<&'a [Self::Output]>, CollectionChange) + 'static>,
) -> BoxWatcherGuard {
Box::new(<T as Collection>::watch(self, range, watcher))
}
}
impl<T> AnyCollection<T> {
pub fn new<C>(collection: C) -> Self
where
C: Collection<Item = T>,
{
Self {
inner: Box::new(collection),
}
}
#[must_use]
pub fn get(&self, index: usize) -> Option<T> {
self.inner.get(index)
}
#[must_use]
pub fn len(&self) -> usize {
self.inner.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn watch(
&self,
range: impl RangeBounds<usize>,
watcher: impl for<'a> Fn(Context<&'a [T]>, CollectionChange) + 'static,
) -> BoxWatcherGuard {
let start_bound = match range.start_bound() {
Bound::Included(&n) => Bound::Included(n),
Bound::Excluded(&n) => Bound::Excluded(n),
Bound::Unbounded => Bound::Unbounded,
};
let end_bound = match range.end_bound() {
Bound::Included(&n) => Bound::Included(n),
Bound::Excluded(&n) => Bound::Excluded(n),
Bound::Unbounded => Bound::Unbounded,
};
self.inner
.watch((start_bound, end_bound), Box::new(watcher))
}
}