use core::ops::Range;
use core::convert::{TryFrom, TryInto};
pub trait FromData: Sized {
const SIZE: usize;
fn parse(data: &[u8]) -> Option<Self>;
}
impl FromData for () {
const SIZE: usize = 0;
#[inline]
fn parse(_: &[u8]) -> Option<Self> {
Some(())
}
}
impl FromData for u8 {
const SIZE: usize = 1;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
data.get(0).copied()
}
}
impl FromData for i8 {
const SIZE: usize = 1;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
data.get(0).copied().map(|n| n as i8)
}
}
impl FromData for u16 {
const SIZE: usize = 2;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
data.try_into().ok().map(u16::from_be_bytes)
}
}
impl FromData for i16 {
const SIZE: usize = 2;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
data.try_into().ok().map(i16::from_be_bytes)
}
}
impl FromData for u32 {
const SIZE: usize = 4;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
data.try_into().ok().map(u32::from_be_bytes)
}
}
impl FromData for i32 {
const SIZE: usize = 4;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
data.try_into().ok().map(i32::from_be_bytes)
}
}
#[derive(Clone, Copy, Debug)]
pub struct U24(pub u32);
impl FromData for U24 {
const SIZE: usize = 3;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let data: [u8; 3] = data.try_into().ok()?;
Some(U24(u32::from_be_bytes([0, data[0], data[1], data[2]])))
}
}
#[derive(Clone, Copy, Debug)]
pub struct F2DOT14(pub i16);
impl F2DOT14 {
#[inline]
pub fn to_f32(&self) -> f32 {
f32::from(self.0) / 16384.0
}
}
impl FromData for F2DOT14 {
const SIZE: usize = 2;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
i16::parse(data).map(F2DOT14)
}
}
#[derive(Clone, Copy, Debug)]
pub struct Fixed(pub f32);
impl FromData for Fixed {
const SIZE: usize = 4;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
i32::parse(data).map(|n| Fixed(n as f32 / 65536.0))
}
}
pub trait NumFrom<T>: Sized {
fn num_from(_: T) -> Self;
}
impl NumFrom<u32> for usize {
#[inline]
fn num_from(v: u32) -> Self {
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
{
v as usize
}
}
}
pub trait TryNumFrom<T>: Sized {
fn try_num_from(_: T) -> Option<Self>;
}
impl TryNumFrom<f32> for u8 {
#[inline]
fn try_num_from(v: f32) -> Option<Self> {
i32::try_num_from(v).and_then(|v| u8::try_from(v).ok())
}
}
impl TryNumFrom<f32> for i16 {
#[inline]
fn try_num_from(v: f32) -> Option<Self> {
i32::try_num_from(v).and_then(|v| i16::try_from(v).ok())
}
}
impl TryNumFrom<f32> for u16 {
#[inline]
fn try_num_from(v: f32) -> Option<Self> {
i32::try_num_from(v).and_then(|v| u16::try_from(v).ok())
}
}
impl TryNumFrom<f32> for i32 {
#[inline]
fn try_num_from(v: f32) -> Option<Self> {
const MIN: f32 = core::i32::MIN as f32;
const MAX_P1: f32 = core::i32::MAX as f32;
if v >= MIN && v < MAX_P1 {
Some(v as i32)
} else {
None
}
}
}
#[derive(Clone, Copy)]
pub struct LazyArray16<'a, T> {
data: &'a [u8],
data_type: core::marker::PhantomData<T>,
}
impl<T> Default for LazyArray16<'_, T> {
#[inline]
fn default() -> Self {
LazyArray16 {
data: &[],
data_type: core::marker::PhantomData,
}
}
}
impl<'a, T: FromData> LazyArray16<'a, T> {
#[inline]
pub fn new(data: &'a [u8]) -> Self {
LazyArray16 {
data,
data_type: core::marker::PhantomData,
}
}
#[inline]
pub fn get(&self, index: u16) -> Option<T> {
if index < self.len() {
let start = usize::from(index) * T::SIZE;
let end = start + T::SIZE;
self.data.get(start..end).and_then(T::parse)
} else {
None
}
}
#[inline]
pub fn last(&self) -> Option<T> {
if !self.is_empty() {
self.get(self.len() - 1)
} else {
None
}
}
#[inline]
pub fn slice(&self, range: Range<u16>) -> Option<Self> {
let start = usize::from(range.start) * T::SIZE;
let end = usize::from(range.end) * T::SIZE;
Some(LazyArray16 {
data: self.data.get(start..end)?,
..LazyArray16::default()
})
}
#[inline]
pub fn len(&self) -> u16 {
(self.data.len() / T::SIZE) as u16
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn binary_search(&self, key: &T) -> Option<(u16, T)>
where T: Ord
{
self.binary_search_by(|p| p.cmp(key))
}
#[inline]
pub fn binary_search_by<F>(&self, mut f: F) -> Option<(u16, T)>
where F: FnMut(&T) -> core::cmp::Ordering
{
use core::cmp::Ordering;
let mut size = self.len();
if size == 0 {
return None;
}
let mut base = 0;
while size > 1 {
let half = size / 2;
let mid = base + half;
let cmp = f(&self.get(mid)?);
base = if cmp == Ordering::Greater { base } else { mid };
size -= half;
}
let value = self.get(base)?;
if f(&value) == Ordering::Equal { Some((base, value)) } else { None }
}
}
impl<'a, T: FromData + core::fmt::Debug + Copy> core::fmt::Debug for LazyArray16<'a, T> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_list().entries(self.into_iter()).finish()
}
}
impl<'a, T: FromData> IntoIterator for LazyArray16<'a, T> {
type Item = T;
type IntoIter = LazyArrayIter16<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
LazyArrayIter16 {
data: self,
index: 0,
}
}
}
#[derive(Clone, Copy)]
#[allow(missing_debug_implementations)]
pub struct LazyArrayIter16<'a, T> {
data: LazyArray16<'a, T>,
index: u16,
}
impl<T: FromData> Default for LazyArrayIter16<'_, T> {
#[inline]
fn default() -> Self {
LazyArrayIter16 {
data: LazyArray16::new(&[]),
index: 0,
}
}
}
impl<'a, T: FromData> Iterator for LazyArrayIter16<'a, T> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.index += 1; self.data.get(self.index - 1)
}
}
#[derive(Clone, Copy)]
pub struct LazyArray32<'a, T> {
data: &'a [u8],
data_type: core::marker::PhantomData<T>,
}
impl<T> Default for LazyArray32<'_, T> {
#[inline]
fn default() -> Self {
LazyArray32 {
data: &[],
data_type: core::marker::PhantomData,
}
}
}
impl<'a, T: FromData> LazyArray32<'a, T> {
#[inline]
pub fn new(data: &'a [u8]) -> Self {
LazyArray32 {
data,
data_type: core::marker::PhantomData,
}
}
#[inline]
pub fn get(&self, index: u32) -> Option<T> {
if index < self.len() {
let start = usize::num_from(index) * T::SIZE;
let end = start + T::SIZE;
self.data.get(start..end).and_then(T::parse)
} else {
None
}
}
#[inline]
pub fn len(&self) -> u32 {
(self.data.len() / T::SIZE) as u32
}
#[inline]
pub fn binary_search(&self, key: &T) -> Option<(u32, T)>
where T: Ord
{
self.binary_search_by(|p| p.cmp(key))
}
#[inline]
pub fn binary_search_by<F>(&self, mut f: F) -> Option<(u32, T)>
where F: FnMut(&T) -> core::cmp::Ordering
{
use core::cmp::Ordering;
let mut size = self.len();
if size == 0 {
return None;
}
let mut base = 0;
while size > 1 {
let half = size / 2;
let mid = base + half;
let cmp = f(&self.get(mid)?);
base = if cmp == Ordering::Greater { base } else { mid };
size -= half;
}
let value = self.get(base)?;
if f(&value) == Ordering::Equal { Some((base, value)) } else { None }
}
}
impl<'a, T: FromData + core::fmt::Debug + Copy> core::fmt::Debug for LazyArray32<'a, T> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_list().entries(self.into_iter()).finish()
}
}
impl<'a, T: FromData> IntoIterator for LazyArray32<'a, T> {
type Item = T;
type IntoIter = LazyArrayIter32<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
LazyArrayIter32 {
data: self,
index: 0,
}
}
}
#[derive(Clone, Copy)]
#[allow(missing_debug_implementations)]
pub struct LazyArrayIter32<'a, T> {
data: LazyArray32<'a, T>,
index: u32,
}
impl<'a, T: FromData> Iterator for LazyArrayIter32<'a, T> {
type Item = T;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.index += 1; self.data.get(self.index - 1)
}
#[inline]
fn count(self) -> usize {
usize::num_from(self.data.len())
}
}
#[derive(Clone, Copy, Default, Debug)]
pub struct Stream<'a> {
data: &'a [u8],
offset: usize,
}
impl<'a> Stream<'a> {
#[inline]
pub fn new(data: &'a [u8]) -> Self {
Stream { data, offset: 0 }
}
#[inline]
pub fn new_at(data: &'a [u8], offset: usize) -> Option<Self> {
if offset <= data.len() {
Some(Stream { data, offset })
} else {
None
}
}
#[inline]
pub fn at_end(&self) -> bool {
self.offset >= self.data.len()
}
#[inline]
pub fn jump_to_end(&mut self) {
self.offset = self.data.len();
}
#[inline]
pub fn offset(&self) -> usize {
self.offset
}
#[inline]
pub fn tail(&self) -> Option<&'a [u8]> {
self.data.get(self.offset..)
}
#[inline]
pub fn skip<T: FromData>(&mut self) {
self.advance(T::SIZE);
}
#[inline]
pub fn advance(&mut self, len: usize) {
self.offset += len;
}
#[inline]
pub fn advance_checked(&mut self, len: usize) -> Option<()> {
if self.offset + len <= self.data.len() {
self.advance(len);
Some(())
} else {
None
}
}
#[inline]
pub fn read<T: FromData>(&mut self) -> Option<T> {
self.read_bytes(T::SIZE).and_then(T::parse)
}
#[inline]
pub fn read_at<T: FromData>(data: &[u8], offset: usize) -> Option<T> {
data.get(offset..offset + T::SIZE).and_then(T::parse)
}
#[inline]
pub fn read_bytes(&mut self, len: usize) -> Option<&'a [u8]> {
let v = self.data.get(self.offset..self.offset + len)?;
self.advance(len);
Some(v)
}
#[inline]
pub fn read_array16<T: FromData>(&mut self, count: u16) -> Option<LazyArray16<'a, T>> {
let len = usize::from(count) * T::SIZE;
self.read_bytes(len).map(LazyArray16::new)
}
#[inline]
pub fn read_array32<T: FromData>(&mut self, count: u32) -> Option<LazyArray32<'a, T>> {
let len = usize::num_from(count) * T::SIZE;
self.read_bytes(len).map(LazyArray32::new)
}
}
pub trait Offset {
fn to_usize(&self) -> usize;
fn is_null(&self) -> bool { self.to_usize() == 0 }
}
#[derive(Clone, Copy, Debug)]
pub struct Offset16(pub u16);
impl Offset for Offset16 {
#[inline]
fn to_usize(&self) -> usize {
usize::from(self.0)
}
}
impl FromData for Offset16 {
const SIZE: usize = 2;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
u16::parse(data).map(Offset16)
}
}
impl FromData for Option<Offset16> {
const SIZE: usize = Offset16::SIZE;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let offset = Offset16::parse(data)?;
if offset.0 != 0 { Some(Some(offset)) } else { Some(None) }
}
}
#[derive(Clone, Copy, Debug)]
pub struct Offset32(pub u32);
impl Offset for Offset32 {
#[inline]
fn to_usize(&self) -> usize {
usize::num_from(self.0)
}
}
impl FromData for Offset32 {
const SIZE: usize = 4;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
u32::parse(data).map(Offset32)
}
}
impl FromData for Option<Offset32> {
const SIZE: usize = Offset32::SIZE;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let offset = Offset32::parse(data)?;
if offset.0 != 0 { Some(Some(offset)) } else { Some(None) }
}
}
#[inline]
pub(crate) fn i16_bound(min: i16, val: i16, max: i16) -> i16 {
use core::cmp;
cmp::max(min, cmp::min(max, val))
}
#[inline]
pub(crate) fn f32_bound(min: f32, val: f32, max: f32) -> f32 {
debug_assert!(min.is_finite());
debug_assert!(val.is_finite());
debug_assert!(max.is_finite());
if val > max {
return max;
} else if val < min {
return min;
}
val
}