mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-10 06:34:20 +00:00
add more docs around lifetime downcasting
This commit is contained in:
parent
5f9e5f607b
commit
4bc2f4a713
10 changed files with 46 additions and 23 deletions
|
@ -234,13 +234,17 @@ pub trait AnyStorage: Default {
|
|||
|
||||
/// Downcast a reference in a Ref to a more specific lifetime
|
||||
///
|
||||
/// This function enforces the variance of the lifetime parameter `'a` in Ref.
|
||||
fn downcast_ref<'a: 'b, 'b, T: ?Sized + 'static>(ref_: Self::Ref<'a, T>) -> Self::Ref<'b, T>;
|
||||
/// This function enforces the variance of the lifetime parameter `'a` in Ref. Rust will typically infer this cast with a concrete type, but it cannot with a generic type.
|
||||
fn downcast_lifetime_ref<'a: 'b, 'b, T: ?Sized + 'static>(
|
||||
ref_: Self::Ref<'a, T>,
|
||||
) -> Self::Ref<'b, T>;
|
||||
|
||||
/// 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>;
|
||||
/// This function enforces the variance of the lifetime parameter `'a` in Mut. Rust will typically infer this cast with a concrete type, but it cannot with a generic type.
|
||||
fn downcast_lifetime_mut<'a: 'b, 'b, T: ?Sized + 'static>(
|
||||
mut_: Self::Mut<'a, T>,
|
||||
) -> Self::Mut<'b, T>;
|
||||
|
||||
/// Try to map the mutable ref.
|
||||
fn try_map_mut<T: ?Sized + 'static, U: ?Sized + 'static>(
|
||||
|
|
|
@ -23,11 +23,15 @@ impl AnyStorage for SyncStorage {
|
|||
type Ref<'a, R: ?Sized + 'static> = GenerationalRef<MappedRwLockReadGuard<'a, R>>;
|
||||
type Mut<'a, W: ?Sized + 'static> = GenerationalRefMut<MappedRwLockWriteGuard<'a, W>>;
|
||||
|
||||
fn downcast_ref<'a: 'b, 'b, T: ?Sized + 'static>(ref_: Self::Ref<'a, T>) -> Self::Ref<'b, T> {
|
||||
fn downcast_lifetime_ref<'a: 'b, 'b, T: ?Sized + 'static>(
|
||||
ref_: Self::Ref<'a, T>,
|
||||
) -> Self::Ref<'b, T> {
|
||||
ref_
|
||||
}
|
||||
|
||||
fn downcast_mut<'a: 'b, 'b, T: ?Sized + 'static>(mut_: Self::Mut<'a, T>) -> Self::Mut<'b, T> {
|
||||
fn downcast_lifetime_mut<'a: 'b, 'b, T: ?Sized + 'static>(
|
||||
mut_: Self::Mut<'a, T>,
|
||||
) -> Self::Mut<'b, T> {
|
||||
mut_
|
||||
}
|
||||
|
||||
|
|
|
@ -92,11 +92,15 @@ impl AnyStorage for UnsyncStorage {
|
|||
type Ref<'a, R: ?Sized + 'static> = GenerationalRef<Ref<'a, R>>;
|
||||
type Mut<'a, W: ?Sized + 'static> = GenerationalRefMut<RefMut<'a, W>>;
|
||||
|
||||
fn downcast_ref<'a: 'b, 'b, T: ?Sized + 'static>(ref_: Self::Ref<'a, T>) -> Self::Ref<'b, T> {
|
||||
fn downcast_lifetime_ref<'a: 'b, 'b, T: ?Sized + 'static>(
|
||||
ref_: Self::Ref<'a, T>,
|
||||
) -> Self::Ref<'b, T> {
|
||||
ref_
|
||||
}
|
||||
|
||||
fn downcast_mut<'a: 'b, 'b, T: ?Sized + 'static>(mut_: Self::Mut<'a, T>) -> Self::Mut<'b, T> {
|
||||
fn downcast_lifetime_mut<'a: 'b, 'b, T: ?Sized + 'static>(
|
||||
mut_: Self::Mut<'a, T>,
|
||||
) -> Self::Mut<'b, T> {
|
||||
mut_
|
||||
}
|
||||
|
||||
|
|
|
@ -123,7 +123,7 @@ fn Route3(dynamic: String) -> Element {
|
|||
oninput: move |evt| {
|
||||
*current_route_str.write() = evt.value();
|
||||
},
|
||||
value: "{current_route_str.read()}"
|
||||
value: "{current_route_str}"
|
||||
}
|
||||
"dynamic: {dynamic}"
|
||||
Link { to: Route::Route2 { user_id: 8888 }, "hello world link" }
|
||||
|
|
|
@ -30,9 +30,9 @@ fn app() -> Element {
|
|||
|
||||
#[component]
|
||||
fn Read() -> Element {
|
||||
let mut signal = use_signal_sync(|| 0);
|
||||
let signal = use_signal_sync(|| 0);
|
||||
|
||||
let _write = signal.write();
|
||||
let _write = signal.write_unchecked();
|
||||
let _read = signal.read();
|
||||
|
||||
unreachable!()
|
||||
|
@ -40,10 +40,10 @@ fn Read() -> Element {
|
|||
|
||||
#[component]
|
||||
fn ReadMut() -> Element {
|
||||
let mut signal = use_signal_sync(|| 0);
|
||||
let signal = use_signal_sync(|| 0);
|
||||
|
||||
let _read = signal.read();
|
||||
let _write = signal.write();
|
||||
let _write = signal.write_unchecked();
|
||||
|
||||
unreachable!()
|
||||
}
|
||||
|
|
|
@ -238,8 +238,10 @@ impl<T: 'static, S: Storage<T>> Writable for CopyValue<T, S> {
|
|||
S::try_map_mut(mut_, f)
|
||||
}
|
||||
|
||||
fn downcast_mut<'a: 'b, 'b, R: ?Sized + 'static>(mut_: Self::Mut<'a, R>) -> Self::Mut<'b, R> {
|
||||
S::downcast_mut(mut_)
|
||||
fn downcast_lifetime_mut<'a: 'b, 'b, R: ?Sized + 'static>(
|
||||
mut_: Self::Mut<'a, R>,
|
||||
) -> Self::Mut<'b, R> {
|
||||
S::downcast_lifetime_mut(mut_)
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
|
|
|
@ -104,7 +104,9 @@ impl<T: 'static> Writable for GlobalSignal<T> {
|
|||
Write::filter_map(ref_, f)
|
||||
}
|
||||
|
||||
fn downcast_mut<'a: 'b, 'b, R: ?Sized + 'static>(mut_: Self::Mut<'a, R>) -> Self::Mut<'b, R> {
|
||||
fn downcast_lifetime_mut<'a: 'b, 'b, R: ?Sized + 'static>(
|
||||
mut_: Self::Mut<'a, R>,
|
||||
) -> Self::Mut<'b, R> {
|
||||
Write::downcast_lifetime(mut_)
|
||||
}
|
||||
|
||||
|
|
|
@ -53,7 +53,8 @@ pub trait Readable {
|
|||
/// Try to get the current value of the state. If this is a signal, this will subscribe the current scope to the signal.
|
||||
#[track_caller]
|
||||
fn try_read(&self) -> Result<ReadableRef<Self>, generational_box::BorrowError> {
|
||||
self.try_read_unchecked().map(Self::Storage::downcast_ref)
|
||||
self.try_read_unchecked()
|
||||
.map(Self::Storage::downcast_lifetime_ref)
|
||||
}
|
||||
|
||||
/// Try to get a reference to the value without checking the lifetime.
|
||||
|
@ -80,7 +81,7 @@ pub trait Readable {
|
|||
/// Get the current value of the state without subscribing to updates. If the value has been dropped, this will panic.
|
||||
#[track_caller]
|
||||
fn peek(&self) -> ReadableRef<Self> {
|
||||
Self::Storage::downcast_ref(self.peek_unchecked())
|
||||
Self::Storage::downcast_lifetime_ref(self.peek_unchecked())
|
||||
}
|
||||
|
||||
/// Clone the inner value and return it. If the value has been dropped, this will panic.
|
||||
|
|
|
@ -212,7 +212,9 @@ impl<T: 'static, S: Storage<SignalData<T>>> Writable for Signal<T, S> {
|
|||
Write::filter_map(ref_, f)
|
||||
}
|
||||
|
||||
fn downcast_mut<'a: 'b, 'b, R: ?Sized + 'static>(mut_: Self::Mut<'a, R>) -> Self::Mut<'b, R> {
|
||||
fn downcast_lifetime_mut<'a: 'b, 'b, R: ?Sized + 'static>(
|
||||
mut_: Self::Mut<'a, R>,
|
||||
) -> Self::Mut<'b, R> {
|
||||
Write::downcast_lifetime(mut_)
|
||||
}
|
||||
|
||||
|
@ -312,12 +314,14 @@ impl<'a, T: ?Sized + 'static, S: AnyStorage> Write<'a, T, S> {
|
|||
}
|
||||
|
||||
/// Downcast the lifetime of the mutable reference to the signal's value.
|
||||
///
|
||||
/// This function enforces the variance of the lifetime parameter `'a` in Mut. Rust will typically infer this cast with a concrete type, but it cannot with a generic type.
|
||||
pub fn downcast_lifetime<'b>(mut_: Self) -> Write<'b, T, S>
|
||||
where
|
||||
'a: 'b,
|
||||
{
|
||||
Write {
|
||||
write: S::downcast_mut(mut_.write),
|
||||
write: S::downcast_lifetime_mut(mut_.write),
|
||||
drop_signal: mut_.drop_signal,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -26,7 +26,9 @@ pub trait Writable: Readable {
|
|||
/// 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>;
|
||||
fn downcast_lifetime_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]
|
||||
|
@ -37,7 +39,7 @@ pub trait Writable: Readable {
|
|||
/// 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)
|
||||
self.try_write_unchecked().map(Self::downcast_lifetime_mut)
|
||||
}
|
||||
|
||||
/// Try to get a mutable reference to the value without checking the lifetime.
|
||||
|
@ -238,7 +240,7 @@ impl<'a, T: 'static, R: Writable<Target = Vec<T>>> Iterator for WritableValueIte
|
|||
self.value.try_write_unchecked().unwrap(),
|
||||
|v: &mut Vec<T>| v.get_mut(index),
|
||||
)
|
||||
.map(R::downcast_mut)
|
||||
.map(R::downcast_lifetime_mut)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue