Fix #2612: adjust readable trait to allow try_peek (#2714)

* Fix #2612: adjust readable trait to allow try_peek

Our implementation for Readable was inconsistent.
We had a try_unchecked variant for read but not for peek.
This resolves that by making a breaking change to the
Readable interface.

* fix doctests too
This commit is contained in:
Jonathan Kelley authored and GitHub committed 2024-07-26 18:20:16 -05:00
1 parent 443b9a4af6
commit e42b71b1ba
13 files changed
+102 -58

No files matched your search

+42 -15
View File
@@ -83,10 +83,20 @@ pub trait Readable {
dyn Fn() -> Result<ReadableRef<'static, Self, O>, generational_box::BorrowError>
+ 'static,
>;
let peek = Rc::new(move || {
<Self::Storage as AnyStorage>::map(self.peek_unchecked(), |r| mapping(r))
}) as Rc<dyn Fn() -> ReadableRef<'static, Self, O> + 'static>;
MappedSignal::new(try_read, peek)
let try_peek = Rc::new({
let self_ = self.clone();
let mapping = mapping.clone();
move || {
self_
.try_peek_unchecked()
.map(|ref_| <Self::Storage as AnyStorage>::map(ref_, |r| mapping(r)))
}
})
as Rc<
dyn Fn() -> Result<ReadableRef<'static, Self, O>, generational_box::BorrowError>
+ 'static,
>;
MappedSignal::new(try_read, try_peek)
}
/// Get the current value of the state. If this is a signal, this will subscribe the current scope to the signal.
@@ -104,13 +114,6 @@ pub trait Readable {
.map(Self::Storage::downcast_lifetime_ref)
}
/// Try to get a reference to the value without checking the lifetime. This will subscribe the current scope to the signal.
///
/// NOTE: This method is completely safe because borrow checking is done at runtime.
fn try_read_unchecked(
&self,
) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError>;
/// Get a reference to the value without checking the lifetime. This will subscribe the current scope to the signal.
///
/// NOTE: This method is completely safe because borrow checking is done at runtime.
@@ -119,12 +122,12 @@ pub trait Readable {
self.try_read_unchecked().unwrap()
}
/// Get the current value of the signal without checking the lifetime. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
///
/// If the signal has been dropped, this will panic.
/// Try to get a reference to the value without checking the lifetime. This will subscribe the current scope to the signal.
///
/// NOTE: This method is completely safe because borrow checking is done at runtime.
fn peek_unchecked(&self) -> ReadableRef<'static, Self>;
fn try_read_unchecked(
&self,
) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError>;
/// Get the current value of the state without subscribing to updates. If the value has been dropped, this will panic.
///
@@ -164,6 +167,30 @@ pub trait Readable {
Self::Storage::downcast_lifetime_ref(self.peek_unchecked())
}
/// Try to peek the current value of the signal without subscribing to updates. If the value has
/// been dropped, this will return an error.
#[track_caller]
fn try_peek(&self) -> Result<ReadableRef<Self>, generational_box::BorrowError> {
self.try_peek_unchecked()
.map(Self::Storage::downcast_lifetime_ref)
}
/// Get the current value of the signal without checking the lifetime. **Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.**
///
/// If the signal has been dropped, this will panic.
#[track_caller]
fn peek_unchecked(&self) -> ReadableRef<'static, Self> {
self.try_peek_unchecked().unwrap()
}
/// Try to peek the current value of the signal without subscribing to updates. If the value has
/// been dropped, this will return an error.
///
/// NOTE: This method is completely safe because borrow checking is done at runtime.
fn try_peek_unchecked(
&self,
) -> Result<ReadableRef<'static, Self>, generational_box::BorrowError>;
/// Clone the inner value and return it. If the value has been dropped, this will panic.
#[track_caller]
fn cloned(&self) -> Self::Target