Rust

types

Module

The PHP value model: Value, Str, Array, ArrayKey.

One model for every crate that carries PHP values (request payloads, documents, cache entries, queue messages), with the engine's semantics: byte strings, ordered arrays with integer and string keys, stdClass, integers distinct from floats. The codecs (crate::json, crate::serialize, crate::igbinary), type juggling and comparison (Value::loose_eq, Value::cmp_php) and sorting (crate::sort) work on it.

PHPRust
null, bool, int, floatValue::Null, Value::Bool, Value::Int, Value::Float
string (bytes)Value::Str / Str
arrayValue::Array / Array, keys ArrayKey
stdClassValue::Object (its property table, string keys as given)
other objects (Utopia\Database\Document, ...)Value::Ext, an Extension
ZEND_HANDLE_NUMERIC_STRArrayKey::normalize, numeric_key
gettype, (bool), (string), (float)Value::type_name, Value::truthy, Value::to_php_string, Value::to_double
==, ===, <=> (zend_compare)Value::loose_eq, Value::strict_eq, Value::cmp_php, Value::compare
Utopia\Http\Request body decodingValue::from_json

Values have no identity: PHP references and shared instances are copies here. Two object values are never === (separately created PHP objects are distinct instances), which is the one place where identity shows.

Against the skeleton in rfc/rust-database.md: Array::iter yields borrowed KeyRefs (a packed array stores no keys), and the engine's packed-array bookkeeping is part of Array because it decides the next free index (see its docs).

Contents

use utopia_php_std::Array;
Rust
pub struct Array<X: Extension = Never> {
    // private fields
}

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.

Also implements Drop, Clone, Default, IntoIterator, FromIterator<(ArrayKey, Value<X>)>, FromIterator<Value<X>>, PartialEq, Debug, From<Array<X>>.
use utopia_php_std::types::ArrayFull;
Rust
pub struct ArrayFull;

$a[] = $v failed: the next integer key would be past PHP_INT_MAX ("Cannot add element to the array as the next element is already occupied").

Also implements Display.
use utopia_php_std::EngineError;
Rust
pub struct EngineError {
    pub class: &'static str,
    pub message: String,
}

An engine error thrown while juggling a value: TypeError, ValueError, JsonException, or Error (an object that cannot become a string).

Also implements Display.
use utopia_php_std::Str;
Rust
pub struct Str(_);

A PHP string: bytes, not necessarily UTF-8.

Three representations, chosen on construction and invisible otherwise: short strings (up to 23 bytes) are stored inline, string literals borrow their 'static bytes, and longer strings share a Bytes buffer, so clones and slices of decoded input never copy.

Also implements Default, Deref, AsRef<[u8]>, Borrow<[u8]>, PartialEq, PartialEq<[u8]>, PartialEq<&[u8]>, PartialEq<&[u8; N]>, PartialEq<str>, PartialEq<&str>, PartialOrd, Ord, Hash, Debug, From<&str>, From<String>, From<&[u8]>, From<Vec<u8>>, From<Bytes>, From<Str>.
use utopia_php_std::ArrayKey;
Rust
pub enum ArrayKey {
    Int(i64),
    Str(Str),
}

A PHP array key: an integer or a byte string.

PHP turns canonical decimal integer strings into integer keys ($a["7"] is $a[7], "07" and "-0" stay strings): ArrayKey::normalize applies that rule. Array stores keys as given, because some producers (igbinary, object property tables) keep numeric strings as string keys.

Also implements Hash, Debug, From<i64>, From<&str>.
use utopia_php_std::types::Iter;
Rust
pub enum Iter<'a, X: Extension> {
    Packed(Enumerate<Iter<'a, Value<X>>>),
    Hash(Iter<'a, (ArrayKey, Value<X>)>),
}

Entries of an Array in order.

Also implements Iterator, DoubleEndedIterator.
use utopia_php_std::types::KeyRef;
Rust
pub enum KeyRef<'a> {
    Int(i64),
    Str(&'a [u8]),
}

A borrowed ArrayKey, used to look keys up and to iterate packed arrays (which store no keys).

Also implements Hash.
use utopia_php_std::Never;
Rust
pub enum Never {

}

No extension: plain PHP values.

use utopia_php_std::Value;
Rust
pub enum Value<X: Extension = Never> {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(Str),
    Array(Array<X>),
    Object(Array<X>),
    Ext(X),
}

A PHP value.

Also implements FromIterator<Value<X>>, From<bool>, From<i64>, From<f64>, From<&str>, From<Str>, From<Array<X>>.
use utopia_php_std::Extension;
Rust
pub trait Extension: Clone + PartialEq + Debug + Send + Sync + 'static

Objects of classes other than stdClass that a crate carries in Value::Ext (for the database: Document and Operator).

Implemented byNever
use utopia_php_std::types::numeric_key;
Rust
pub fn numeric_key(key: &[u8]) -> Option<i64>

ZEND_HANDLE_NUMERIC_STR: the integer a string array key becomes, if any.

An optional - and decimal digits without leading zeros ("0" itself is allowed, "-0" is not), within the i64 range.