util/frame/audio: use a trait instead of type_of

This commit is contained in:
meh 2015-06-10 22:03:59 +02:00
parent 24af20b79d
commit 0bed7e7346

View File

@ -1,13 +1,11 @@
use std::mem; use std::mem;
use std::slice; use std::slice;
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::any::TypeId;
use std::marker::Reflect;
use libc::{c_int, int64_t, c_ulonglong}; use libc::{c_int, int64_t, c_ulonglong};
use ffi::*; use ffi::*;
use ::ChannelLayout; use ::ChannelLayout;
use ::util::format::Sample; use ::util::format;
use super::Frame; use super::Frame;
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
@ -18,7 +16,7 @@ impl Audio {
Audio(Frame::wrap(ptr)) Audio(Frame::wrap(ptr))
} }
pub unsafe fn alloc(&mut self, format: Sample, samples: usize, layout: ChannelLayout) { pub unsafe fn alloc(&mut self, format: format::Sample, samples: usize, layout: ChannelLayout) {
self.set_format(format); self.set_format(format);
self.set_samples(samples); self.set_samples(samples);
self.set_channel_layout(layout); self.set_channel_layout(layout);
@ -34,7 +32,7 @@ impl Audio {
} }
} }
pub fn new(format: Sample, samples: usize, layout: ChannelLayout) -> Self { pub fn new(format: format::Sample, samples: usize, layout: ChannelLayout) -> Self {
unsafe { unsafe {
let mut frame = Audio::empty(); let mut frame = Audio::empty();
frame.alloc(format, samples, layout); frame.alloc(format, samples, layout);
@ -43,18 +41,18 @@ impl Audio {
} }
} }
pub fn format(&self) -> Sample { pub fn format(&self) -> format::Sample {
unsafe { unsafe {
if (*self.as_ptr()).format == -1 { if (*self.as_ptr()).format == -1 {
Sample::None format::Sample::None
} }
else { else {
Sample::from(mem::transmute::<_, AVSampleFormat>(((*self.as_ptr()).format))) format::Sample::from(mem::transmute::<_, AVSampleFormat>(((*self.as_ptr()).format)))
} }
} }
} }
pub fn set_format(&mut self, value: Sample) { pub fn set_format(&mut self, value: format::Sample) {
unsafe { unsafe {
(*self.as_mut_ptr()).format = mem::transmute::<AVSampleFormat, c_int>(value.into()); (*self.as_mut_ptr()).format = mem::transmute::<AVSampleFormat, c_int>(value.into());
} }
@ -131,12 +129,12 @@ impl Audio {
} }
} }
pub fn plane<T: Reflect + 'static>(&self, index: usize) -> &[T] { pub fn plane<T: Sample>(&self, index: usize) -> &[T] {
if index >= self.planes() { if index >= self.planes() {
panic!("out of bounds"); panic!("out of bounds");
} }
if !valid::<T>(self.format()) { if !<T as Sample>::is_valid(self.format()) {
panic!("unsupported type"); panic!("unsupported type");
} }
@ -147,12 +145,12 @@ impl Audio {
} }
} }
pub fn plane_mut<T: Reflect + 'static>(&mut self, index: usize) -> &[T] { pub fn plane_mut<T: Sample>(&mut self, index: usize) -> &[T] {
if index >= self.planes() { if index >= self.planes() {
panic!("out of bounds"); panic!("out of bounds");
} }
if !valid::<T>(self.format()) { if !<T as Sample>::is_valid(self.format()) {
panic!("unsupported type"); panic!("unsupported type");
} }
@ -222,27 +220,61 @@ impl Clone for Audio {
} }
} }
fn valid<T: Reflect + 'static>(format: Sample) -> bool { pub trait Sample {
match format { fn is_valid(format: format::Sample) -> bool;
Sample::None => }
false,
Sample::U8(..) if TypeId::of::<T>() != TypeId::of::<u8>() => impl Sample for u8 {
false, fn is_valid(format: format::Sample) -> bool {
if let format::Sample::U8(..) = format {
Sample::I16(..) if TypeId::of::<T>() != TypeId::of::<i16>() =>
false,
Sample::I32(..) if TypeId::of::<T>() != TypeId::of::<i32>() =>
false,
Sample::F32(..) if TypeId::of::<T>() != TypeId::of::<f32>() =>
false,
Sample::F64(..) if TypeId::of::<T>() != TypeId::of::<f64>() =>
false,
_ =>
true true
}
else {
false
}
}
}
impl Sample for i16 {
fn is_valid(format: format::Sample) -> bool {
if let format::Sample::I16(..) = format {
true
}
else {
false
}
}
}
impl Sample for i32 {
fn is_valid(format: format::Sample) -> bool {
if let format::Sample::I32(..) = format {
true
}
else {
false
}
}
}
impl Sample for f32 {
fn is_valid(format: format::Sample) -> bool {
if let format::Sample::F32(..) = format {
true
}
else {
false
}
}
}
impl Sample for f64 {
fn is_valid(format: format::Sample) -> bool {
if let format::Sample::F64(..) = format {
true
}
else {
false
}
} }
} }