Struct ext_php_rs::ffi::_zend_array

source ·
#[repr(C)]
pub struct _zend_array { pub gc: zend_refcounted_h, pub u: _zend_array__bindgen_ty_1, pub nTableMask: u32, pub __bindgen_anon_1: _zend_array__bindgen_ty_2, pub nNumUsed: u32, pub nNumOfElements: u32, pub nTableSize: u32, pub nInternalPointer: u32, pub nNextFreeElement: zend_long, pub pDestructor: dtor_func_t, }

Fields§

§gc: zend_refcounted_h§u: _zend_array__bindgen_ty_1§nTableMask: u32§__bindgen_anon_1: _zend_array__bindgen_ty_2§nNumUsed: u32§nNumOfElements: u32§nTableSize: u32§nInternalPointer: u32§nNextFreeElement: zend_long§pDestructor: dtor_func_t

Implementations§

source§

impl _zend_array

source

pub fn new() -> ZBox<Self>

Creates a new, empty, PHP hashtable, returned inside a ZBox.

§Example
use ext_php_rs::types::ZendHashTable;

let ht = ZendHashTable::new();
§Panics

Panics if memory for the hashtable could not be allocated.

source

pub fn with_capacity(size: u32) -> ZBox<Self>

Creates a new, empty, PHP hashtable with an initial size, returned inside a ZBox.

§Parameters
  • size - The size to initialize the array with.
§Example
use ext_php_rs::types::ZendHashTable;

let ht = ZendHashTable::with_capacity(10);
§Panics

Panics if memory for the hashtable could not be allocated.

source

pub fn len(&self) -> usize

Returns the current number of elements in the array.

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.push(1);
ht.push("Hello, world");

assert_eq!(ht.len(), 2);
source

pub fn is_empty(&self) -> bool

Returns whether the hash table is empty.

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

assert_eq!(ht.is_empty(), true);

ht.push(1);
ht.push("Hello, world");

assert_eq!(ht.is_empty(), false);
source

pub fn clear(&mut self)

Clears the hash table, removing all values.

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.insert("test", "hello world");
assert_eq!(ht.is_empty(), false);

ht.clear();
assert_eq!(ht.is_empty(), true);
source

pub fn get(&self, key: &str) -> Option<&Zval>

Attempts to retrieve a value from the hash table with a string key.

§Parameters
  • key - The key to search for in the hash table.
§Returns
  • Some(&Zval) - A reference to the zval at the position in the hash table.
  • None - No value at the given position was found.
§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.insert("test", "hello world");
assert_eq!(ht.get("test").and_then(|zv| zv.str()), Some("hello world"));
source

pub fn get_mut(&self, key: &str) -> Option<&mut Zval>

Attempts to retrieve a value from the hash table with a string key.

§Parameters
  • key - The key to search for in the hash table.
§Returns
  • Some(&Zval) - A reference to the zval at the position in the hash table.
  • None - No value at the given position was found.
§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.insert("test", "hello world");
assert_eq!(ht.get("test").and_then(|zv| zv.str()), Some("hello world"));
source

pub fn get_index(&self, key: u64) -> Option<&Zval>

Attempts to retrieve a value from the hash table with an index.

§Parameters
  • key - The key to search for in the hash table.
§Returns
  • Some(&Zval) - A reference to the zval at the position in the hash table.
  • None - No value at the given position was found.
§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.push(100);
assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(100));
source

pub fn get_index_mut(&self, key: u64) -> Option<&mut Zval>

Attempts to retrieve a value from the hash table with an index.

§Parameters
  • key - The key to search for in the hash table.
§Returns
  • Some(&Zval) - A reference to the zval at the position in the hash table.
  • None - No value at the given position was found.
§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.push(100);
assert_eq!(ht.get_index(0).and_then(|zv| zv.long()), Some(100));
source

pub fn remove(&mut self, key: &str) -> Option<()>

Attempts to remove a value from the hash table with a string key.

§Parameters
  • key - The key to remove from the hash table.
§Returns
  • Some(()) - Key was successfully removed.
  • None - No key was removed, did not exist.
§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.insert("test", "hello world");
assert_eq!(ht.len(), 1);

ht.remove("test");
assert_eq!(ht.len(), 0);
source

pub fn remove_index(&mut self, key: u64) -> Option<()>

Attempts to remove a value from the hash table with a string key.

§Parameters
  • key - The key to remove from the hash table.
§Returns
  • Ok(()) - Key was successfully removed.
  • None - No key was removed, did not exist.
§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.push("hello");
assert_eq!(ht.len(), 1);

ht.remove_index(0);
assert_eq!(ht.len(), 0);
source

pub fn insert<V>(&mut self, key: &str, val: V) -> Result<()>
where V: IntoZval,

Attempts to insert an item into the hash table, or update if the key already exists. Returns nothing in a result if successful.

§Parameters
  • key - The key to insert the value at in the hash table.
  • value - The value to insert into the hash table.
§Returns

Returns nothing in a result on success. Returns an error if the key could not be converted into a CString, or converting the value into a Zval failed.

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.insert("a", "A");
ht.insert("b", "B");
ht.insert("c", "C");
assert_eq!(ht.len(), 3);
source

pub fn insert_at_index<V>(&mut self, key: u64, val: V) -> Result<()>
where V: IntoZval,

Inserts an item into the hash table at a specified index, or updates if the key already exists. Returns nothing in a result if successful.

§Parameters
  • key - The index at which the value should be inserted.
  • val - The value to insert into the hash table.
§Returns

Returns nothing in a result on success. Returns an error if converting the value into a Zval failed.

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.insert_at_index(0, "A");
ht.insert_at_index(5, "B");
ht.insert_at_index(0, "C"); // notice overriding index 0
assert_eq!(ht.len(), 2);
source

pub fn push<V>(&mut self, val: V) -> Result<()>
where V: IntoZval,

Pushes an item onto the end of the hash table. Returns a result containing nothing if the element was successfully inserted.

§Parameters
  • val - The value to insert into the hash table.
§Returns

Returns nothing in a result on success. Returns an error if converting the value into a Zval failed.

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.push("a");
ht.push("b");
ht.push("c");
assert_eq!(ht.len(), 3);
source

pub fn has_numerical_keys(&self) -> bool

Checks if the hashtable only contains numerical keys.

§Returns

True if all keys on the hashtable are numerical.

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.push(0);
ht.push(3);
ht.push(9);
assert!(ht.has_numerical_keys());

ht.insert("obviously not numerical", 10);
assert!(!ht.has_numerical_keys());
source

pub fn has_sequential_keys(&self) -> bool

Checks if the hashtable has numerical, sequential keys.

§Returns

True if all keys on the hashtable are numerical and are in sequential order (i.e. starting at 0 and not skipping any keys).

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

ht.push(0);
ht.push(3);
ht.push(9);
assert!(ht.has_sequential_keys());

ht.insert_at_index(90, 10);
assert!(!ht.has_sequential_keys());
source

pub fn values(&self) -> Values<'_>

Returns an iterator over the values contained inside the hashtable, as if it was a set or list.

§Example
use ext_php_rs::types::ZendHashTable;

let mut ht = ZendHashTable::new();

for val in ht.values() {
    dbg!(val);
}
source

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

Returns an iterator over the key(s) and value contained inside the hashtable.

§Example
use ext_php_rs::types::{ZendHashTable, ArrayKey};

let mut ht = ZendHashTable::new();

for (key, val) in ht.iter() {
    match &key {
        ArrayKey::Long(index) => {
        }
        ArrayKey::String(key) => {
        }
    }
    dbg!(key, val);
}

Trait Implementations§

source§

impl<'a, V> TryFrom<&'a _zend_array> for HashMap<String, V>
where V: FromZval<'a>,

HashMap

§

type Error = Error

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

fn try_from(value: &'a ZendHashTable) -> Result<Self>

Performs the conversion.
source§

impl<'a, T> TryFrom<&'a _zend_array> for Vec<T>
where T: FromZval<'a>,

Vec

§

type Error = Error

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

fn try_from(value: &'a ZendHashTable) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.