(valence_nbt) Add reverse From<Value> for Option<T> (#122)

This way we can use generic bounds to specify the return type and get `None` if the value does not match the type.
This commit is contained in:
Terminator 2022-10-18 23:40:46 +02:00 committed by GitHub
parent dc46f27d5d
commit 03e89adeb8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -234,3 +234,123 @@ impl From<Vec<Vec<i64>>> for List {
List::LongArray(v)
}
}
impl From<Value> for Option<i8> {
fn from(value: Value) -> Self {
if let Value::Byte(b) = value {
Some(b)
} else {
None
}
}
}
impl From<Value> for Option<i16> {
fn from(value: Value) -> Self {
if let Value::Short(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<i32> {
fn from(value: Value) -> Self {
if let Value::Int(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<i64> {
fn from(value: Value) -> Self {
if let Value::Long(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<f32> {
fn from(value: Value) -> Self {
if let Value::Float(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<f64> {
fn from(value: Value) -> Self {
if let Value::Double(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<Vec<i8>> {
fn from(value: Value) -> Self {
if let Value::ByteArray(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<String> {
fn from(value: Value) -> Self {
if let Value::String(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<List> {
fn from(value: Value) -> Self {
if let Value::List(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<Compound> {
fn from(value: Value) -> Self {
if let Value::Compound(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<Vec<i32>> {
fn from(value: Value) -> Self {
if let Value::IntArray(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<Vec<i64>> {
fn from(value: Value) -> Self {
if let Value::LongArray(val) = value {
Some(val)
} else {
None
}
}
}