Add downcast_ref to AnyViewHandle
I use unsafe code to transmute an AnyViewHandle to a ViewHandle<T> when the type matches. Because ViewHandle<T> is repr(transparent) to the wrapped AnyViewHandle, this is safe.
This commit is contained in:
parent
71cf6ec5ae
commit
138aa0492a
4 changed files with 15 additions and 12 deletions
|
@ -353,8 +353,7 @@ mod tests {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
active_item
|
active_item
|
||||||
.as_any()
|
.as_any()
|
||||||
.clone()
|
.downcast_ref::<Editor>()
|
||||||
.downcast::<Editor>()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.title(cx),
|
.title(cx),
|
||||||
|
|
|
@ -4661,6 +4661,7 @@ impl<T> Clone for WeakModelHandle<T> {
|
||||||
|
|
||||||
impl<T> Copy for WeakModelHandle<T> {}
|
impl<T> Copy for WeakModelHandle<T> {}
|
||||||
|
|
||||||
|
#[repr(transparent)]
|
||||||
pub struct ViewHandle<T> {
|
pub struct ViewHandle<T> {
|
||||||
any_handle: AnyViewHandle,
|
any_handle: AnyViewHandle,
|
||||||
view_type: PhantomData<T>,
|
view_type: PhantomData<T>,
|
||||||
|
@ -4877,6 +4878,14 @@ impl AnyViewHandle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn downcast_ref<T: View>(&self) -> Option<&ViewHandle<T>> {
|
||||||
|
if self.is::<T>() {
|
||||||
|
Some(unsafe { mem::transmute(self) })
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn downgrade(&self) -> AnyWeakViewHandle {
|
pub fn downgrade(&self) -> AnyWeakViewHandle {
|
||||||
AnyWeakViewHandle {
|
AnyWeakViewHandle {
|
||||||
window_id: self.window_id,
|
window_id: self.window_id,
|
||||||
|
|
|
@ -2279,8 +2279,7 @@ mod tests {
|
||||||
.map(|(ix, item)| {
|
.map(|(ix, item)| {
|
||||||
let mut state = item
|
let mut state = item
|
||||||
.as_any()
|
.as_any()
|
||||||
.clone()
|
.downcast_ref::<TestItem>()
|
||||||
.downcast::<TestItem>()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.label
|
.label
|
||||||
|
|
|
@ -1021,8 +1021,7 @@ mod tests {
|
||||||
.active_item()
|
.active_item()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_any()
|
.as_any()
|
||||||
.clone()
|
.downcast_ref::<Editor>()
|
||||||
.downcast::<Editor>()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.title(cx),
|
.title(cx),
|
||||||
|
@ -1058,8 +1057,7 @@ mod tests {
|
||||||
.active_item()
|
.active_item()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_any()
|
.as_any()
|
||||||
.clone()
|
.downcast_ref::<Editor>()
|
||||||
.downcast::<Editor>()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.title(cx),
|
.title(cx),
|
||||||
|
@ -1095,8 +1093,7 @@ mod tests {
|
||||||
.active_item()
|
.active_item()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_any()
|
.as_any()
|
||||||
.clone()
|
.downcast_ref::<Editor>()
|
||||||
.downcast::<Editor>()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.title(cx),
|
.title(cx),
|
||||||
|
@ -1146,8 +1143,7 @@ mod tests {
|
||||||
.active_item()
|
.active_item()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_any()
|
.as_any()
|
||||||
.clone()
|
.downcast_ref::<Editor>()
|
||||||
.downcast::<Editor>()
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.read(cx)
|
.read(cx)
|
||||||
.title(cx),
|
.title(cx),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue