-
-
Notifications
You must be signed in to change notification settings - Fork 218
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement support for RID #171
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use std::num::NonZeroU64; | ||
|
||
use godot_ffi as sys; | ||
use sys::{ffi_methods, GodotFfi}; | ||
|
||
/// A RID ("resource ID") is an opaque handle that refers to a Godot `Resource`. | ||
/// | ||
/// RIDs do not grant access to the resource itself. Instead, they can be used in lower-level resource APIs | ||
/// such as the [servers]. See also [Godot API docs for `RID`][docs]. | ||
/// | ||
/// RIDs should be largely safe to work with. Certain calls to servers may fail, however doing so will | ||
/// trigger an error from Godot, and will not cause any UB. | ||
/// | ||
/// # Safety Caveat: | ||
/// | ||
/// In Godot 3, RID was not as safe as described here. We believe that this is fixed in Godot 4, but this has | ||
/// not been extensively tested as of yet. Some confirmed UB from Godot 3 does not occur anymore, but if you | ||
/// find anything suspicious or outright UB please open an issue. | ||
/// | ||
/// [servers]: https://docs.godotengine.org/en/stable/tutorials/optimization/using_servers.html | ||
/// [docs]: https://docs.godotengine.org/en/stable/classes/class_rid.html | ||
|
||
// Using normal rust repr to take advantage advantage of the nullable pointer optimization. As this enum is | ||
// eligible for it, it is also guaranteed to have it. Meaning the layout of this type is identical to `u64`. | ||
// See: https://doc.rust-lang.org/nomicon/ffi.html#the-nullable-pointer-optimization | ||
// Cannot use `#[repr(C)]` as it does not use the nullable pointer optimization. | ||
#[derive(Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)] | ||
pub enum Rid { | ||
/// A valid RID may refer to some resource, but is not guaranteed to do so. | ||
Valid(NonZeroU64), | ||
/// An invalid RID will never refer to a resource. Internally it is represented as a 0. | ||
Invalid, | ||
} | ||
|
||
impl Rid { | ||
/// Create a new RID. | ||
#[inline] | ||
pub const fn new(id: u64) -> Self { | ||
match NonZeroU64::new(id) { | ||
Some(id) => Self::Valid(id), | ||
None => Self::Invalid, | ||
} | ||
} | ||
|
||
/// Convert this RID into a [`u64`]. Returns 0 if it is invalid. | ||
/// | ||
/// _Godot equivalent: `Rid.get_id()`_ | ||
#[inline] | ||
pub const fn to_u64(self) -> u64 { | ||
match self { | ||
Rid::Valid(id) => id.get(), | ||
Rid::Invalid => 0, | ||
} | ||
} | ||
|
||
/// Convert this RID into a [`u64`] if it is valid. Otherwise return None. | ||
#[inline] | ||
pub const fn to_valid_u64(self) -> Option<u64> { | ||
match self { | ||
Rid::Valid(id) => Some(id.get()), | ||
Rid::Invalid => None, | ||
} | ||
} | ||
|
||
/// Returns `true` if this is a valid RID. | ||
#[inline] | ||
pub const fn is_valid(&self) -> bool { | ||
matches!(self, Rid::Valid(_)) | ||
} | ||
|
||
/// Returns `true` if this is an invalid RID. | ||
#[inline] | ||
pub const fn is_invalid(&self) -> bool { | ||
matches!(self, Rid::Invalid) | ||
} | ||
} | ||
|
||
impl GodotFfi for Rid { | ||
ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
use std::{collections::HashSet, thread}; | ||
|
||
use godot::{ | ||
engine::RenderingServer, | ||
prelude::{inner::InnerRid, Color, Rid, Vector2}, | ||
}; | ||
|
||
use crate::{itest, suppress_godot_print}; | ||
|
||
#[itest] | ||
fn rid_equiv() { | ||
let invalid: Rid = Rid::Invalid; | ||
let valid: Rid = Rid::new((10 << 32) | 20); | ||
assert!(!InnerRid::from_outer(&invalid).is_valid()); | ||
assert!(InnerRid::from_outer(&valid).is_valid()); | ||
|
||
assert_eq!(InnerRid::from_outer(&invalid).get_id(), 0); | ||
assert_eq!(InnerRid::from_outer(&valid).get_id(), (10 << 32) | 20); | ||
} | ||
|
||
#[itest] | ||
fn canvas_set_parent() { | ||
// This originally caused UB, but still testing it here in case it breaks. | ||
let mut server = RenderingServer::singleton(); | ||
let canvas = server.canvas_create(); | ||
let viewport = server.viewport_create(); | ||
|
||
suppress_godot_print(|| server.canvas_item_set_parent(viewport, canvas)); | ||
suppress_godot_print(|| server.canvas_item_set_parent(viewport, viewport)); | ||
|
||
server.free_rid(canvas); | ||
server.free_rid(viewport); | ||
} | ||
|
||
#[itest] | ||
fn multi_thread_test() { | ||
let threads = (0..10) | ||
.map(|_| { | ||
thread::spawn(|| { | ||
let mut server = RenderingServer::singleton(); | ||
(0..1000).map(|_| server.canvas_item_create()).collect() | ||
}) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
let mut rids: Vec<Rid> = vec![]; | ||
|
||
for thread in threads.into_iter() { | ||
rids.append(&mut thread.join().unwrap()); | ||
} | ||
|
||
let set = rids.iter().cloned().collect::<HashSet<_>>(); | ||
assert_eq!(set.len(), rids.len()); | ||
|
||
let mut server = RenderingServer::singleton(); | ||
|
||
for rid in rids.iter() { | ||
server.canvas_item_add_circle(*rid, Vector2::ZERO, 1.0, Color::from_rgb(1.0, 0.0, 0.0)); | ||
} | ||
|
||
for rid in rids.iter() { | ||
server.free_rid(*rid); | ||
} | ||
} | ||
|
||
/// Check that godot does not crash upon receiving various RIDs that may be edge cases. As it could do in Godot 3. | ||
#[itest] | ||
fn strange_rids() { | ||
Comment on lines
+73
to
+74
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you maybe add a quick comment what exactly is being tested here? |
||
let mut server = RenderingServer::singleton(); | ||
let mut rids: Vec<u64> = vec![ | ||
// Invalid RID. | ||
0, | ||
// Normal RID, should work without issue. | ||
1, | ||
10, | ||
// Testing the boundaries of various ints. | ||
u8::MAX as u64, | ||
u16::MAX as u64, | ||
u32::MAX as u64, | ||
u64::MAX, | ||
i8::MIN as u64, | ||
i8::MAX as u64, | ||
i16::MIN as u64, | ||
i16::MAX as u64, | ||
i32::MIN as u64, | ||
i32::MAX as u64, | ||
i64::MIN as u64, | ||
i64::MAX as u64, | ||
// Biggest RIDs possible in Godot (ignoring local indices). | ||
0xFFFFFFFF << 32, | ||
0x7FFFFFFF << 32, | ||
// Godot's servers treats RIDs as two u32s, so testing what happens round the region where | ||
// one u32 overflows into the next. | ||
u32::MAX as u64 + 1, | ||
u32::MAX as u64 + 2, | ||
u32::MAX as u64 - 1, | ||
u32::MAX as u64 - 2, | ||
// A couple random RIDs. | ||
1234567891011121314, | ||
14930753991246632225, | ||
8079365198791785081, | ||
10737267678893224303, | ||
12442588258967011829, | ||
4275912429544145425, | ||
]; | ||
// Checking every number with exactly 2 bits = 1. | ||
// An approximation of exhaustively checking every number. | ||
for i in 0..64 { | ||
for j in 0..63 { | ||
if j >= i { | ||
rids.push((1 << i) | (1 << (j + 1))) | ||
} else { | ||
rids.push((1 << i) | (1 << j)) | ||
} | ||
} | ||
} | ||
|
||
for id in rids.iter() { | ||
suppress_godot_print(|| server.canvas_item_clear(Rid::new(*id))) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Providing negated versions of accessors is not something we typically do. I don't think we should start a precedent here, as this will lead to discussions in which cases it's OK or not. Given that RIDs validity checks should ideally not be very common, it's fine if a user has to explicitly type a
!
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh fair, i think the c++ code had it so i just added it. But it does follow the precedent of
Option
which has bothis_some
andis_none
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, and it does not follow the precedent of
Vec::is_empty()
which has no such opposite. Or any other type in godot-rust, which is much more relevant here 🙂There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vec
isn't an enum though, i chose to compare it withOption
because they're both similar enums, and having anis_x
function for each enum variant is not uncommon.