From 647d055d8443cded1591c0d793b9bec91df5755b Mon Sep 17 00:00:00 2001 From: Matt Bilker Date: Wed, 12 Dec 2018 20:56:37 +0000 Subject: [PATCH] value: add as_i16, as_u16, as_i64, and as_u64 helper methods --- src/value/mod.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/value/mod.rs b/src/value/mod.rs index 96e23b8..259d050 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -503,6 +503,20 @@ macro_rules! construct_types { } } + pub fn as_i16(&self) -> Result { + match self { + Value::S16(ref n) => Ok(*n), + value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::S16, value.clone()).into()), + } + } + + pub fn as_u16(&self) -> Result { + match self { + Value::U16(ref n) => Ok(*n), + value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::U16, value.clone()).into()), + } + } + pub fn as_i32(&self) -> Result { match self { Value::S32(ref n) => Ok(*n), @@ -517,6 +531,20 @@ macro_rules! construct_types { } } + pub fn as_i64(&self) -> Result { + match self { + Value::S64(ref n) => Ok(*n), + value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::S64, value.clone()).into()), + } + } + + pub fn as_u64(&self) -> Result { + match self { + Value::U64(ref n) => Ok(*n), + value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::U64, value.clone()).into()), + } + } + pub fn as_slice(&self) -> Result<&[u8], KbinError> { match self { Value::Binary(ref data) => Ok(data),