restore lifetime checking for signals
This commit is contained in:
1 parent
295c29db5d
commit
60a84956d1
16 files changed
+275
-148
No files matched your search
@@ -1,33 +1,58 @@
|
||||
use std::ops::DerefMut;
|
||||
use std::ops::IndexMut;
|
||||
use std::ops::{DerefMut, IndexMut};
|
||||
|
||||
use crate::read::Readable;
|
||||
|
||||
/// A reference to a value that can be read from.
|
||||
#[allow(type_alias_bounds)]
|
||||
pub type WritableRef<'a, T: Writable, O = <T as Readable>::Target> = T::Mut<'a, O>;
|
||||
|
||||
/// A trait for states that can be read from like [`crate::Signal`], or [`crate::GlobalSignal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a [`crate::Signal`] and one that accepts a [`crate::GlobalSignal`], you can create one function that accepts a [`Writable`] type.
|
||||
pub trait Writable: Readable {
|
||||
/// The type of the reference.
|
||||
type Mut<R: ?Sized + 'static>: DerefMut<Target = R> + 'static;
|
||||
type Mut<'a, R: ?Sized + 'static>: DerefMut<Target = R>;
|
||||
|
||||
/// Map the reference to a new type.
|
||||
fn map_mut<I: ?Sized, U: ?Sized, F: FnOnce(&mut I) -> &mut U>(
|
||||
ref_: Self::Mut<I>,
|
||||
ref_: Self::Mut<'_, I>,
|
||||
f: F,
|
||||
) -> Self::Mut<U>;
|
||||
) -> Self::Mut<'_, U>;
|
||||
|
||||
/// Try to map the reference to a new type.
|
||||
fn try_map_mut<I: ?Sized, U: ?Sized, F: FnOnce(&mut I) -> Option<&mut U>>(
|
||||
ref_: Self::Mut<I>,
|
||||
ref_: Self::Mut<'_, I>,
|
||||
f: F,
|
||||
) -> Option<Self::Mut<U>>;
|
||||
) -> Option<Self::Mut<'_, U>>;
|
||||
|
||||
/// Downcast a mutable reference in a RefMut to a more specific lifetime
|
||||
///
|
||||
/// This function enforces the variance of the lifetime parameter `'a` in Ref.
|
||||
fn downcast_mut<'a: 'b, 'b, T: ?Sized + 'static>(mut_: Self::Mut<'a, T>) -> Self::Mut<'b, T>;
|
||||
|
||||
/// Get a mutable reference to the value. If the value has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
fn write(&mut self) -> Self::Mut<Self::Target> {
|
||||
fn write(&mut self) -> WritableRef<'_, Self> {
|
||||
self.try_write().unwrap()
|
||||
}
|
||||
|
||||
/// Try to get a mutable reference to the value. If the value has been dropped, this will panic.
|
||||
fn try_write(&mut self) -> Result<Self::Mut<Self::Target>, generational_box::BorrowMutError>;
|
||||
/// Try to get a mutable reference to the value.
|
||||
#[track_caller]
|
||||
fn try_write(&mut self) -> Result<WritableRef<'_, Self>, generational_box::BorrowMutError> {
|
||||
self.try_write_unchecked().map(Self::downcast_mut)
|
||||
}
|
||||
|
||||
/// Try to get a mutable reference to the value without checking the lifetime.
|
||||
///
|
||||
/// NOTE: This method is completely safe because borrow checking is done at runtime.
|
||||
fn try_write_unchecked(
|
||||
&self,
|
||||
) -> Result<WritableRef<'static, Self>, generational_box::BorrowMutError>;
|
||||
|
||||
/// Tet a mutable reference to the value without checking the lifetime.
|
||||
///
|
||||
/// NOTE: This method is completely safe because borrow checking is done at runtime.
|
||||
fn write_unchecked(&self) -> WritableRef<'static, Self> {
|
||||
self.try_write_unchecked().unwrap()
|
||||
}
|
||||
|
||||
/// Run a function with a mutable reference to the value. If the value has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
@@ -55,7 +80,10 @@ pub trait Writable: Readable {
|
||||
|
||||
/// Index into the inner value and return a reference to the result.
|
||||
#[track_caller]
|
||||
fn index_mut<I>(&mut self, index: I) -> Self::Mut<<Self::Target as std::ops::Index<I>>::Output>
|
||||
fn index_mut<I>(
|
||||
&mut self,
|
||||
index: I,
|
||||
) -> WritableRef<'_, Self, <Self::Target as std::ops::Index<I>>::Output>
|
||||
where
|
||||
Self::Target: std::ops::IndexMut<I>,
|
||||
{
|
||||
@@ -84,15 +112,14 @@ pub trait Writable: Readable {
|
||||
/// An extension trait for Writable<Option<T>> that provides some convenience methods.
|
||||
pub trait WritableOptionExt<T: 'static>: Writable<Target = Option<T>> {
|
||||
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
|
||||
fn get_or_insert(&mut self, default: T) -> Self::Mut<T> {
|
||||
fn get_or_insert(&mut self, default: T) -> WritableRef<'_, Self, 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.
|
||||
fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> Self::Mut<T> {
|
||||
let borrow = self.read();
|
||||
if borrow.is_none() {
|
||||
drop(borrow);
|
||||
fn get_or_insert_with(&mut self, default: impl FnOnce() -> T) -> WritableRef<'_, Self, T> {
|
||||
let is_none = self.read().is_none();
|
||||
if is_none {
|
||||
self.with_mut(|v| *v = Some(default()));
|
||||
Self::map_mut(self.write(), |v| v.as_mut().unwrap())
|
||||
} else {
|
||||
@@ -102,7 +129,7 @@ pub trait WritableOptionExt<T: 'static>: Writable<Target = Option<T>> {
|
||||
|
||||
/// Attempts to write the inner value of the Option.
|
||||
#[track_caller]
|
||||
fn as_mut(&mut self) -> Option<Self::Mut<T>> {
|
||||
fn as_mut(&mut self) -> Option<WritableRef<'_, Self, T>> {
|
||||
Self::try_map_mut(self.write(), |v: &mut Option<T>| v.as_mut())
|
||||
}
|
||||
}
|
||||
@@ -178,36 +205,40 @@ pub trait WritableVecExt<T: 'static>: Writable<Target = Vec<T>> {
|
||||
|
||||
/// Try to mutably get an element from the vector.
|
||||
#[track_caller]
|
||||
fn get_mut(&mut self, index: usize) -> Option<Self::Mut<T>> {
|
||||
fn get_mut(&mut self, index: usize) -> Option<WritableRef<'_, Self, T>> {
|
||||
Self::try_map_mut(self.write(), |v: &mut Vec<T>| v.get_mut(index))
|
||||
}
|
||||
|
||||
/// Gets an iterator over the values of the vector.
|
||||
#[track_caller]
|
||||
fn iter_mut(&self) -> WritableValueIterator<Self>
|
||||
fn iter_mut(&mut self) -> WritableValueIterator<'_, Self>
|
||||
where
|
||||
Self: Sized + Clone,
|
||||
{
|
||||
WritableValueIterator {
|
||||
index: 0,
|
||||
value: self.clone(),
|
||||
value: self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An iterator over the values of a `Writable<Vec<T>>`.
|
||||
pub struct WritableValueIterator<R> {
|
||||
pub struct WritableValueIterator<'a, R> {
|
||||
index: usize,
|
||||
value: R,
|
||||
value: &'a mut R,
|
||||
}
|
||||
|
||||
impl<T: 'static, R: Writable<Target = Vec<T>>> Iterator for WritableValueIterator<R> {
|
||||
type Item = R::Mut<T>;
|
||||
impl<'a, T: 'static, R: Writable<Target = Vec<T>>> Iterator for WritableValueIterator<'a, R> {
|
||||
type Item = WritableRef<'a, R, T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
let index = self.index;
|
||||
self.index += 1;
|
||||
self.value.get_mut(index)
|
||||
R::try_map_mut(
|
||||
self.value.try_write_unchecked().unwrap(),
|
||||
|v: &mut Vec<T>| v.get_mut(index),
|
||||
)
|
||||
.map(R::downcast_mut)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user