|
| 1 | +/* |
| 2 | + * Copyright (c) godot-rust; Bromeon and contributors. |
| 3 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 4 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 6 | + */ |
| 7 | + |
| 8 | +use std::fmt::{Debug, Formatter, Result as FmtResult}; |
| 9 | +use std::mem::ManuallyDrop; |
| 10 | +use std::ops::{Deref, DerefMut}; |
| 11 | + |
| 12 | +use crate::classes; |
| 13 | +use crate::obj::{bounds, Base, Bounds, Gd, GdDerefTarget, GodotClass, Inherits, RawGd}; |
| 14 | + |
| 15 | +/// Weak pointer to objects owned by the Godot engine. |
| 16 | +/// `WeakGd<T>` doesn't guarantee the validation of the object and can hold a dead object. |
| 17 | +/// For `RefCounted`, it doesn't affect the reference count, which means it doesn't decrease reference count when dropped and doesn't prevent the `RefCounted` from being released. |
| 18 | +/// Can be used during initialization [`I*::init()`][crate::classes::IObject::init] or [`Gd::from_init_fn()`], and deconstruction [Drop]. |
| 19 | +/// |
| 20 | +/// # Panics |
| 21 | +/// If the weak pointer is invalid when dereferencing to call a Godot method. |
| 22 | +pub struct WeakGd<T: GodotClass> { |
| 23 | + gd: Option<ManuallyDrop<Gd<T>>>, |
| 24 | +} |
| 25 | + |
| 26 | +impl<T: GodotClass> WeakGd<T> { |
| 27 | + fn from_raw_gd(val: &RawGd<T>) -> Self { |
| 28 | + Self { |
| 29 | + gd: if val.is_instance_valid() { |
| 30 | + unsafe { Some(ManuallyDrop::new(Gd::from_obj_sys_weak(val.obj_sys()))) } |
| 31 | + } else { |
| 32 | + None |
| 33 | + }, |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Create a weak pointer from a [Gd]. |
| 38 | + pub fn from_gd(val: &Gd<T>) -> Self { |
| 39 | + Self::from_raw_gd(&val.raw) |
| 40 | + } |
| 41 | + |
| 42 | + /// Create a weak pointer from a [Base]. |
| 43 | + pub fn from_base(val: &Base<T>) -> Self { |
| 44 | + Self { |
| 45 | + gd: if val.is_instance_valid() { |
| 46 | + unsafe { Some(ManuallyDrop::new(Gd::from_obj_sys_weak(val.obj_sys()))) } |
| 47 | + } else { |
| 48 | + None |
| 49 | + }, |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fn ffi_cast<U: GodotClass>(self) -> Result<WeakGd<U>, Self> { |
| 54 | + if !self.is_instance_valid() { |
| 55 | + return Err(WeakGd { gd: None }); |
| 56 | + } |
| 57 | + let WeakGd { gd } = self; |
| 58 | + let mut gd = gd.unwrap(); |
| 59 | + let Gd { raw } = unsafe { ManuallyDrop::take(&mut gd) }; |
| 60 | + let cast = raw.ffi_cast::<U>(); |
| 61 | + |
| 62 | + let raw = match cast { |
| 63 | + Ok(res) => Ok(ManuallyDrop::new(res.into_dest(raw))), |
| 64 | + Err(_) => Err(ManuallyDrop::new(raw)), |
| 65 | + }; |
| 66 | + raw.map(|v| WeakGd::from_raw_gd(&v)) |
| 67 | + .map_err(|e| WeakGd::from_raw_gd(&e)) |
| 68 | + } |
| 69 | + |
| 70 | + /// **Downcast:** try to convert into a weak pointer to a derived class, fails if this weak pointer is invalid. |
| 71 | + pub fn try_cast<Derived>(self) -> Result<WeakGd<Derived>, Self> |
| 72 | + where |
| 73 | + Derived: Inherits<T>, |
| 74 | + { |
| 75 | + self.ffi_cast() |
| 76 | + } |
| 77 | + |
| 78 | + /// **Upcast:** try to convert into a weak pointer to a base class, fails if this weak pointer is invalid. |
| 79 | + pub fn try_upcast<Base>(self) -> Result<WeakGd<Base>, Self> |
| 80 | + where |
| 81 | + Base: GodotClass, |
| 82 | + T: Inherits<Base>, |
| 83 | + { |
| 84 | + self.ffi_cast() |
| 85 | + } |
| 86 | + |
| 87 | + /// Checks if this weak pointer points to a live object. |
| 88 | + pub fn is_instance_valid(&self) -> bool { |
| 89 | + self.gd |
| 90 | + .as_ref() |
| 91 | + .map(|v| v.is_instance_valid()) |
| 92 | + .unwrap_or(false) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl<T: GodotClass> Deref for WeakGd<T> |
| 97 | +where |
| 98 | + GdDerefTarget<T>: Bounds<Declarer = bounds::DeclEngine>, |
| 99 | +{ |
| 100 | + type Target = GdDerefTarget<T>; |
| 101 | + |
| 102 | + fn deref(&self) -> &Self::Target { |
| 103 | + self.gd.as_ref().unwrap().deref() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +impl<T: GodotClass> DerefMut for WeakGd<T> |
| 108 | +where |
| 109 | + GdDerefTarget<T>: Bounds<Declarer = bounds::DeclEngine>, |
| 110 | +{ |
| 111 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 112 | + self.gd.as_mut().unwrap().deref_mut() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +impl<T: GodotClass> Clone for WeakGd<T> { |
| 117 | + fn clone(&self) -> Self { |
| 118 | + if let Some(gd) = self.gd.as_ref() { |
| 119 | + Self::from_gd(gd) |
| 120 | + } else { |
| 121 | + Self { gd: None } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl<T: GodotClass> Debug for WeakGd<T> { |
| 127 | + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { |
| 128 | + if self.is_instance_valid() { |
| 129 | + classes::debug_string(self.gd.as_ref().unwrap(), f, "WeakGd") |
| 130 | + } else { |
| 131 | + write!(f, "WeakGd {{ null }}") |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments