Value

Enum Value 

Source
pub enum Value {
Show 18 variants F32(f32), F64(f64), I32(i32), I64(i64), U32(u32), U64(u64), I8(i8), U8(u8), I16(i16), U16(u16), String(String), Array(Array), Map(Map), Bool(bool), Null, Binary(Binary), TimeStamp(TimeStamp), Id(Id),
}

Variants§

§

F32(f32)

§

F64(f64)

§

I32(i32)

§

I64(i64)

§

U32(u32)

§

U64(u64)

§

I8(i8)

§

U8(u8)

§

I16(i16)

§

U16(u16)

§

String(String)

§

Array(Array)

§

Map(Map)

§

Bool(bool)

§

Null

§

Binary(Binary)

§

TimeStamp(TimeStamp)

§

Id(Id)

Implementations§

Source§

impl Value

Source

pub fn from_bytes(bytes: &[u8]) -> DecodeResult<Value>

Source§

impl Value

Source

pub fn to_bytes(&self) -> EncodeResult<Vec<u8>>

Examples found in repository?
examples/to_bytes.rs (line 6)
1fn main() {
2    use nson::m;
3    use nson::Value;
4
5    let a = Value::I32(123);
6    println!("{:?}", a.to_bytes());
7
8    let m = m! {
9        "a": 123i32,
10        "b": {
11            "c": 456
12        }
13    };
14    println!("{:?}", m.to_bytes());
15    let m: Value = m.into();
16    println!("{:?}", m.to_bytes());
17
18    let a = 123;
19    let bytes = nson::encode::to_bytes(&a).unwrap();
20    println!("{:?}", bytes);
21
22    let b: i32 = nson::decode::from_bytes(&bytes).unwrap();
23    println!("{:?}", b);
24    assert_eq!(a, b);
25}
Source§

impl Value

Source

pub fn element_type(&self) -> DataType

Source

pub fn bytes_size(&self) -> usize

Examples found in repository?
examples/basic_types.rs (line 75)
7fn main() {
8    println!("=== NSON 基本类型示例 ===\n");
9
10    // 1. 所有数字类型
11    let numbers = m! {
12        "i8": -128i8,           // 8位有符号整数
13        "u8": 255u8,            // 8位无符号整数
14        "i16": -32768i16,       // 16位有符号整数
15        "u16": 65535u16,        // 16位无符号整数
16        "i32": -2147483648i32,  // 32位有符号整数
17        "u32": 4294967295u32,   // 32位无符号整数
18        "i64": -9223372036854775808i64,  // 64位有符号整数
19        "u64": 18446744073709551615u64,  // 64位无符号整数
20        "f32": 3.14159f32,      // 32位浮点数
21        "f64": 2.718281828459045f64,  // 64位浮点数
22    };
23
24    println!("数字类型 Map:");
25    println!("{}\n", numbers);
26
27    // 2. 编码和解码
28    let bytes = numbers.to_bytes().unwrap();
29    println!("编码后大小: {} 字节", bytes.len());
30
31    let decoded = Map::from_bytes(&bytes).unwrap();
32    println!("解码成功: {:?}\n", decoded == numbers);
33
34    // 3. 类型访问
35    println!("访问各种类型:");
36    println!("  i8 value: {:?}", decoded.get_i8("i8"));
37    println!("  u8 value: {:?}", decoded.get_u8("u8"));
38    println!("  i16 value: {:?}", decoded.get_i16("i16"));
39    println!("  u16 value: {:?}", decoded.get_u16("u16"));
40    println!("  i32 value: {:?}", decoded.get_i32("i32"));
41    println!("  u32 value: {:?}", decoded.get_u32("u32"));
42    println!("  i64 value: {:?}", decoded.get_i64("i64"));
43    println!("  u64 value: {:?}", decoded.get_u64("u64"));
44    println!("  f32 value: {:?}", decoded.get_f32("f32"));
45    println!("  f64 value: {:?}", decoded.get_f64("f64"));
46
47    println!("\n=== 数组中的类型 ===\n");
48
49    // 4. 数组中使用不同类型
50    let mixed_array = Array::from_vec(vec![
51        Value::I8(-10),
52        Value::U8(200),
53        Value::I16(-1000),
54        Value::U16(50000),
55        Value::F32(1.5),
56        Value::from("text"),
57    ]);
58
59    println!("混合类型数组: {:?}", mixed_array);
60
61    let array_bytes = mixed_array.to_bytes().unwrap();
62    let decoded_array = Array::from_bytes(&array_bytes).unwrap();
63    println!("数组解码成功: {:?}\n", decoded_array == mixed_array);
64
65    // 5. 类型大小比较
66    println!("=== 类型存储大小 ===");
67    let size_demo = m! {
68        "i8": -1i8,
69        "i16": -1i16,
70        "i32": -1i32,
71        "i64": -1i64,
72    };
73
74    for (key, value) in size_demo.iter() {
75        println!("{}: {} 字节 + 1字节类型标记", key, value.bytes_size());
76    }
77}
Source

pub fn as_f32(&self) -> Option<f32>

Source

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

Source

pub fn as_i32(&self) -> Option<i32>

Source

pub fn as_u32(&self) -> Option<u32>

Source

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

Source

pub fn as_u64(&self) -> Option<u64>

Source

pub fn as_i8(&self) -> Option<i8>

Source

pub fn as_u8(&self) -> Option<u8>

Source

pub fn as_i16(&self) -> Option<i16>

Source

pub fn as_u16(&self) -> Option<u16>

Source

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

Source

pub fn as_array(&self) -> Option<&Array>

Source

pub fn as_map(&self) -> Option<&Map>

Source

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

Source

pub fn as_id(&self) -> Option<&Id>

Source

pub fn as_timestamp(&self) -> Option<TimeStamp>

Source

pub fn as_null(&self) -> Option<()>

Source

pub fn as_binary(&self) -> Option<&Binary>

Trait Implementations§

Source§

impl Clone for Value

Source§

fn clone(&self) -> Value

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Value

Source§

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

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

impl<'de> Deserialize<'de> for Value

Source§

fn deserialize<D>(deserializer: D) -> Result<Value, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Value

Source§

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

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

impl<'a> From<&'a Binary> for Value

Source§

fn from(b: &'a Binary) -> Value

Converts to this type from the input type.
Source§

impl<'a> From<&'a Id> for Value

Source§

fn from(o: &'a Id) -> Value

Converts to this type from the input type.
Source§

impl<'a> From<&'a String> for Value

Source§

fn from(s: &'a String) -> Value

Converts to this type from the input type.
Source§

impl From<&str> for Value

Source§

fn from(s: &str) -> Value

Converts to this type from the input type.
Source§

impl From<[u8; 12]> for Value

Source§

fn from(o: [u8; 12]) -> Value

Converts to this type from the input type.
Source§

impl From<Array> for Value

Source§

fn from(a: Array) -> Value

Converts to this type from the input type.
Source§

impl From<Binary> for Value

Source§

fn from(b: Binary) -> Value

Converts to this type from the input type.
Source§

impl From<Id> for Value

Source§

fn from(o: Id) -> Value

Converts to this type from the input type.
Source§

impl From<Map> for Value

Source§

fn from(d: Map) -> Value

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl From<String> for Value

Source§

fn from(s: String) -> Value

Converts to this type from the input type.
Source§

impl From<TimeStamp> for Value

Source§

fn from(t: TimeStamp) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for Value

Source§

fn from(value: Value) -> Self

Converts to this type from the input type.
Source§

impl From<Value> for Value

Source§

fn from(value: Value) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<&String>> for Value

Source§

fn from(vec: Vec<&String>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<&str>> for Value

Source§

fn from(vec: Vec<&str>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<Array>> for Value

Source§

fn from(vec: Vec<Array>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<Id>> for Value

Source§

fn from(vec: Vec<Id>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<Map>> for Value

Source§

fn from(vec: Vec<Map>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<String>> for Value

Source§

fn from(vec: Vec<String>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<Vec<u8>>> for Value

Source§

fn from(vec: Vec<Vec<u8>>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<bool>> for Value

Source§

fn from(vec: Vec<bool>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<f32>> for Value

Source§

fn from(vec: Vec<f32>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<f64>> for Value

Source§

fn from(vec: Vec<f64>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<i32>> for Value

Source§

fn from(vec: Vec<i32>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<i64>> for Value

Source§

fn from(vec: Vec<i64>) -> Value

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for Value

Source§

fn from(b: Vec<u8>) -> Value

Converts to this type from the input type.
Source§

impl From<bool> for Value

Source§

fn from(b: bool) -> Value

Converts to this type from the input type.
Source§

impl From<f32> for Value

Source§

fn from(f: f32) -> Value

Converts to this type from the input type.
Source§

impl From<f64> for Value

Source§

fn from(f: f64) -> Value

Converts to this type from the input type.
Source§

impl From<i16> for Value

Source§

fn from(i: i16) -> Value

Converts to this type from the input type.
Source§

impl From<i32> for Value

Source§

fn from(i: i32) -> Value

Converts to this type from the input type.
Source§

impl From<i64> for Value

Source§

fn from(i: i64) -> Value

Converts to this type from the input type.
Source§

impl From<i8> for Value

Source§

fn from(i: i8) -> Value

Converts to this type from the input type.
Source§

impl From<u16> for Value

Source§

fn from(u: u16) -> Value

Converts to this type from the input type.
Source§

impl From<u32> for Value

Source§

fn from(u: u32) -> Value

Converts to this type from the input type.
Source§

impl From<u64> for Value

Source§

fn from(u: u64) -> Value

Converts to this type from the input type.
Source§

impl From<u8> for Value

Source§

fn from(u: u8) -> Value

Converts to this type from the input type.
Source§

impl FromIterator<Value> for Array

Source§

fn from_iter<I: IntoIterator<Item = Value>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl PartialEq for Value

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Value

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Value

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 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,