Skip to content

Commit

Permalink
Memory fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bromeon committed Jul 21, 2024
1 parent a24dd8a commit 4c57a36
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
23 changes: 14 additions & 9 deletions godot-core/src/builtin/collections/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,9 +741,19 @@ impl<T: ArrayElement> Array<T> {
}

/// # Safety
/// Returned type may be inaccurate and must be checked separately.
/// Returned type may be inaccurate and must be type-checked after the call.
// Visibility: shared with OutArray.
pub(super) unsafe fn unchecked_clone(&self) -> Array<T> {
pub(super) unsafe fn default_unchecked() -> Array<T> {
Self::new_with_uninit(|self_ptr| {
let ctor = sys::builtin_fn!(array_construct_default);
ctor(self_ptr, std::ptr::null_mut())
})
}

/// # Safety
/// Returned type may be inaccurate and must be type-checked after the call.
// Visibility: shared with OutArray.
pub(super) unsafe fn clone_unchecked(&self) -> Array<T> {
Self::new_with_uninit(|self_ptr| {
let ctor = sys::builtin_fn!(array_construct_copy);
let args = [self.sys()];
Expand Down Expand Up @@ -936,7 +946,7 @@ impl<T: ArrayElement + fmt::Display> fmt::Display for Array<T> {
impl<T: ArrayElement> Clone for Array<T> {
fn clone(&self) -> Self {
// SAFETY: `self` is a valid array, since we have a reference that keeps it alive. Type is checked below.
let array = unsafe { self.unchecked_clone() };
let array = unsafe { self.clone_unchecked() };

array
.with_checked_type()
Expand Down Expand Up @@ -996,12 +1006,7 @@ impl Export for Array<Variant> {
impl<T: ArrayElement> Default for Array<T> {
#[inline]
fn default() -> Self {
let mut array = unsafe {
Self::new_with_uninit(|self_ptr| {
let ctor = sys::builtin_fn!(array_construct_default);
ctor(self_ptr, std::ptr::null_mut())
})
};
let mut array = unsafe { Self::default_unchecked() };

// SAFETY: We just created this array, and haven't called `init_inner_type` before.
unsafe { array.init_inner_type() };
Expand Down
9 changes: 5 additions & 4 deletions godot-core/src/builtin/collections/out_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ impl OutArray {
}

pub(crate) fn new_untyped() -> Self {
Self {
inner: VariantArray::new(),
}
// SAFETY: we explicitly create an untyped array, so we don't need to set a type.
let inner = unsafe { VariantArray::default_unchecked() };

Self { inner }
}

/// ⚠️ Returns the value at the specified index.
Expand Down Expand Up @@ -448,7 +449,7 @@ unsafe impl GodotFfi for OutArray {
impl Clone for OutArray {
fn clone(&self) -> Self {
// SAFETY: we don't want to check that static type (Variant) matches dynamic type (anything), because all types are valid in OutArray.
let inner = unsafe { VariantArray::unchecked_clone(&self.inner) };
let inner = unsafe { VariantArray::clone_unchecked(&self.inner) };

Self { inner }
}
Expand Down

0 comments on commit 4c57a36

Please sign in to comment.