Skip to content
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

#310 - Array builtin completeness #561

Merged
merged 1 commit into from
Jan 13, 2024
Merged

Conversation

0awful
Copy link
Contributor

@0awful 0awful commented Jan 10, 2024

This will make Array in #310 complete.

There would be additional work for Array and Dictionary if we are interested in adding read_only semantics.

@0awful 0awful marked this pull request as ready for review January 10, 2024 07:02
@GodotRust
Copy link

API docs are being generated and will be shortly available at: https://godot-rust.github.io/docs/gdext/pr-561

@Bromeon Bromeon added feature Adds functionality to the library c: core Core components labels Jan 10, 2024
Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Maybe we can rename the two binary_search* variants to bsearch* (to stay with Godot terms), because Rust's own slice::binary_search() has a different signature.

For comments, please use the available width of 120-145 chars, even if some existing code doesn't follow that.

Great tests! 👍

Comment on lines 147 to 149
/// Note: The sorting algorithm used is not
/// [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability). This means that values
/// considered equal may have their order changed when using `sort_unstable`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the 120-145 available width:

Suggested change
/// Note: The sorting algorithm used is not
/// [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability). This means that values
/// considered equal may have their order changed when using `sort_unstable`.
/// Note: the sorting algorithm used is not [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability).
/// This means that values considered equal may have their order changed when using `sort_unstable`.

let res = i32::from_variant(args[0]) > i32::from_variant(args[1]);
Ok(Variant::from(res))
});
assert_eq!(a.clone().binary_search_custom(&1, func.clone()), 3);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the clone here? Maybe express the intention with a short comment before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a.clone() was a carryover from the array implementation. Didn't catch that it wasn't required anymore because .binary_search_custom() is immutable.

func.clone() is probably easier to understand. I can add a comment there if you think its worthwhile to clarify, but the compiler will get mad if its gone. Use after move and all that.

@Bromeon
Copy link
Member

Bromeon commented Jan 10, 2024

Also, code that relies on Callable::from_fn must be guarded with #[cfg(since_api = "4.2")].

You can apply this to both #[itest] functions -- it should be OK if this is not tested for older Godot versions.

@0awful
Copy link
Contributor Author

0awful commented Jan 10, 2024

Maybe we can rename the two binary_search* variants to bsearch* (to stay with Godot terms), because Rust's own slice::binary_search() has a different signature.

What about the case of Array.sort_unstable() should we keep that or move to Array.sort() for that?

@Bromeon
Copy link
Member

Bromeon commented Jan 12, 2024

What about the case of Array.sort_unstable() should we keep that or move to Array.sort() for that?

Keep it because "sort" in Rust also has a meaning: stable sort.

Copy link
Member

@Bromeon Bromeon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot!

Comment on lines +508 to +514
/// Finds the index of an existing value in a sorted array using binary search.
/// Equivalent of `bsearch` in GDScript.
///
/// If the value is not present in the array, returns the insertion index that would maintain
/// sorting order.
/// If the value is not present in the array, returns the insertion index that
/// would maintain sorting order.
///
/// Calling `binary_search` on an unsorted array results in unspecified behavior.
pub fn binary_search(&self, value: &T) -> usize {
/// Calling `bsearch` on an unsorted array results in unspecified behavior.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we try to keep 120-145 characters, see here.

It's not important enough to block this PR, but maybe think of it next time 🙂

@Bromeon Bromeon added this pull request to the merge queue Jan 13, 2024
Merged via the queue into godot-rust:master with commit 0aa07d9 Jan 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: core Core components feature Adds functionality to the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants