Struct SimpleArray

Source
pub struct SimpleArray<T, A: Allocator = TESGlobalAlloc> { /* private fields */ }
Expand description

Array whose first pointer is only a pointer to the length.

Memory layout:

┌────────────┬────────────┬────────────┬──────┬────────────┐
│ len: usize │ T[0]       │ T[1]       │ ...  │ T[N - 1]   │
└────────────┴────────────┴────────────┴──────┴────────────┘
                  ↑
  data: Unique<T> ┘

§Example

use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::new();
assert_eq!(array.len(), 0);

Implementations§

Source§

impl<T> SimpleArray<T>

Source

pub const fn new() -> Self

Creates a new empty SimpleArray.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::new();
assert_eq!(array.len(), 0);
Source

pub fn with_capacity(count: usize) -> Self

Creates a new SimpleArray with the specified capacity.

Uninitialized memory leads to undefined operation, so 0 is entered.

§Panics

This function may panic if the allocation fails.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::with_capacity(5);
assert_eq!(array.len(), 5);
Source§

impl<T, A> SimpleArray<T, A>
where A: Allocator,

Source

pub const fn new_in(data: Option<Unique<T>>, alloc: A) -> Self

Creates a new empty SimpleArray with Allocator.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
use stdx::alloc::Global; // Since TESAllocator is not available for CI, use Rust's.

let array: SimpleArray<i32, Global> = SimpleArray::new_in(None, Global);
assert_eq!(array.len(), 0);
Source

pub const fn len(&self) -> usize

Returns the length of the array (the number of elements currently stored).

Equivalent C++ size()

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::with_capacity(5);
assert_eq!(array.len(), 5);
Source

pub const fn is_empty(&self) -> bool

Checks if the array is empty.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::new();
assert!(array.is_empty());
Source

pub fn clear(&mut self)

Clears the array by deallocating its memory.

§Safety

This function performs unsafe operations to deallocate the array memory.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array: SimpleArray<i32> = SimpleArray::with_capacity(3);
array.clear();
assert!(array.is_empty());
Source

pub fn resize(&mut self, count: usize)

Resizes the array to the specified count.

§Panics

This function may panic if the allocation fails.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::<u32>::with_capacity(3);
array.resize(5);
assert_eq!(array.len(), 5);
Source

pub fn get(&self, index: usize) -> Option<&T>

Gets a reference to the element at the given index.

Return None if the index is out of bounds.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::with_capacity(3);
array[0] = 1;
assert_eq!(array.get(0), Some(&1));
assert_eq!(array.get(5), None);
Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Gets a mutable reference to the element at the given index.

Return None if the index is out of bounds.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::<i32>::with_capacity(3);
if let Some(value) = array.get_mut(1) {
    *value = 42;
};
assert_eq!(array[1], 42);
assert_eq!(array.get_mut(5), None);
Source

pub const fn as_ptr(&self) -> *mut T

Returns a raw pointer to the data.

Equivalent C++ data()

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array: SimpleArray<i32> = SimpleArray::new();
let data = array.as_ptr();
Source

pub const fn as_mut_slice(&mut self) -> &mut [T]

Returns a mutable slice of the array.

§Safety

This function assumes that the array is properly initialized.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::with_capacity(4);
let slice = array.as_mut_slice();
slice.copy_from_slice(&[1, 2, 3, 4]);
Source

pub const fn as_slice(&self) -> &[T]

Returns a slice of the array.

§Safety

This function assumes that the array is properly initialized.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let array = SimpleArray::<i32>::with_capacity(4);
let slice = array.as_slice();
assert_eq!(slice, &[0, 0, 0, 0]);
Source

pub const fn iter(&self) -> SimpleArrayIterator<'_, T, A>

Creates an iterator that borrows the elements of the SimpleArray.

This method returns an iterator over references to the elements of the array. The iterator allows you to traverse the array without consuming it, meaning the array remains usable after the iteration.

§Example
use commonlibsse_ng::re::MemoryManager::SimpleArray::SimpleArray;
let mut array = SimpleArray::with_capacity(3);
let slice = array.as_mut_slice();
slice[0] = 10;
slice[1] = 20;
slice[2] = 30;

let sum: i32 = array.iter().sum();
assert_eq!(sum, 60);

Trait Implementations§

Source§

impl<T> Default for SimpleArray<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T, A: Allocator> Drop for SimpleArray<T, A>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Hash, A: Allocator> Hash for SimpleArray<T, A>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T, A: Allocator> Index<usize> for SimpleArray<T, A>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T, A: Allocator> IndexMut<usize> for SimpleArray<T, A>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T, A: Allocator> IntoIterator for &'a SimpleArray<T, A>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = SimpleArrayIterator<'a, T, A>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T, A: Allocator> IntoIterator for SimpleArray<T, A>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = SimpleArrayIntoIterator<T, A>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<T: Ord, A: Allocator> Ord for SimpleArray<T, A>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq, A: Allocator> PartialEq for SimpleArray<T, A>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd, A: Allocator> PartialOrd for SimpleArray<T, A>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq, A: Allocator> Eq for SimpleArray<T, A>

Auto Trait Implementations§

§

impl<T, A> Freeze for SimpleArray<T, A>
where A: Freeze,

§

impl<T, A> RefUnwindSafe for SimpleArray<T, A>

§

impl<T, A> Send for SimpleArray<T, A>
where A: Send, T: Send,

§

impl<T, A> Sync for SimpleArray<T, A>
where A: Sync, T: Sync,

§

impl<T, A> Unpin for SimpleArray<T, A>
where A: Unpin, T: Unpin,

§

impl<T, A> UnwindSafe for SimpleArray<T, A>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more