#[repr(C)]pub struct BSTArray<T, A = TESGlobalAlloc>where
A: SelflessAllocator,{ /* private fields */ }
Expand description
A binary-compatible, growable array.
BSTArray<T, A>
is a contiguous, heap-allocated collection of elements of type T
with memory layout designed to match Havok’s native BSTArray
. This type is similar
to Vec<T>
in usage but may differ in layout due to alignment, padding, or
platform-specific serialization constraints.
This array uses a custom allocator A
(which implements [Allocator
]) to control
memory allocation. In contexts where allocation is fixed (e.g., read-only arrays or
statically-sized blocks), capacity may be fixed or omitted entirely.
Internally, the array stores:
- a pointer to the data buffer
- the number of elements (
len
) - the capacity of the allocation (
cap
)
§Features
- Compatible with Havok’s binary layout for
BSTArray<T>
- Supports growable or fixed-capacity semantics
- Custom allocator support (
A: Allocator
) - Methods similar to
Vec<T>
§Panics
Most methods panic under the following conditions:
- Indexing out of bounds (
array[index]
) - Reserving more capacity than is possible
- Pushing to a full fixed-capacity array (if applicable)
§Example
let mut array = BSTArray::<i32>::new();
array.push(1);
array.push(2);
assert_eq!(array.len(), 2);
assert_eq!(array[1], 2);
§See also
Vec<T>
- [
Box<[T]>
]
Implementations§
Source§impl<T, A> BSTArray<T, A>where
A: SelflessAllocator,
impl<T, A> BSTArray<T, A>where
A: SelflessAllocator,
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates a new, empty BSTArray<T, A>
with the specified allocator.
The array will not allocate until elements are pushed.
§Example
let array = BSTArray::<i32>::new();
assert!(array.is_empty());
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new, empty BSTArray<T, A>
with the capacity.
§Example
let array = BSTArray::<i32>::with_capacity(5);
assert_eq!(array.capacity(), 5);
Sourcepub const fn len(&self) -> usize
pub const fn len(&self) -> usize
Returns the number of elements in the array.
This is also referred to as the array’s “length”.
§Example
let array = BSTArray::<i32>::new();
assert_eq!(array.len(), 0);
Sourcepub const fn is_empty(&self) -> bool
pub const fn is_empty(&self) -> bool
Returns true
if the array contains no elements.
§Example
let array = BSTArray::<i32>::new();
assert!(array.is_empty());
Sourcepub const fn capacity(&self) -> usize
pub const fn capacity(&self) -> usize
Returns the total number of elements the array can hold without reallocating.
This is the allocated capacity, which may be larger than the current length.
§Example
let mut array = BSTArray::<i32>::with_capacity(10);
assert!(array.capacity() >= 10);
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks the capacity of the array as much as possible.
It will drop any excess capacity not used by the current elements.
§Examples
let mut array = BSTArray::<i32>::with_capacity(10);
array.push(1);
assert_eq!(array.len(), 1);
array.shrink_to_fit();
assert!(array.capacity() >= array.len());
Sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the last element from the array and returns it, or None
if it’s empty.
§Example
let mut array = BSTArray::<i32>::new();
array.push(1);
assert_eq!(array.pop(), Some(1));
assert_eq!(array.pop(), None);
Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns a reference to the element at the given index, if it exists.
§Example
let mut array = BSTArray::<i32>::new();
array.push(42);
assert_eq!(array.get(0), Some(&42));
assert_eq!(array.get(1), None);
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the element at the given index, if it exists.
§Example
let mut array = BSTArray::<i32>::new();
array.push(10);
if let Some(x) = array.get_mut(0) {
*x += 1;
}
assert_eq!(array[0], 11);
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the array, removing all elements but preserving the capacity.
§Examples
let mut array = BSTArray::<i32>::with_capacity(10);
array.push(1);
array.push(2);
assert_eq!(array.len(), 2);
array.clear();
assert_eq!(array.len(), 0);
assert_eq!(array.capacity(), 10); // Capacity is preserved
Sourcepub const fn as_non_null_ptr(&mut self) -> Option<NonNull<T>>
pub const fn as_non_null_ptr(&mut self) -> Option<NonNull<T>>
Returns a non null pointer of the array’s buffer.
Sourcepub const fn as_const_non_null_ptr(&self) -> Option<ConstNonNull<T>>
pub const fn as_const_non_null_ptr(&self) -> Option<ConstNonNull<T>>
Returns a non null pointer of the array’s buffer.
Sourcepub fn contains(&self, value: &T) -> boolwhere
T: PartialEq,
pub fn contains(&self, value: &T) -> boolwhere
T: PartialEq,
Checks if the array contains the given element.
Returns true
if the element is present in the array, and false
otherwise.
§Examples
let mut array = BSTArray::<i32>::with_capacity(10);
array.push(1);
array.push(2);
assert!(array.contains(&1));
assert!(!array.contains(&3));
Sourcepub fn retain<F>(&mut self, f: F)
pub fn retain<F>(&mut self, f: F)
Retains only the elements that satisfy the predicate.
This method takes a closure that accepts an element of the array and returns a boolean.
Elements for which the closure returns true
will be kept, while elements for which
it returns false
will be removed.
§Examples
let mut array = BSTArray::<i32>::with_capacity(10);
array.push(1);
array.push(2);
array.push(3);
array.retain(|&x| x > 1);
assert_eq!(array.len(), 2);
assert!(array.contains(&2));
assert!(array.contains(&3));
§Panics
array ptr is null
Sourcepub fn resize(&mut self, new_size: usize, value: T)where
T: Clone,
pub fn resize(&mut self, new_size: usize, value: T)where
T: Clone,
Resizes the array to the specified length.
If the list is shorter, it will be extended with cloning the value given as the argument.
§Examples
let mut array = BSTArray::<i32>::with_capacity(10);
array.push(1);
array.push(2);
array.resize(5, 0);
assert_eq!(array.len(), 5);
assert_eq!(array[3], 0);
Sourcepub fn drain<R>(&mut self, range: R) -> BSTDrain<'_, T, A>where
R: RangeBounds<usize>,
pub fn drain<R>(&mut self, range: R) -> BSTDrain<'_, T, A>where
R: RangeBounds<usize>,
Removes a range of elements from the array, returning them as a vector.
This method removes the elements within the specified range and returns them as
a Vec<T>
. The range must be within the bounds of the array.
§Examples
let mut array = BSTArray::<i32>::with_capacity(10);
array.push(1);
array.push(2);
array.push(3);
array.push(4);
array.push(5);
let drained = array.drain(0..2);
assert_eq!(drained.collect::<Vec<_>>(), vec![1, 2]);
assert_eq!(array.len(), 3);
assert_eq!(array[0], 3);
assert_eq!(array[1], 4);
assert_eq!(array[2], 5);
§Panics
Panics if the range is out of bounds.
Sourcepub const fn as_mut_slice(&mut self) -> &mut [T]
pub const fn as_mut_slice(&mut self) -> &mut [T]
Returns a mutable slice of all elements in the array.
Sourcepub const fn iter(&self) -> BSTArrayIterator<'_, T, A>
pub const fn iter(&self) -> BSTArrayIterator<'_, T, A>
Returns an iterator over the elements of the array.
This iterator yields references to the elements in the array.
§Examples
let mut array = BSTArray::<i32>::with_capacity(10);
array.push(1);
array.push(2);
let sum: i32 = array.iter().sum();
assert_eq!(sum, 3);
Trait Implementations§
Source§impl<T, A> Drop for BSTArray<T, A>where
A: SelflessAllocator,
impl<T, A> Drop for BSTArray<T, A>where
A: SelflessAllocator,
Source§impl<T, A> Extend<T> for BSTArray<T, A>where
A: SelflessAllocator,
impl<T, A> Extend<T> for BSTArray<T, A>where
A: SelflessAllocator,
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)