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

Implement packed arrays non-generically #91

Merged
merged 1 commit into from Jan 28, 2023

Conversation

ttencate
Copy link
Contributor

The ideal is to have a struct PackedArray<T>, where T implements some PackedArrayElement trait. But since the autogenerated InnerPacked*Array types are entirely independent at the moment and don't implement a common trait, that would require indirecting every method implementation through PackedArrayElement, which results in mentioning each method call three times: once in impl PackedArray<T>, once in PackedArrayElement and once in impl PackedArrayElement for T.

So, for now, we use a big (but quite straightforward) macro to define each Packed*Array as a separate type.

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! This is a great first step for Packed*Array support!

bors try

Comment on lines +346 to +361
let mut array = Self::new();
let len = slice.len();
if len == 0 {
return array;
}
array.resize(len);
let ptr = array.ptr_mut(0);
for (i, element) in slice.iter().enumerate() {
// SAFETY: The array contains exactly `len` elements, stored contiguously in memory.
unsafe {
// `GodotString` does not implement `Copy` so we have to call `.clone()`
// here.
*ptr.offset(to_isize(i)) = element.clone();
}
}
array
Copy link
Member

Choose a reason for hiding this comment

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

Could this not be moved to FromIterator and then reused from here?

There, instead of slice.len() we could use size_hint(), I'm almost certain that provides an accurate number for slices.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does for slices, but not for iterators in general. So we would need a lot more complexity to deal with iterators that don't have a size hint, whose size hint is incorrect, or even whose size hint changes during iteration. Let's keep it simple and stupid for now.

self.check_bounds(index);
// SAFETY: We just checked that the index is not out of bounds.
let ptr = unsafe {
(interface_fn!($operator_index_const))(self.sys(), to_i64(index)) as *const $Element
Copy link
Member

Choose a reason for hiding this comment

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

Maybe also here, split statements into FFI call and pointer cast.
Explicit type annotations for the pointer is currently not possible, but can easily be added once we have generics.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type is either $Element or TagType or TagString. Had to add another macro argument for this, and it'll couple the implementation more tightly to the generated code. Which, I suppose, is exactly the idea :)

Comment on lines 274 to 279
assert!(
index < len,
"Array index {} is out of bounds: length is {}",
index,
len
);
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion:

                assert!(
                    index < len,
                    "Array index {index} is out of bounds: length is {len}"
                );

Does clippy not complain here? I just fixed a ton of similar issues 😀

A few other places like this...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not in CI at least... maybe it needs a Rust version upgrade?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm on 1.66.0 stable and not getting complaints when I run cargo clippy in the project root 🤷 I'll fix these ones but we might need a more structural solution here.

Copy link
Member

Choose a reason for hiding this comment

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

I encountered them e.g. in this CI run, but it was format! in that case.

The Clippy lint doc does not explicitly mention assert!, but clicking on Related issues shows some issues mentioning assert!. Hmm... 🤔

bors bot added a commit that referenced this pull request Jan 27, 2023
@Bromeon Bromeon added feature Adds functionality to the library c: core Core components labels Jan 27, 2023
@bors
Copy link
Contributor

bors bot commented Jan 27, 2023

try

Build succeeded:

@Bromeon
Copy link
Member

Bromeon commented Jan 28, 2023

Small conflicts introduced by just-merged #90, other than that it looks good to me!

The ideal is to have a `struct PackedArray<T>`, where `T` implements
some `PackedArrayElement` trait. But since the autogenerated
`InnerPacked*Array` types are entirely independent at the moment and
don't implement a common trait, that would require indirecting every
method implementation through `PackedArrayElement`, which results in
mentioning each method call three times: once in `impl PackedArray<T>`,
once in `PackedArrayElement` and once in `impl PackedArrayElement for
T`.

So, for now, we use a big (but quite straightforward) macro to define
each `Packed*Array` as a separate type.
@Bromeon
Copy link
Member

Bromeon commented Jan 28, 2023

bors r+

@bors
Copy link
Contributor

bors bot commented Jan 28, 2023

Build succeeded:

@bors bors bot merged commit 154b87f into godot-rust:master Jan 28, 2023
@ttencate ttencate deleted the feature/packed_array branch January 28, 2023 14:11
@ttencate ttencate restored the feature/packed_array branch January 28, 2023 14:11
@ttencate ttencate deleted the feature/packed_array branch January 28, 2023 14:11
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.

None yet

2 participants