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 From<[Elem; N]> for Packed Array and Optimize From<Vec<Elem>> #827

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 65 additions & 17 deletions godot-core/src/builtin/collections/packed_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use godot_ffi as sys;

use crate::builtin::*;
use crate::meta::ToGodot;
use std::{fmt, ops};
use std::{fmt, ops, ptr};
use sys::types::*;
use sys::{ffi_methods, interface_fn, GodotFfi};

Expand Down Expand Up @@ -378,6 +378,30 @@ macro_rules! impl_packed_array {
pub fn as_inner(&self) -> inner::$Inner<'_> {
inner::$Inner::from_outer(self)
}

/// Create array filled with default elements.
fn default_with_size(n: usize) -> Self {
let mut array = Self::new();
array.resize(n);
array
}

/// Drops all elements in `self` and replaces them with data from an array of values.
///
/// # Safety
///
/// * Pointer must be valid slice of data with `len` size.
/// * Pointer must not point to `self` data.
/// * Length must be equal to `self.len()`.
/// * Source data must not be dropped later.
unsafe fn move_from_slice(&mut self, src: *const $Element, len: usize) {
let ptr = self.ptr_mut(0);
debug_assert_eq!(len, self.len(), "length precondition violated");
// Drops all elements in place. Drop impl must not panic.
ptr::drop_in_place(ptr::slice_from_raw_parts_mut(ptr, len));
// Copy is okay since all elements are dropped.
ptr.copy_from_nonoverlapping(src, len);
}
}

impl_builtin_traits! {
Expand Down Expand Up @@ -414,29 +438,53 @@ macro_rules! impl_packed_array {
#[doc = concat!("Creates a `", stringify!($PackedArray), "` from the given slice.")]
impl From<&[$Element]> for $PackedArray {
fn from(slice: &[$Element]) -> Self {
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 {
// `GString` does not implement `Copy` so we have to call `.clone()`
// here.
*ptr.offset(to_isize(i)) = element.clone();
}
if slice.is_empty() {
return Self::new();
}
let mut array = Self::default_with_size(slice.len());

// SAFETY: The array contains exactly `len` elements, stored contiguously in memory.
let dst = unsafe { std::slice::from_raw_parts_mut(array.ptr_mut(0), slice.len()) };
dst.clone_from_slice(slice);
array
}
}

#[doc = concat!("Creates a `", stringify!($PackedArray), "` from the given Rust array.")]
impl<const N: usize> From<[$Element; N]> for $PackedArray {
fn from(arr: [$Element; N]) -> Self {
if N == 0 {
return Self::new();
}
let mut packed_array = Self::default_with_size(N);

// Not using forget() so if move_from_slice somehow panics then there is no double-free.
let arr = std::mem::ManuallyDrop::new(arr);

// SAFETY: The packed array contains exactly N elements and the source array will be forgotten.
unsafe {
packed_array.move_from_slice(arr.as_ptr(), N);
}
packed_array
}
}

#[doc = concat!("Creates a `", stringify!($PackedArray), "` from the given Rust vec.")]
impl From<Vec<$Element>> for $PackedArray {
fn from(vec: Vec<$Element>) -> Self{
vec.into_iter().collect()
fn from(mut vec: Vec<$Element>) -> Self {
if vec.is_empty() {
return Self::new();
}
let len = vec.len();
let mut array = Self::default_with_size(len);

// SAFETY: The packed array and vector contain exactly `len` elements.
// The vector is forcibly set to empty, so its contents are forgotten.
unsafe {
vec.set_len(0);
array.move_from_slice(vec.as_ptr(), len);
}
array
}
}

Expand Down
53 changes: 52 additions & 1 deletion itest/rust/src/builtin_tests/containers/packed_array_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
*/

use crate::framework::{expect_panic, itest};
use godot::builtin::{PackedByteArray, PackedFloat32Array, PackedInt32Array, PackedStringArray};
use godot::builtin::{
Color, PackedByteArray, PackedColorArray, PackedFloat32Array, PackedInt32Array,
PackedStringArray,
};

#[itest]
fn packed_array_default() {
Expand Down Expand Up @@ -45,6 +48,54 @@ fn packed_array_from_vec_i32() {
assert_eq!(int32_array[1], 2);
}

#[itest]
fn packed_array_from_vec_color() {
const SRC: [Color; 3] = [
Color::from_rgb(1., 0., 0.),
Color::from_rgb(0., 1., 0.),
Color::from_rgb(0., 0., 1.),
];
let color_array = PackedColorArray::from(Vec::from(SRC));

assert_eq!(color_array.len(), SRC.len());
for (i, c) in SRC.into_iter().enumerate() {
assert_eq!(color_array[i], c, "value mismatch at index {}", i);
}
}

#[itest]
fn packed_array_from_array_str() {
let string_array = PackedStringArray::from(["hello".into(), "world".into()]);

assert_eq!(string_array.len(), 2);
assert_eq!(string_array[0], "hello".into());
assert_eq!(string_array[1], "world".into());
}

#[itest]
fn packed_array_from_array_i32() {
let int32_array = PackedInt32Array::from([1, 2]);

assert_eq!(int32_array.len(), 2);
assert_eq!(int32_array[0], 1);
assert_eq!(int32_array[1], 2);
}

#[itest]
fn packed_array_from_array_color() {
const SRC: [Color; 3] = [
Color::from_rgb(1., 0., 0.),
Color::from_rgb(0., 1., 0.),
Color::from_rgb(0., 0., 1.),
];
let color_array = PackedColorArray::from(SRC);

assert_eq!(color_array.len(), SRC.len());
for (i, c) in SRC.into_iter().enumerate() {
assert_eq!(color_array[i], c, "value mismatch at index {}", i);
}
}

#[itest]
fn packed_array_to_vec() {
let array = PackedByteArray::new();
Expand Down
Loading