|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +use std::num::NonZeroU64; |
| 8 | + |
| 9 | +use godot_ffi as sys; |
| 10 | +use sys::{ffi_methods, GodotFfi}; |
| 11 | + |
| 12 | +/// A RID ("resource ID") is an opaque handle that refers to a Godot `Resource`. |
| 13 | +/// |
| 14 | +/// RIDs do not grant access to the resource itself. Instead, they can be used in lower-level resource APIs |
| 15 | +/// such as the [servers]. See also [Godot API docs for `RID`][docs]. |
| 16 | +/// |
| 17 | +/// RIDs should be largely safe to work with. Certain calls to servers may fail, however doing so will |
| 18 | +/// trigger an error from Godot, and will not cause any UB. |
| 19 | +/// |
| 20 | +/// # Layout: |
| 21 | +/// |
| 22 | +/// `Rid` has the same layout as a [`u64`], where [`Rid::Invalid`] is 0, and [`Rid::Valid(i)`] is `i`. |
| 23 | +/// |
| 24 | +/// # Safety Caveat: |
| 25 | +/// |
| 26 | +/// In Godot 3, RID was not as safe as described here. We believe that this is fixed in Godot 4, but this has |
| 27 | +/// not been extensively tested as of yet. Some confirmed UB from Godot 3 does not occur anymore, but if you |
| 28 | +/// find anything suspicious or outright UB please open an issue. |
| 29 | +/// |
| 30 | +/// [servers]: https://docs.godotengine.org/en/stable/tutorials/optimization/using_servers.html |
| 31 | +/// [docs]: https://docs.godotengine.org/en/stable/classes/class_rid.html |
| 32 | +
|
| 33 | +// Using normal rust repr to take advantage advantage of the nullable pointer optimization. As this enum is |
| 34 | +// eligible for it, it is also guaranteed to have it. Meaning the layout of this type is identical to `u64`. |
| 35 | +// See: https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization |
| 36 | +// Cannot use `#[repr(C)]` as it does not use the nullable pointer optimization. |
| 37 | +#[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)] |
| 38 | +pub enum Rid { |
| 39 | + /// A valid RID may refer to some resource, but is not guaranteed to do so. |
| 40 | + Valid(NonZeroU64), |
| 41 | + /// An invalid RID will never refer to a resource. Internally it is represented as a 0. |
| 42 | + Invalid, |
| 43 | +} |
| 44 | + |
| 45 | +impl Rid { |
| 46 | + /// Create a new RID. |
| 47 | + #[inline] |
| 48 | + pub const fn new(id: u64) -> Self { |
| 49 | + match NonZeroU64::new(id) { |
| 50 | + Some(id) => Self::Valid(id), |
| 51 | + None => Self::Invalid, |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + /// Convert this RID into a [`u64`]. |
| 56 | + /// |
| 57 | + /// _Godot equivalent: `Rid.get_id()`_ |
| 58 | + #[inline] |
| 59 | + pub const fn to_u64(self) -> u64 { |
| 60 | + match self { |
| 61 | + Rid::Valid(id) => id.get(), |
| 62 | + Rid::Invalid => 0, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Convert this RID into a [`NonZeroU64`]. |
| 67 | + #[inline] |
| 68 | + pub const fn to_non_zero_u64(self) -> Option<NonZeroU64> { |
| 69 | + match self { |
| 70 | + Rid::Valid(id) => Some(id), |
| 71 | + Rid::Invalid => None, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// Returns `true` if this is a valid RID. |
| 76 | + #[inline] |
| 77 | + pub const fn is_valid(&self) -> bool { |
| 78 | + matches!(self, Rid::Valid(_)) |
| 79 | + } |
| 80 | + |
| 81 | + /// Returns `true` if this is an invalid RID. |
| 82 | + #[inline] |
| 83 | + pub const fn is_invalid(&self) -> bool { |
| 84 | + matches!(self, Rid::Invalid) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +impl From<u64> for Rid { |
| 89 | + #[inline] |
| 90 | + fn from(id: u64) -> Self { |
| 91 | + Self::new(id) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl From<NonZeroU64> for Rid { |
| 96 | + #[inline] |
| 97 | + fn from(id: NonZeroU64) -> Self { |
| 98 | + Self::Valid(id) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl From<Rid> for u64 { |
| 103 | + #[inline] |
| 104 | + fn from(rid: Rid) -> Self { |
| 105 | + rid.to_u64() |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +impl TryFrom<Rid> for NonZeroU64 { |
| 110 | + type Error = (); |
| 111 | + |
| 112 | + #[inline] |
| 113 | + fn try_from(rid: Rid) -> Result<Self, Self::Error> { |
| 114 | + rid.to_non_zero_u64().ok_or(()) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +impl From<Rid> for Option<NonZeroU64> { |
| 119 | + #[inline] |
| 120 | + fn from(rid: Rid) -> Self { |
| 121 | + match rid { |
| 122 | + Rid::Invalid => None, |
| 123 | + Rid::Valid(id) => Some(id), |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +impl From<Option<NonZeroU64>> for Rid { |
| 129 | + #[inline] |
| 130 | + fn from(id: Option<NonZeroU64>) -> Self { |
| 131 | + match id { |
| 132 | + Some(id) => Rid::Valid(id), |
| 133 | + None => Rid::Invalid, |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl GodotFfi for Rid { |
| 139 | + ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } |
| 140 | +} |
0 commit comments