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
28 changes: 28 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,34 @@ 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);
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);
unsafe {
std::slice::from_raw_parts_mut(data, len)
}
}

/// Asserts that the given index refers to an existing element.
///
/// # Panics
Expand Down