[−][src]Enum msgpack_simple::MsgPack
A piece of MessagePack-compatible data
use msgpack_simple::{MsgPack, MapElement, Extension}; let message = MsgPack::Map(vec![ MapElement { key: MsgPack::String(String::from("hello")), value: MsgPack::Int(42) }, MapElement { key: MsgPack::String(String::from("world")), value: MsgPack::Array(vec![ MsgPack::Boolean(true), MsgPack::Nil, MsgPack::Binary(vec![0x42, 0xff]), MsgPack::Extension(Extension { type_id: 2, value: vec![0x32, 0x4a, 0x67, 0x11] }) ]) } ]);
Variants
Empty value
use msgpack_simple::MsgPack; let nil = MsgPack::Nil; assert!(nil.is_nil());
Int(i64)
Signed integer, not much magic here
use msgpack_simple::MsgPack; let int = MsgPack::Int(42); assert!(int.is_int()); assert!(int.is_some_int()); assert_eq!(int.clone().as_int().unwrap(), 42); assert_eq!(int.as_some_int().unwrap(), 42);
Uint(u64)
Unsigned integer
use msgpack_simple::MsgPack; let uint = MsgPack::Uint(42); assert!(uint.is_uint()); assert!(uint.is_some_int()); assert_eq!(uint.clone().as_uint().unwrap(), 42); assert_eq!(uint.as_some_int().unwrap(), 42);
Float(f64)
Floating-point number
use msgpack_simple::MsgPack; let float = MsgPack::Float(42.0); assert!(float.is_float()); assert_eq!(float.as_float().unwrap(), 42.0);
Boolean(bool)
Boolean (wait, really?)
use msgpack_simple::MsgPack; let boolean = MsgPack::Boolean(true); assert!(boolean.is_boolean()); assert_eq!(boolean.as_boolean().unwrap(), true);
String(String)
Unicode compatible string
use msgpack_simple::MsgPack; let string = MsgPack::String(String::from("foo")); assert!(string.is_string()); assert_eq!(string.as_string().unwrap(), "foo".to_string());
Raw binary value
use msgpack_simple::MsgPack; let binary = MsgPack::Binary(vec![0x42]); assert!(binary.is_binary()); assert_eq!(binary.as_binary().unwrap(), vec![0x42]);
An array of other MsgPack fields
use msgpack_simple::MsgPack; let array = MsgPack::Array(vec![ MsgPack::Int(42) ]); assert!(array.is_array()); assert_eq!(array.as_array().unwrap(), vec![MsgPack::Int(42)]);
Map(Vec<MapElement>)
A map with key-value pairs, both being MsgPack data fields
use msgpack_simple::{MsgPack, MapElement}; let map = MsgPack::Map(vec![ MapElement { key: MsgPack::String("foo".to_string()), value: MsgPack::String("bar".to_string()) } ]); assert!(map.is_map()); assert_eq!(map.as_map().unwrap(), vec![MapElement { key: MsgPack::String("foo".to_string()), value: MsgPack::String("bar".to_string()) }]);
Extension(Extension)
A tuple of an extension type and a raw data value
use msgpack_simple::{MsgPack, Extension}; let extension = MsgPack::Extension(Extension { type_id: 42, value: vec![0x42] }); assert!(extension.is_extension()); assert_eq!(extension.as_extension().unwrap(), Extension { type_id: 42, value: vec![0x42] });
Implementations
impl MsgPack
[src]
pub fn parse(raw: &[u8]) -> Result<MsgPack, ParseError>
[src]
Parses binary data as MessagePack
use msgpack_simple::MsgPack; let data = vec![0xaa, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x52, 0x75, 0x73, 0x74]; let decoded = MsgPack::parse(&data); assert!(decoded.is_ok()); let decoded = decoded.unwrap(); assert!(decoded.is_string()); assert_eq!(decoded.as_string().unwrap(), "Hello Rust".to_string());
pub fn encode(&self) -> Vec<u8>
[src]
Encodes a MsgPack enum into binary format
use msgpack_simple::MsgPack; let message = MsgPack::String("Hello Rust".to_string()); let encoded = message.encode(); let data = vec![0xaa, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x52, 0x75, 0x73, 0x74]; assert_eq!(encoded, data);
pub fn is_int(&self) -> bool
[src]
Checks if the MsgPack is an int variant
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Int(42).is_int(), true); assert_eq!(MsgPack::Float(42.0).is_int(), false);
pub fn as_int(self) -> Result<i64, ConversionError>
[src]
Consumes the MsgPack as int
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Int(42).as_int().unwrap(), 42);
pub fn is_uint(&self) -> bool
[src]
Checks if the MsgPack is a uint variant
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Uint(42).is_uint(), true); assert_eq!(MsgPack::Float(42.0).is_uint(), false);
pub fn as_uint(self) -> Result<u64, ConversionError>
[src]
Consumes the MsgPack as uint
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Uint(42).as_uint().unwrap(), 42);
pub fn is_some_int(&self) -> bool
[src]
Checks if the MsgPack is one of the integer variants
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Int(42).is_some_int(), true); assert_eq!(MsgPack::Uint(42).is_some_int(), true); assert_eq!(MsgPack::Float(42.0).is_some_int(), false);
pub fn as_some_int(self) -> Result<i64, ConversionError>
[src]
Consumes the MsgPack as an int, even if it's a uint
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Int(42).as_some_int().unwrap(), 42); assert_eq!(MsgPack::Uint(42).as_some_int().unwrap(), 42);
pub fn is_float(&self) -> bool
[src]
Checks if the MsgPack is a float
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Float(42.0).is_float(), true); assert_eq!(MsgPack::Int(42).is_float(), false);
pub fn as_float(self) -> Result<f64, ConversionError>
[src]
Consumes the MsgPack as a float
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Float(42.0).as_float().unwrap(), 42.0);
pub fn is_boolean(&self) -> bool
[src]
Checks if the MsgPack is a boolean
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Boolean(true).is_boolean(), true); assert_eq!(MsgPack::Int(1).is_boolean(), false);
pub fn as_boolean(self) -> Result<bool, ConversionError>
[src]
Consumes the MsgPack as a boolean
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Boolean(true).as_boolean().unwrap(), true);
pub fn is_nil(&self) -> bool
[src]
Checks if the MsgPack is a nil
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Nil.is_nil(), true); assert_eq!(MsgPack::Boolean(false).is_nil(), false);
pub fn is_string(&self) -> bool
[src]
Checks if the MsgPack is a string
use msgpack_simple::MsgPack; assert_eq!(MsgPack::String("foo".to_string()).is_string(), true); assert_eq!(MsgPack::Binary(vec![0x66, 0x6f, 0x6f]).is_string(), false);
pub fn as_string(self) -> Result<String, ConversionError>
[src]
Consumes the MsgPack as a string
use msgpack_simple::MsgPack; assert_eq!(MsgPack::String("foo".to_string()).as_string().unwrap(), "foo".to_string());
pub fn is_binary(&self) -> bool
[src]
Checks if the MsgPack is a binary
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Binary(vec![0x66, 0x6f, 0x6f]).is_binary(), true); assert_eq!(MsgPack::String("foo".to_string()).is_binary(), false);
pub fn as_binary(self) -> Result<Vec<u8>, ConversionError>
[src]
Consumes the MsgPack as a binary
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Binary(vec![0x66, 0x6f, 0x6f]).as_binary().unwrap(), vec![0x66, 0x6f, 0x6f]);
pub fn is_array(&self) -> bool
[src]
Checks if the MsgPack is an array
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Array(vec![]).is_array(), true); assert_eq!(MsgPack::Map(vec![]).is_array(), false);
pub fn as_array(self) -> Result<Vec<MsgPack>, ConversionError>
[src]
Consumes the MsgPack as an array
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Array(vec![]).as_array().unwrap(), vec![]);
pub fn is_map(&self) -> bool
[src]
Checks if the MsgPack is a map
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Map(vec![]).is_map(), true); assert_eq!(MsgPack::Array(vec![]).is_map(), false);
pub fn as_map(self) -> Result<Vec<MapElement>, ConversionError>
[src]
Consumes the MsgPack as a map
use msgpack_simple::MsgPack; assert_eq!(MsgPack::Map(vec![]).as_map().unwrap(), vec![]);
pub fn is_extension(&self) -> bool
[src]
Checks if the MsgPack is an extension
use msgpack_simple::{MsgPack, Extension}; let value = Extension { type_id: 42, value: vec![0x42] }; assert_eq!(MsgPack::Extension(value).is_extension(), true); assert_eq!(MsgPack::Binary(vec![0x42]).is_extension(), false);
pub fn as_extension(self) -> Result<Extension, ConversionError>
[src]
Consumes the MsgPack as an extension
use msgpack_simple::{MsgPack, Extension}; let value = Extension { type_id: 42, value: vec![0x42] }; assert_eq!(MsgPack::Extension(value.clone()).as_extension().unwrap(), value);
Trait Implementations
impl Clone for MsgPack
[src]
impl Debug for MsgPack
[src]
impl Display for MsgPack
[src]
impl PartialEq<MsgPack> for MsgPack
[src]
impl StructuralPartialEq for MsgPack
[src]
Auto Trait Implementations
impl RefUnwindSafe for MsgPack
impl Send for MsgPack
impl Sync for MsgPack
impl Unpin for MsgPack
impl UnwindSafe for MsgPack
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,