Module sciter::value
[−]
[src]
Rust interface to the sciter::value
.
Sciter Value
holds superset of JSON objects.
It can contain as pure JSON objects (numbers, strings, maps and arrays) as internal objects like DOM elements, proxies of script functions, objects and arrays.
Basic usage
You can create an empty (undefined) Sciter Value
with new()
:
use sciter::Value; let v = Value::new(); assert!(v.is_undefined()); assert!(!v.is_null());
Or explicitly create Value
for specified type:
use sciter::Value; let v = Value::null(); assert!(v.is_null()); let v = Value::symbol("hello"); assert!(v.is_symbol()); assert!(v.is_string()); let v = Value::error("hello"); assert!(v.is_error_string()); assert!(v.is_string()); // allocate a new array with 4 empty elements let v = Value::array(4); assert!(v.is_array()); assert!(v.len() == 4); // allocate a new value with map type let v = Value::map(); assert!(v.is_map()); assert!(v.len() == 0);
Also there is conversion from Rust types:
use sciter::Value; let v = Value::from(true); assert!(v.is_bool()); let v = Value::from(1); assert!(v.is_int()); let v = Value::from(1.0); assert!(v.is_float()); let v = Value::from("hello"); assert!(v.is_string()); let v = Value::from(b"123".as_ref()); assert!(v.is_bytes());
And from sequence of objects:
use sciter::Value; let v: Value = ["1","2","3"].iter().cloned().collect(); assert!(v.is_array()); assert_eq!(v.len(), 3); assert_eq!(v[0], Value::from("1"));
To access its contents you should use one of to_
methods:
use sciter::Value; let v = Value::from(4); assert_eq!(v.to_int(), Some(4));
Note that there is two functions that converts Value
to JSON and back:
use sciter::Value; let mut v: Value = "[1, 2, 3, 4]".parse().unwrap(); let json_str = v.into_string();
Array access:
use sciter::Value; let mut v: Value = "[10, 20]".parse().unwrap(); assert_eq!(v[0], Value::from(10)); // explicit arguments: v.set(1, Value::from(21)); v.set(2, Value::from(22)); // implicit arguments: v.set(1, 21); v.set(2, 22); assert_eq!(v.len(), 3); assert!(v.get(0).is_int());
Map access:
use sciter::Value; let mut v: Value = "{one: 1, two: 2}".parse().unwrap(); assert_eq!(v["one"], 1.into()); assert_eq!(v.get_item("one"), 1.into()); assert_eq!(v[Value::from("one")], Value::from(1)); v.set_item("three", 3); assert!(v.get_item("one").is_int());
Thread safety
Essentially, sciter::Value
is a reference to a certain resource of the single-threaded script VM
(besides simple cases like integers or floating point values), so it should be accessed from GUI thread only.
To eliminate this restriction, Value.isolate()
is used to isolate the value
from the VM and convert it to plain JSON types.
Isolated values can be moved between threads safely.
At any given moment of time only one thread shall be an owner/operator of corresponding Sciter value.
Therefore you can pass isolate
d Sciter values safely between threads,
but only one thread shall access/mutate it.
Structs
IsolatedValue |
Used to pass values between threads. |
KeyIterator |
An iterator visiting all keys of key/value pairs in the map-like |
SeqIterator |
An iterator over the sub-elements of a |
Value |
|
Enums
VALUE_RESULT | |
VALUE_STRING_CVT_TYPE | |
VALUE_TYPE |
Traits
FromValue |
Helper trait |