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

Add as_slice and as_mut_slice for Packed*Array #137

Closed
wants to merge 11 commits into from
32 changes: 32 additions & 0 deletions godot-core/src/builtin/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,38 @@ macro_rules! impl_packed_array {
self.as_inner().sort();
}

/// Extracts a slice of the entire array.
pub fn as_slice<'a>(&'a self) -> &'a [$Element] {
let len = self.len();

if len == 0 {
return &[];
}
indubitablement2 marked this conversation as resolved.
Show resolved Hide resolved

let data = self.ptr(0);
// SAFETY: Data is valid for len elements as it was allocated by the array.
// Data is not aliased as it is CoW.
unsafe {
indubitablement2 marked this conversation as resolved.
Show resolved Hide resolved
std::slice::from_raw_parts(data, len)
indubitablement2 marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Extracts a mutable slice of the entire array.
pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [$Element] {
let len = self.len();

if len == 0 {
return &mut [];
}

let data = self.ptr_mut(0);
// SAFETY: Data is valid for len elements as it was allocated by the array.
// Data is not aliased as it is CoW.
unsafe {
std::slice::from_raw_parts_mut(data, len)
}
}

/// Asserts that the given index refers to an existing element.
///
/// # Panics
Expand Down
40 changes: 40 additions & 0 deletions itest/rust/src/packed_array_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub fn run() -> bool {
ok &= packed_array_extend();
ok &= packed_array_reverse();
ok &= packed_array_sort();
ok &= packed_array_as_slice();
ok &= packed_array_is_mut_unique();
ok
}

Expand Down Expand Up @@ -200,3 +202,41 @@ fn packed_array_reverse() {
array.reverse();
assert_eq!(array.to_vec(), vec![2, 1]);
}

#[itest]
fn packed_array_as_slice() {
let vec = vec![1, 2];
let array = PackedByteArray::from(vec.as_slice());
assert_eq!(array.as_slice(), vec.as_slice());

assert_eq!(
PackedByteArray::new().as_slice(),
&[],
"array is empty, but still a valid slice"
);
}

#[itest]
fn packed_array_is_mut_unique() {
let array1 = PackedByteArray::from(&[1, 2]);
let mut array2 = array1.clone();

assert_eq!(
array1.as_slice().as_ptr(),
array2.as_slice().as_ptr(),
Comment on lines +208 to +209
Copy link
Member

Choose a reason for hiding this comment

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

Here you could use is_buffer_shared() (see other comment).

Same for similar occurrences.

"arrays should share the same buffer"
);

// array2 should become a copy of array1 since rc > 1.
Copy link
Member

Choose a reason for hiding this comment

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

The "RC > 1" is somewhat irrelevant, now that Godot 4 is released.

I would rather mention that as_mut_slice triggers a copy-on-write.

array2.as_mut_slice();
assert_ne!(
array1.as_slice().as_ptr(),
array2.as_slice().as_ptr(),
"arrays should not share the same buffer after a mutable access"
);
assert_eq!(
array1.as_slice(),
array2.as_slice(),
"array2 should be a copy of array1"
);
}