Expand description
GcValue (for Garbage-collected Value) is a generic value type,
capturing the data model of common formats such as JSON, YAML, CBOR,
and the INFRA API.
It uses Arc for cheap shallow cloning,
mimicking the assumptions of garbage-collected language (such as Javascript).
It also supports serialization/deserialization via serde.
§Construction
GcValue provides a constructor for each variant
(e.g. GcValue::new_text, GcValue::new_int, etc.),
which accept any type directly convertible (with From) to the underlying data.
For composite variants (like GcValue::Array or GcValue::Map),
it also provides “collecting” constructors (e.g. GcValue::collect_array)
that accepts any iterable of convertible types.
GcValue also implements From and FromIterator for a number of types.
§Comparison
This type implements usual comparison traits such as Eq, Ord and Hash.
It should be noted that:
- the implementation of
Ordis mostly for internal use, (sorting key-value pairs in theGcValue::Mapvariant). - the implementation of
EqandHashfor theGcValue::Floatvariant use bit-wise comparison, not IEE754-compliant comparison. In particular, twoGcValue::Floatwith valueNaNare considered equal.
GcValue also implements PartialEq with common types, in the expected way:
- comparing with
&strforGcValue::Text; - comparing with most integer types for
GcValue::Int. - comparing with
f64andf32forGcValue::Float(using bit-wise comparison, not numeric comparison); - comparing with
boolforGcValue::Boolean, - comparing with
[T]forGcValue::Array, whereTis any type comparable toGcValue; - comparing with
MapforGcValue::Map; - comparing with
(T, U)forGcValue::Tag, whereTandUare any types comparable toGcValue.
Note that, for regularity, a [u8] will match a GcValue::Array, not a GcValue::Bytes.
§Tagged value
GcValue has the notion of tagged value,
because this notion exists in both CBOR (with integers as tags) and YAML (with strings as tags).
Note however that this notions does not have any direct correspondence in serde,
and is currently not supported by the implementation of serde:Serializer.
(consistently with serde_cbor, for example).
Structs§
- Map
- Internal representation of
GcValue::Map, representing an order map where keys and values areGcValue.