value: add TryFrom implementations to get inner type from value

This commit is contained in:
Matt Bilker
2018-12-12 07:57:17 +00:00
parent 01f407f1ca
commit f6fbf001f5
3 changed files with 16 additions and 0 deletions

View File

@@ -23,3 +23,4 @@ serde_derive = { version = "1.0.79", optional = true }
[features]
serde-1 = ["serde", "serde_bytes", "serde_derive", "indexmap/serde-1"]
try_from = []

View File

@@ -1,4 +1,5 @@
#![cfg_attr(test, feature(test))]
#![cfg_attr(feature = "try_from", feature(try_from))]
extern crate byteorder;
extern crate bytes;

View File

@@ -1,3 +1,5 @@
#[cfg(feature = "try_from")]
use std::convert::TryFrom;
use std::fmt;
use std::net::Ipv4Addr;
use std::str::FromStr;
@@ -412,6 +414,18 @@ macro_rules! construct_types {
Value::$konst(value)
}
}
#[cfg(feature = "try_from")]
impl TryFrom<Value> for $($value_type)* {
type Error = KbinError;
fn try_from(value: Value) -> Result<Self, Self::Error> {
match value {
Value::$konst(v) => Ok(v),
value => Err(KbinErrorKind::ValueTypeMismatch(StandardType::$konst, value).into()),
}
}
}
)+
impl Value {