use std::ffi::CStr;
use std::convert::TryInto;
#[derive(Debug)]
pub enum TypedValue<'a> {
I8(&'a [i8]),
U8(&'a [u8]),
I16(&'a [i16]),
U16(&'a [u16]),
I32(&'a [i32]),
U32(&'a [u32]),
F(&'a [f32]),
Str(&'a CStr),
SStr(&'a [u8]),
Null,
}
impl <'a> TypedValue<'a> {
fn cast_int<T: TryInto<i64> + Copy>(slice: &[T], idx: usize) -> Option<i64> {
let value = *slice.get(idx)?;
value.try_into().ok()
}
pub fn get_int(&self, idx: usize) -> Option<i64> {
use TypedValue::*;
match self {
I8(v) => Self::cast_int(v, idx),
U8(v) => Self::cast_int(v, idx),
I16(v) => Self::cast_int(v, idx),
U16(v) => Self::cast_int(v, idx),
I32(v) => Self::cast_int(v, idx),
U32(v) => Self::cast_int(v, idx),
_ => None
}
}
}