|
@@ -1,8 +1,9 @@
|
|
use crate::read::Readable;
|
|
use crate::read::Readable;
|
|
|
|
+use crate::read::ReadableVecExt;
|
|
use crate::rt::CopyValue;
|
|
use crate::rt::CopyValue;
|
|
use crate::signal::{Signal, Write};
|
|
use crate::signal::{Signal, Write};
|
|
use crate::write::Writable;
|
|
use crate::write::Writable;
|
|
-use crate::{GlobalMemo, GlobalSignal, ReadOnlySignal, SignalData};
|
|
|
|
|
|
+use crate::{GlobalMemo, GlobalSignal, ReadOnlySignal, ReadableValueIterator, SignalData};
|
|
use generational_box::{AnyStorage, Storage};
|
|
use generational_box::{AnyStorage, Storage};
|
|
use generational_box::{GenerationalRef, UnsyncStorage};
|
|
use generational_box::{GenerationalRef, UnsyncStorage};
|
|
|
|
|
|
@@ -134,194 +135,15 @@ macro_rules! write_impls {
|
|
self.with(|v| *v / rhs)
|
|
self.with(|v| *v / rhs)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- write_vec_impls!($ty, S: $vec_bound);
|
|
|
|
- };
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-macro_rules! write_vec_impls {
|
|
|
|
- ($ty:ident $(, $vec_bound_ty:ident: $vec_bound:path)?) => {
|
|
|
|
- impl<T: 'static $(, $vec_bound_ty: $vec_bound)?> $ty<Vec<T> $(, $vec_bound_ty)?> {
|
|
|
|
- /// Pushes a new value to the end of the vector.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn push(&mut self, value: T) {
|
|
|
|
- self.with_mut(|v| v.push(value))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Pops the last value from the vector.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn pop(&mut self) -> Option<T> {
|
|
|
|
- self.with_mut(|v| v.pop())
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Inserts a new value at the given index.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn insert(&mut self, index: usize, value: T) {
|
|
|
|
- self.with_mut(|v| v.insert(index, value))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Removes the value at the given index.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn remove(&mut self, index: usize) -> T {
|
|
|
|
- self.with_mut(|v| v.remove(index))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Clears the vector, removing all values.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn clear(&mut self) {
|
|
|
|
- self.with_mut(|v| v.clear())
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Extends the vector with the given iterator.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn extend(&mut self, iter: impl IntoIterator<Item = T>) {
|
|
|
|
- self.with_mut(|v| v.extend(iter))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Truncates the vector to the given length.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn truncate(&mut self, len: usize) {
|
|
|
|
- self.with_mut(|v| v.truncate(len))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Swaps two values in the vector.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn swap_remove(&mut self, index: usize) -> T {
|
|
|
|
- self.with_mut(|v| v.swap_remove(index))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Retains only the values that match the given predicate.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn retain(&mut self, f: impl FnMut(&T) -> bool) {
|
|
|
|
- self.with_mut(|v| v.retain(f))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Splits the vector into two at the given index.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn split_off(&mut self, at: usize) -> Vec<T> {
|
|
|
|
- self.with_mut(|v| v.split_off(at))
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
read_impls!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
|
|
read_impls!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
|
|
-
|
|
|
|
-impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
|
|
|
|
- /// Read a value from the inner vector.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn get(&self, index: usize) -> Option<S::Ref<T>> {
|
|
|
|
- S::try_map(self.read(), move |v| v.get(index))
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
|
|
|
- /// Unwraps the inner value and clones it.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn unwrap(&self) -> T
|
|
|
|
- where
|
|
|
|
- T: Clone,
|
|
|
|
- {
|
|
|
|
- self.with(|v| v.clone()).unwrap()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Attempts to read the inner value of the Option.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn as_ref(&self) -> Option<S::Ref<T>> {
|
|
|
|
- S::try_map(self.read(), |v| v.as_ref())
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
|
|
write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
|
|
|
|
|
|
-impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
|
|
|
- /// Takes the value out of the Option.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn take(&self) -> Option<T> {
|
|
|
|
- self.with_mut(|v| v.take())
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Replace the value in the Option.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn replace(&self, value: T) -> Option<T> {
|
|
|
|
- self.with_mut(|v| v.replace(value))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Gets the value out of the Option, or inserts the given value if the Option is empty.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn get_or_insert(&self, default: T) -> S::Ref<T> {
|
|
|
|
- self.get_or_insert_with(|| default)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
|
|
|
|
- #[track_caller]
|
|
|
|
- pub fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> S::Ref<T> {
|
|
|
|
- let borrow = self.read();
|
|
|
|
- if borrow.is_none() {
|
|
|
|
- drop(borrow);
|
|
|
|
- self.with_mut(|v| *v = Some(default()));
|
|
|
|
- S::map(self.read(), |v| v.as_ref().unwrap())
|
|
|
|
- } else {
|
|
|
|
- S::map(borrow, |v| v.as_ref().unwrap())
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
read_impls!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
|
|
read_impls!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
|
|
-
|
|
|
|
-impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S> {
|
|
|
|
- /// Read a value from the inner vector.
|
|
|
|
- pub fn get(&self, index: usize) -> Option<S::Ref<T>> {
|
|
|
|
- S::try_map(self.read(), move |v| v.get(index))
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
|
|
|
|
- /// Unwraps the inner value and clones it.
|
|
|
|
- pub fn unwrap(&self) -> T
|
|
|
|
- where
|
|
|
|
- T: Clone,
|
|
|
|
- {
|
|
|
|
- self.with(|v| v.clone()).unwrap()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Attempts to read the inner value of the Option.
|
|
|
|
- pub fn as_ref(&self) -> Option<S::Ref<T>> {
|
|
|
|
- S::try_map(self.read(), |v| v.as_ref())
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
|
|
write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
|
|
|
|
|
|
-impl<T: 'static, S: Storage<SignalData<Option<T>>>> Signal<Option<T>, S> {
|
|
|
|
- /// Takes the value out of the Option.
|
|
|
|
- pub fn take(&mut self) -> Option<T> {
|
|
|
|
- self.with_mut(|v| v.take())
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Replace the value in the Option.
|
|
|
|
- pub fn replace(&mut self, value: T) -> Option<T> {
|
|
|
|
- self.with_mut(|v| v.replace(value))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Gets the value out of the Option, or inserts the given value if the Option is empty.
|
|
|
|
- pub fn get_or_insert(&mut self, default: T) -> S::Ref<T> {
|
|
|
|
- self.get_or_insert_with(|| default)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
|
|
|
|
- pub fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> S::Ref<T> {
|
|
|
|
- let borrow = self.read();
|
|
|
|
- if borrow.is_none() {
|
|
|
|
- drop(borrow);
|
|
|
|
- self.with_mut(|v| *v = Some(default()));
|
|
|
|
- S::map(self.read(), |v| v.as_ref().unwrap())
|
|
|
|
- } else {
|
|
|
|
- S::map(borrow, |v| v.as_ref().unwrap())
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
read_impls!(
|
|
read_impls!(
|
|
ReadOnlySignal,
|
|
ReadOnlySignal,
|
|
S: Storage<SignalData<T>>,
|
|
S: Storage<SignalData<T>>,
|
|
@@ -329,63 +151,6 @@ read_impls!(
|
|
);
|
|
);
|
|
|
|
|
|
read_impls!(GlobalSignal);
|
|
read_impls!(GlobalSignal);
|
|
-
|
|
|
|
-impl<T: 'static> GlobalSignal<Vec<T>> {
|
|
|
|
- /// Read a value from the inner vector.
|
|
|
|
- pub fn get(&'static self, index: usize) -> Option<GenerationalRef<Ref<'static, T>>> {
|
|
|
|
- <UnsyncStorage as AnyStorage>::try_map(self.read(), move |v| v.get(index))
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T: 'static> GlobalSignal<Option<T>> {
|
|
|
|
- /// Unwraps the inner value and clones it.
|
|
|
|
- pub fn unwrap(&'static self) -> T
|
|
|
|
- where
|
|
|
|
- T: Clone,
|
|
|
|
- {
|
|
|
|
- self.with(|v| v.clone()).unwrap()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Attempts to read the inner value of the Option.
|
|
|
|
- pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
|
|
|
|
- <UnsyncStorage as AnyStorage>::try_map(self.read(), |v| v.as_ref())
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-write_vec_impls!(GlobalSignal);
|
|
|
|
-
|
|
|
|
-impl<T: 'static> GlobalSignal<Option<T>> {
|
|
|
|
- /// Takes the value out of the Option.
|
|
|
|
- pub fn take(&self) -> Option<T> {
|
|
|
|
- self.with_mut(|v| v.take())
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Replace the value in the Option.
|
|
|
|
- pub fn replace(&self, value: T) -> Option<T> {
|
|
|
|
- self.with_mut(|v| v.replace(value))
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Gets the value out of the Option, or inserts the given value if the Option is empty.
|
|
|
|
- pub fn get_or_insert(&self, default: T) -> GenerationalRef<Ref<'static, T>> {
|
|
|
|
- self.get_or_insert_with(|| default)
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
|
|
|
|
- pub fn get_or_insert_with(
|
|
|
|
- &self,
|
|
|
|
- default: impl FnOnce() -> T,
|
|
|
|
- ) -> GenerationalRef<Ref<'static, T>> {
|
|
|
|
- let borrow = self.read();
|
|
|
|
- if borrow.is_none() {
|
|
|
|
- drop(borrow);
|
|
|
|
- self.with_mut(|v| *v = Some(default()));
|
|
|
|
- <UnsyncStorage as AnyStorage>::map(self.read(), |v| v.as_ref().unwrap())
|
|
|
|
- } else {
|
|
|
|
- <UnsyncStorage as AnyStorage>::map(borrow, |v| v.as_ref().unwrap())
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
read_impls!(GlobalMemo: PartialEq);
|
|
read_impls!(GlobalMemo: PartialEq);
|
|
|
|
|
|
impl<T: PartialEq + 'static> GlobalMemo<Vec<T>> {
|
|
impl<T: PartialEq + 'static> GlobalMemo<Vec<T>> {
|
|
@@ -395,54 +160,13 @@ impl<T: PartialEq + 'static> GlobalMemo<Vec<T>> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-impl<T: PartialEq + 'static> GlobalMemo<Option<T>> {
|
|
|
|
- /// Unwraps the inner value and clones it.
|
|
|
|
- pub fn unwrap(&'static self) -> T
|
|
|
|
- where
|
|
|
|
- T: Clone,
|
|
|
|
- {
|
|
|
|
- self.with(|v| v.clone()).unwrap()
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /// Attempts to read the inner value of the Option.
|
|
|
|
- pub fn as_ref(&'static self) -> Option<GenerationalRef<Ref<'static, T>>> {
|
|
|
|
- <UnsyncStorage as AnyStorage>::try_map(self.read(), |v| v.as_ref())
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/// An iterator over the values of a `CopyValue<Vec<T>>`.
|
|
|
|
-pub struct CopyValueIterator<T: 'static, S: Storage<Vec<T>>> {
|
|
|
|
- index: usize,
|
|
|
|
- value: CopyValue<Vec<T>, S>,
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T, S: Storage<Vec<T>>> Iterator for CopyValueIterator<T, S> {
|
|
|
|
- type Item = S::Ref<T>;
|
|
|
|
-
|
|
|
|
- fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
- let index = self.index;
|
|
|
|
- self.index += 1;
|
|
|
|
- self.value.get(index)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
|
|
impl<T: 'static, S: Storage<Vec<T>>> IntoIterator for CopyValue<Vec<T>, S> {
|
|
- type IntoIter = CopyValueIterator<T, S>;
|
|
|
|
|
|
+ type IntoIter = ReadableValueIterator<T, Self>;
|
|
|
|
|
|
type Item = S::Ref<T>;
|
|
type Item = S::Ref<T>;
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
- CopyValueIterator {
|
|
|
|
- index: 0,
|
|
|
|
- value: self,
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T: 'static, S: Storage<Vec<T>>> CopyValue<Vec<T>, S> {
|
|
|
|
- /// Write to an element in the inner vector.
|
|
|
|
- pub fn get_mut(&self, index: usize) -> Option<S::Mut<T>> {
|
|
|
|
- S::try_map_mut(self.write(), |v: &mut Vec<T>| v.get_mut(index))
|
|
|
|
|
|
+ self.iter()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -453,39 +177,13 @@ impl<T: 'static, S: Storage<Option<T>>> CopyValue<Option<T>, S> {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-/// An iterator over items in a `Signal<Vec<T>>`.
|
|
|
|
-pub struct SignalIterator<T: 'static, S: Storage<SignalData<Vec<T>>>> {
|
|
|
|
- index: usize,
|
|
|
|
- value: Signal<Vec<T>, S>,
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T, S: Storage<SignalData<Vec<T>>>> Iterator for SignalIterator<T, S> {
|
|
|
|
- type Item = S::Ref<T>;
|
|
|
|
-
|
|
|
|
- fn next(&mut self) -> Option<Self::Item> {
|
|
|
|
- let index = self.index;
|
|
|
|
- self.index += 1;
|
|
|
|
- self.value.get(index)
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
|
|
impl<T: 'static, S: Storage<SignalData<Vec<T>>>> IntoIterator for Signal<Vec<T>, S> {
|
|
- type IntoIter = SignalIterator<T, S>;
|
|
|
|
|
|
+ type IntoIter = ReadableValueIterator<T, Self>;
|
|
|
|
|
|
type Item = S::Ref<T>;
|
|
type Item = S::Ref<T>;
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
- SignalIterator {
|
|
|
|
- index: 0,
|
|
|
|
- value: self,
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-impl<T: 'static, S: Storage<SignalData<Vec<T>>>> Signal<Vec<T>, S> {
|
|
|
|
- /// Returns a reference to an element or `None` if out of bounds.
|
|
|
|
- pub fn get_mut(&mut self, index: usize) -> Option<Write<T, S>> {
|
|
|
|
- Write::filter_map(self.write(), |v| v.get_mut(index))
|
|
|
|
|
|
+ self.iter()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|