1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
//! Contains Value and ValueRef structs and its conversion traits. #[derive(Copy, Clone, Debug, PartialEq)] pub enum Integer { /// Every non-negative integer is treated as u64, even if it fits in i64. U64(u64), /// Every negative integer is treated as i64. I64(i64), } #[derive(Copy, Clone, Debug, PartialEq)] pub enum Float { F32(f32), F64(f64), } #[derive(Clone, Debug, PartialEq)] pub enum Value { /// Nil represents nil. Nil, /// Boolean represents true or false. Boolean(bool), /// Integer represents an integer. Integer(Integer), /// Float represents a floating point number. Float(Float), /// String extending Raw type represents a UTF-8 string. String(String), /// Binary extending Raw type represents a byte array. Binary(Vec<u8>), /// Array represents a sequence of objects. Array(Vec<Value>), /// Map represents key-value pairs of objects. Map(Vec<(Value, Value)>), /// Extended implements Extension interface: represents a tuple of type information and a byte /// array where type information is an integer whose meaning is defined by applications. Ext(i8, Vec<u8>), } #[derive(Clone, Debug, PartialEq)] pub enum ValueRef<'a> { /// Nil represents nil. Nil, /// Boolean represents true or false. Boolean(bool), /// Integer represents an integer. Integer(Integer), /// Float represents a floating point number. Float(Float), /// String extending Raw type represents a UTF-8 string. String(&'a str), /// Binary extending Raw type represents a byte array. Binary(&'a [u8]), /// Array represents a sequence of objects. Array(Vec<ValueRef<'a>>), /// Map represents key-value pairs of objects. Map(Vec<(ValueRef<'a>, ValueRef<'a>)>), /// Extended implements Extension interface: represents a tuple of type information and a byte /// array where type information is an integer whose meaning is defined by applications. Ext(i8, &'a [u8]), } impl<'a> ValueRef<'a> { /// Converts the current non-owning value to an owned Value. /// /// This is achieved by deep copying all underlying structures and borrowed buffers. /// /// # Panics /// /// Panics in unable to allocate memory to keep all internal structures and buffers. /// /// # Examples /// ``` /// use rmp::{Value, ValueRef}; /// use rmp::value::Integer; /// /// let val = ValueRef::Array(vec![ /// ValueRef::Nil, /// ValueRef::Integer(Integer::U64(42)), /// ValueRef::Array(vec![ /// ValueRef::String("le message"), /// ]) /// ]); /// /// let expected = Value::Array(vec![ /// Value::Nil, /// Value::Integer(Integer::U64(42)), /// Value::Array(vec![ /// Value::String("le message".to_string()) /// ]) /// ]); /// /// assert_eq!(expected, val.to_owned()); /// ``` pub fn to_owned(&self) -> Value { match self { &ValueRef::Nil => Value::Nil, &ValueRef::Boolean(val) => Value::Boolean(val), &ValueRef::Integer(val) => Value::Integer(val), &ValueRef::Float(val) => Value::Float(val), &ValueRef::String(val) => Value::String(val.to_string()), &ValueRef::Binary(val) => Value::Binary(val.to_vec()), &ValueRef::Array(ref val) => { Value::Array(val.iter().map(|v| v.to_owned()).collect()) } &ValueRef::Map(ref val) => { Value::Map(val.iter().map(|&(ref k, ref v)| (k.to_owned(), v.to_owned())).collect()) } &ValueRef::Ext(ty, buf) => Value::Ext(ty, buf.to_vec()), } } } // For some weird reasons I can't implement it manually. // It gives: conflicting implementations for trait `collections::borrow::ToOwned` // impl<'a> ToOwned for ValueRef<'a> { // type Owned = Value; // }