codec: use Option<_> in the getter API

This commit is contained in:
meh
2015-08-29 16:15:57 +02:00
parent b39d8df4ee
commit 8ed092c0c3
4 changed files with 147 additions and 69 deletions

View File

@ -18,21 +18,36 @@ impl<'a> Audio<'a> {
}
impl<'a> Audio<'a> {
pub fn rates(&self) -> RateIter {
pub fn rates(&self) -> Option<RateIter> {
unsafe {
RateIter::new((*self.codec.as_ptr()).supported_samplerates)
if (*self.as_ptr()).supported_samplerates.is_null() {
None
}
else {
Some(RateIter::new((*self.codec.as_ptr()).supported_samplerates))
}
}
}
pub fn formats(&self) -> FormatIter {
pub fn formats(&self) -> Option<FormatIter> {
unsafe {
FormatIter::new((*self.codec.as_ptr()).sample_fmts)
if (*self.codec.as_ptr()).sample_fmts.is_null() {
None
}
else {
Some(FormatIter::new((*self.codec.as_ptr()).sample_fmts))
}
}
}
pub fn channel_layouts(&self) -> ChannelLayoutIter {
pub fn channel_layouts(&self) -> Option<ChannelLayoutIter> {
unsafe {
ChannelLayoutIter::new((*self.codec.as_ptr()).channel_layouts)
if (*self.codec.as_ptr()).channel_layouts.is_null() {
None
}
else {
Some(ChannelLayoutIter::new((*self.codec.as_ptr()).channel_layouts))
}
}
}
}
@ -62,15 +77,14 @@ impl<'a> Iterator for RateIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
if !self.ptr.is_null() && (*self.ptr) != 0 {
let rate = *self.ptr;
self.ptr = self.ptr.offset(1);
if *self.ptr == 0 {
return None;
}
Some(rate)
}
else {
None
}
let rate = *self.ptr;
self.ptr = self.ptr.offset(1);
Some(rate)
}
}
}
@ -92,15 +106,14 @@ impl<'a> Iterator for FormatIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
if !self.ptr.is_null() && (*self.ptr) != AVSampleFormat::AV_SAMPLE_FMT_NONE {
let format = (*self.ptr).into();
self.ptr = self.ptr.offset(1);
if *self.ptr == AVSampleFormat::AV_SAMPLE_FMT_NONE {
return None;
}
Some(format)
}
else {
None
}
let format = (*self.ptr).into();
self.ptr = self.ptr.offset(1);
Some(format)
}
}
}
@ -122,15 +135,14 @@ impl<'a> Iterator for ChannelLayoutIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
if !self.ptr.is_null() && (*self.ptr) != 0 {
let chl = ChannelLayout::from_bits_truncate(*self.ptr);
self.ptr = self.ptr.offset(1);
if *self.ptr == 0 {
return None;
}
Some(chl)
}
else {
None
}
let layout = ChannelLayout::from_bits_truncate(*self.ptr);
self.ptr = self.ptr.offset(1);
Some(layout)
}
}
}

View File

@ -105,7 +105,7 @@ impl<'a> Codec<'a> {
pub fn profiles(&self) -> Option<ProfileIter> {
unsafe {
if (*self.as_ptr()).profiles.is_null() {
return None;
None
}
else {
Some(ProfileIter::new(self.id(), (*self.as_ptr()).profiles))
@ -132,15 +132,14 @@ impl<'a> Iterator for ProfileIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
if (*self.ptr).profile != FF_PROFILE_UNKNOWN && !(*self.ptr).name.is_null() {
let profile = Profile::from((self.id, (*self.ptr).profile));
self.ptr = self.ptr.offset(1);
if (*self.ptr).profile == FF_PROFILE_UNKNOWN {
return None;
}
Some(profile)
}
else {
None
}
let profile = Profile::from((self.id, (*self.ptr).profile));
self.ptr = self.ptr.offset(1);
Some(profile)
}
}
}

View File

@ -18,15 +18,25 @@ impl<'a> Video<'a> {
}
impl<'a> Video<'a> {
pub fn rates(&self) -> RateIter {
pub fn rates(&self) -> Option<RateIter> {
unsafe {
RateIter::new((*self.codec.as_ptr()).supported_framerates)
if (*self.codec.as_ptr()).supported_framerates.is_null() {
None
}
else {
Some(RateIter::new((*self.codec.as_ptr()).supported_framerates))
}
}
}
pub fn formats(&self) -> FormatIter {
pub fn formats(&self) -> Option<FormatIter> {
unsafe {
FormatIter::new((*self.codec.as_ptr()).pix_fmts)
if (*self.codec.as_ptr()).pix_fmts.is_null() {
None
}
else {
Some(FormatIter::new((*self.codec.as_ptr()).pix_fmts))
}
}
}
}
@ -56,15 +66,14 @@ impl<'a> Iterator for RateIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
if !self.ptr.is_null() && (*self.ptr) != (AVRational { num: 0, den: 0 }) {
let rate = (*self.ptr).into();
self.ptr = self.ptr.offset(1);
if (*self.ptr).num == 0 && (*self.ptr).den == 0 {
return None;
}
Some(rate)
}
else {
None
}
let rate = (*self.ptr).into();
self.ptr = self.ptr.offset(1);
Some(rate)
}
}
}
@ -86,15 +95,14 @@ impl<'a> Iterator for FormatIter<'a> {
fn next(&mut self) -> Option<<Self as Iterator>::Item> {
unsafe {
if !self.ptr.is_null() && (*self.ptr) != AVPixelFormat::AV_PIX_FMT_NONE {
let format = (*self.ptr).into();
self.ptr = self.ptr.offset(1);
if *self.ptr == AVPixelFormat::AV_PIX_FMT_NONE {
return None;
}
Some(format)
}
else {
None
}
let format = (*self.ptr).into();
self.ptr = self.ptr.offset(1);
Some(format)
}
}
}