Skip to main content

Value

Enum Value 

Source
pub enum Value {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    String(String),
    Bytes(Vec<u8>),
    List(Vec<Value>),
    Map(Vec<(Key, Value)>),
    Object {
        class: String,
        properties: Vec<(Key, Value)>,
    },
}
Expand description

A PHP value, as produced by unserialize() or accepted by serialize().

PHP arrays are ordered maps with int or string keys. An array whose keys are exactly 0..n in order decodes to Value::List; any other array decodes to Value::Map, which keeps PHP’s key order.

Variants§

§

Null

PHP null.

§

Bool(bool)

PHP bool.

§

Int(i64)

PHP int (64-bit).

§

Float(f64)

PHP float.

§

String(String)

PHP string holding valid UTF-8.

§

Bytes(Vec<u8>)

PHP string that is not valid UTF-8. Decoding yields this variant only for such strings; both string variants serialize to a PHP string.

§

List(Vec<Value>)

PHP array with the keys 0..n.

§

Map(Vec<(Key, Value)>)

PHP array with any other keys, in PHP order.

§

Object

A PHP object. Decode-only: the class is never instantiated or looked up, so decrypting untrusted payloads cannot trigger unserialize gadget chains, and the value cannot be serialized again (Error::Serialize). Property names keep PHP’s visibility mangling ("\0*\0name" for protected, "\0Class\0name" for private).

Fields

§class: String

Class name.

§properties: Vec<(Key, Value)>

Properties in PHP order.

Implementations§

Source§

impl Value

Source

pub fn list<I, V>(items: I) -> Self
where I: IntoIterator<Item = V>, V: Into<Value>,

Builds a Value::List from an iterator.

Examples found in repository?
examples/genvectors.rs (lines 57-63)
30fn cases() -> Vec<(&'static str, Value)> {
31    vec![
32        ("null", Value::Null),
33        ("true", true.into()),
34        ("false", false.into()),
35        ("int_zero", 0.into()),
36        ("int_negative", (-42).into()),
37        ("int_max", i64::MAX.into()),
38        ("int_min", i64::MIN.into()),
39        ("float", 2.5.into()),
40        ("float_whole", 2.0.into()),
41        ("float_tenth", 0.1.into()),
42        ("float_large", 1.0e25.into()),
43        ("float_small", 1.5e-7.into()),
44        ("float_negative_zero", (-0.0).into()),
45        ("float_pi", std::f64::consts::PI.into()),
46        ("float_max", f64::MAX.into()),
47        ("float_min_subnormal", 4.9e-324.into()),
48        ("float_inf", f64::INFINITY.into()),
49        ("float_negative_inf", f64::NEG_INFINITY.into()),
50        ("float_nan", f64::NAN.into()),
51        ("string_empty", "".into()),
52        ("string_unicode", "Grüße 世界 🚀 café".into()),
53        ("string_quotes", r#"say "hi"; s:3:"x";"#.into()),
54        ("string_binary", Value::Bytes(vec![0x00, 0xff, 0xfe, 0x80])),
55        (
56            "list",
57            Value::list([
58                Value::Int(1),
59                "two".into(),
60                true.into(),
61                Value::Null,
62                Value::list([3, 4]),
63            ]),
64        ),
65        ("empty_array", Value::List(vec![])),
66        (
67            "assoc",
68            Value::map([
69                ("user", Value::from("alice")),
70                ("id", 7.into()),
71                ("active", true.into()),
72                ("score", 2.5.into()),
73                ("tags", Value::list(["x", "y"])),
74                ("meta", Value::Null),
75            ]),
76        ),
77        (
78            "mixed_keys",
79            Value::Map(vec![
80                (5.into(), "a".into()),
81                ("x".into(), "b".into()),
82                (7.into(), Value::map([("nested", Value::list([1, 2]))])),
83            ]),
84        ),
85        (
86            "non_sequential_list",
87            Value::Map(vec![(1.into(), "a".into()), (0.into(), "b".into())]),
88        ),
89        (
90            "nested",
91            Value::map([(
92                "l1",
93                Value::map([(
94                    "l2",
95                    Value::map([(
96                        "l3",
97                        Value::List(vec!["deep".into(), 1.5.into(), false.into()]),
98                    )]),
99                )]),
100            )]),
101        ),
102    ]
103}
Source

pub fn map<I, K, V>(entries: I) -> Self
where I: IntoIterator<Item = (K, V)>, K: Into<Key>, V: Into<Value>,

Builds a Value::Map from key/value pairs, keeping their order.

Examples found in repository?
examples/genvectors.rs (lines 68-75)
30fn cases() -> Vec<(&'static str, Value)> {
31    vec![
32        ("null", Value::Null),
33        ("true", true.into()),
34        ("false", false.into()),
35        ("int_zero", 0.into()),
36        ("int_negative", (-42).into()),
37        ("int_max", i64::MAX.into()),
38        ("int_min", i64::MIN.into()),
39        ("float", 2.5.into()),
40        ("float_whole", 2.0.into()),
41        ("float_tenth", 0.1.into()),
42        ("float_large", 1.0e25.into()),
43        ("float_small", 1.5e-7.into()),
44        ("float_negative_zero", (-0.0).into()),
45        ("float_pi", std::f64::consts::PI.into()),
46        ("float_max", f64::MAX.into()),
47        ("float_min_subnormal", 4.9e-324.into()),
48        ("float_inf", f64::INFINITY.into()),
49        ("float_negative_inf", f64::NEG_INFINITY.into()),
50        ("float_nan", f64::NAN.into()),
51        ("string_empty", "".into()),
52        ("string_unicode", "Grüße 世界 🚀 café".into()),
53        ("string_quotes", r#"say "hi"; s:3:"x";"#.into()),
54        ("string_binary", Value::Bytes(vec![0x00, 0xff, 0xfe, 0x80])),
55        (
56            "list",
57            Value::list([
58                Value::Int(1),
59                "two".into(),
60                true.into(),
61                Value::Null,
62                Value::list([3, 4]),
63            ]),
64        ),
65        ("empty_array", Value::List(vec![])),
66        (
67            "assoc",
68            Value::map([
69                ("user", Value::from("alice")),
70                ("id", 7.into()),
71                ("active", true.into()),
72                ("score", 2.5.into()),
73                ("tags", Value::list(["x", "y"])),
74                ("meta", Value::Null),
75            ]),
76        ),
77        (
78            "mixed_keys",
79            Value::Map(vec![
80                (5.into(), "a".into()),
81                ("x".into(), "b".into()),
82                (7.into(), Value::map([("nested", Value::list([1, 2]))])),
83            ]),
84        ),
85        (
86            "non_sequential_list",
87            Value::Map(vec![(1.into(), "a".into()), (0.into(), "b".into())]),
88        ),
89        (
90            "nested",
91            Value::map([(
92                "l1",
93                Value::map([(
94                    "l2",
95                    Value::map([(
96                        "l3",
97                        Value::List(vec!["deep".into(), 1.5.into(), false.into()]),
98                    )]),
99                )]),
100            )]),
101        ),
102    ]
103}
Source

pub fn is_null(&self) -> bool

Returns true for Value::Null.

Source

pub fn as_bool(&self) -> Option<bool>

Returns the value of a Value::Bool.

Source

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

Returns the value of a Value::Int.

Source

pub fn as_f64(&self) -> Option<f64>

Returns the value of a Value::Float.

Source

pub fn as_str(&self) -> Option<&str>

Returns the text of a Value::String.

Source

pub fn as_list(&self) -> Option<&[Value]>

Returns the items of a Value::List.

Source

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

Looks up a string key in a Value::Map or the properties of a Value::Object.

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

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

Formats the value using the given formatter. Read more
Source§

impl From<&str> for Value

Source§

fn from(s: &str) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<Value>> From<Option<T>> for Value

Source§

fn from(v: Option<T>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Self

Converts to this type from the input type.
Source§

impl<T: Into<Value>> From<Vec<T>> for Value

Source§

fn from(items: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(f: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(f: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Value

Source§

fn from(i: i8) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Value

Source§

fn from(i: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(i: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(i: i64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Value

Source§

fn from(i: u8) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Value

Source§

fn from(i: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(i: u32) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Value

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnsafeUnpin for Value

§

impl UnwindSafe for Value

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.