types
ModuleThe 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, and each is fuzzed against the engine (bin/compat fuzz php-std).
| PHP | Rust |
|---|---|
null, bool, int, float | Value::Null, Value::Bool, Value::Int, Value::Float |
string (bytes) | Value::Str / Str |
array | Value::Array / Array, keys ArrayKey |
stdClass | Value::Object (its property table, string keys as given) |
other objects (Utopia\Database\Document, ...) | Value::Ext, an Extension |
ZEND_HANDLE_NUMERIC_STR | ArrayKey::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 decoding | Value::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
3 submodules, and what the module defines itself.
use php_std::EngineError;pub struct EngineErrorAn engine error thrown while juggling a value: TypeError, ValueError,
JsonException, or Error (an object that cannot become a string).
Display.use php_std::Never;pub enum NeverNo extension: plain PHP values.
use php_std::Extension;pub trait Extension: Clone + PartialEq + fmt::Debug + Send + Sync + 'staticObjects of classes other than stdClass that a crate carries in
Value::Ext (for the database: Document and Operator).
types::array
Own page[Array]: a PHP array (an ordered map with integer and string keys).
use php_std::Array;pub struct Array<X: Extension = Never>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.
Drop, Clone, Default, IntoIterator, FromIterator, PartialEq, Debug.use php_std::types::array::ArrayFull;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").
Display.use php_std::ArrayKey;pub enum ArrayKeyA 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.
Hash, Debug, From.use php_std::types::array::Iter;pub enum Iter<'a, X: Extension>Entries of an Array in order.
Iterator, DoubleEndedIterator.use php_std::types::array::KeyRef;pub enum KeyRef<'a>A borrowed ArrayKey, used to look keys up and to iterate packed
arrays (which store no keys).
Hash.use php_std::types::array::numeric_key;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.
types::str
Own page[Str]: a PHP string, which is a byte string.
use php_std::Str;pub struct Str(Repr)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.
Default, Deref, AsRef, Borrow, PartialEq, PartialOrd, Ord, Hash, Debug, From.types::value
Own page[Value]: a PHP value, and the engine's type juggling and comparison on it.
use php_std::Value;pub enum Value<X: Extension = Never>A PHP value.
From.