Skip to main content

Array

Struct Array 

Source
pub struct Array<X: Extension = Never> { /* private fields */ }
Expand description

A PHP array: an ordered map from ArrayKey to Value.

Packed (a Vec, no stored keys) while it is a list 0, 1, 2, ... whose next free index is its length; an ordered hash otherwise. The switch is invisible. Semantics follow the engine: assigning an existing key keeps its position, a removed key assigned again goes to the end, and push uses the next free integer key (nNextFreeElement): one past the largest integer key inserted, except that the engine’s packed arrays (integer keys in ascending order, possibly with holes) set it to one past the key they append, which lowers it after trailing elements were removed ([0, 1, 2], unset 1 and 2, $a[1] = x, then $a[] uses 2). That state is tracked as the engine does (nNumUsed, nTableSize), with the table size an array of n elements is created with (max(8, 2^k >= n)).

Copy-on-write, like the engine’s arrays: a clone shares the elements (an atomic reference count, so values can cross threads) and the first mutation of a shared array copies it. Mutating a clone never changes the original. A new empty array allocates nothing.

Implementations§

Source§

impl<X: Extension> Array<X>

Source

pub const fn new() -> Self

An empty array.

Source

pub fn with_capacity(n: usize) -> Self

An empty array with room for n list elements.

Source

pub fn from_list(v: Vec<Value<X>>) -> Self

[$v0, $v1, ...].

Source

pub fn from_entries( entries: impl IntoIterator<Item = (ArrayKey, Value<X>)>, next: Option<i64>, ) -> Self

An array with these entries (in order, keys as given) and this next free index.

Source

pub fn id(&self) -> Option<usize>

An identity for the elements: the same for clones that share them and never reused while one of them is alive (a write to a shared array copies it first). None for a new empty array.

Source

pub fn reserve(&mut self, additional: usize)

Reserves room for additional more elements (nothing observable changes).

Source

pub fn is_list(&self) -> bool

array_is_list(): keys are exactly 0, 1, 2, ... in order.

Source

pub fn len(&self) -> usize

The number of elements.

Source

pub fn is_empty(&self) -> bool

Whether the array has no elements.

Source

pub fn next_index(&self) -> Option<i64>

The key push would use, as PHP tracks it (None: no integer key yet, so 0).

Source

pub fn get(&self, k: &ArrayKey) -> Option<&Value<X>>

The value at k, if any (the key as given, not normalised).

Source

pub fn get_ref(&self, k: KeyRef<'_>) -> Option<&Value<X>>

The value at a borrowed key, if any (the key as given, not normalised).

Source

pub fn get_str(&self, k: &str) -> Option<&Value<X>>

$a["..."]: the key is normalised ("7" finds 7).

Source

pub fn get_mut(&mut self, k: &ArrayKey) -> Option<&mut Value<X>>

The value at k, mutably, if any (a shared array is copied first).

Source

pub fn contains_key(&self, k: &ArrayKey) -> bool

Whether k is set.

Source

pub fn set(&mut self, k: ArrayKey, v: Value<X>) -> Option<Value<X>>

$a[$k] = $v (the key as given, see ArrayKey::normalize): an existing key keeps its position. Returns the previous value.

Source

pub fn push(&mut self, v: Value<X>) -> Result<(), ArrayFull>

$a[] = $v.

Source

pub fn remove(&mut self, k: &ArrayKey) -> Option<Value<X>>

unset($a[$k]): the key, if set again, goes to the end.

Source

pub fn map_values( &self, f: impl FnMut(KeyRef<'_>, &Value<X>) -> Value<X>, ) -> Array<X>

A new array with the same keys, in the same order, and the value f makes of each entry (array_map() with one array, which keeps the keys): the same array in every other respect, next free index included. No key is copied into a new table or looked up again.

Source

pub fn iter(&self) -> Iter<'_, X> ⓘ

The entries in order, with borrowed keys.

Source

pub fn values( &self, ) -> impl ExactSizeIterator<Item = &Value<X>> + DoubleEndedIterator

The values in order.

Source

pub fn keys(&self) -> impl ExactSizeIterator<Item = KeyRef<'_>>

The keys in order.

Source

pub fn retain(&mut self, f: impl FnMut(KeyRef<'_>, &Value<X>) -> bool)

Keeps the entries f accepts (in order); the next free index is unchanged.

Trait Implementations§

Source§

impl<X: Extension> Clone for Array<X>

Source§

fn clone(&self) -> Self

Shares the elements (copied on the first mutation of either side).

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<X: Extension> Debug for Array<X>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Writes the entries as a map.

Source§

impl<X: Extension> Default for Array<X>

Source§

fn default() -> Self

An empty array.

Source§

impl<X: Extension> Drop for Array<X>

Source§

fn drop(&mut self)

Frees the elements without recursion, so arrays nested thousands of levels deep (which the decoders accept) do not exhaust the stack.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<X: Extension> From<Array<X>> for Value<X>

Source§

fn from(a: Array<X>) -> Self

A PHP array.

Source§

impl<X: Extension> FromIterator<(ArrayKey, Value<X>)> for Array<X>

$a[$k] = $v for each pair.

Source§

fn from_iter<T: IntoIterator<Item = (ArrayKey, Value<X>)>>(iter: T) -> Self

An array with each pair assigned in order, as $a[$k] = $v.

Built unshared and presized: a list for an integer first key, a hash for a string one (what assigning to an empty array leads to).

Source§

impl<X: Extension> FromIterator<Value<X>> for Array<X>

A list.

Source§

fn from_iter<T: IntoIterator<Item = Value<X>>>(iter: T) -> Self

A list of the values.

Source§

impl<X: Extension> IntoIterator for Array<X>

Source§

fn into_iter(self) -> Self::IntoIter

The entries in order, with owned keys.

Source§

type Item = (ArrayKey, Value<X>)

The type of the elements being iterated over.
Source§

type IntoIter = Box<dyn Iterator<Item = (ArrayKey, Value<X>)>>

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

impl<'a, X: Extension> IntoIterator for &'a Array<X>

Source§

fn into_iter(self) -> Self::IntoIter

The entries in order, with borrowed keys.

Source§

type Item = (KeyRef<'a>, &'a Value<X>)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, X>

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

impl<X: Extension> PartialEq for Array<X>

The same entries in the same order (structural, not PHP’s ==).

Source§

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

Whether both have the same entries in the same order.

1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<X> Forget for Array<X>

§

impl<X> Freeze for Array<X>
where Option<Arc<Repr<X>>>: Freeze,

§

impl<X> Move for Array<X>

§

impl<X> RefUnwindSafe for Array<X>
where Option<Arc<Repr<X>>>: RefUnwindSafe,

§

impl<X> Send for Array<X>
where Option<Arc<Repr<X>>>: Send,

§

impl<X> Sync for Array<X>
where Option<Arc<Repr<X>>>: Sync,

§

impl<X> Unpin for Array<X>
where Option<Arc<Repr<X>>>: Unpin,

§

impl<X> UnsafeUnpin for Array<X>
where Option<Arc<Repr<X>>>: UnsafeUnpin,

§

impl<X> UnwindSafe for Array<X>
where Option<Arc<Repr<X>>>: UnwindSafe,

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.